TranslateProject/published/201407/20140703 How to find and kill misbehaving MySQL queries.md
2014-08-01 23:35:01 +08:00

63 lines
4.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

怎样把坏的MySQL查询找到并杀死
================================================================================
有时关系型相关数据库系统的复杂性会把你搞晕不过幸运的是使用MySQL工具来管理查询就就可以避免这些复杂性。 在本教程中,我将向你们展示 **怎样去查找并杀掉任何非法的MySQL查询**
为了浏览当前正在运行的查询登陆到MySQL终端然后运行show processlist命令:
mysql> show processlist;
+--------+--------+-----------------+---------+---------+-------+-------+------------------+-----------+---------------+-----------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | Rows_read |
+--------+--------+-----------------+---------+---------+-------+-------+------------------+-----------+---------------+-----------+
| 78233 | root | 127.0.0.1:37527 | mysql | Sleep | 16474 | | NULL | 6 | 6 | 6 |
| 84546 | root | 127.0.0.1:48593 | mysql | Sleep | 13237 | | NULL | 2 | 2 | 2 |
| 107083 | root | 127.0.0.1:56451 | mysql | Sleep | 15488 | | NULL | 1 | 121 | 121 |
| 131455 | root | 127.0.0.1:48550 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 0 |
+--------+--------+-----------------+---------+---------+-------+-------+------------------+-----------+---------------+-----------+
4 rows in set (0.03 sec)
首先你应该查看'Time'项,这里记录了进程执行 "做其当做的事情" 操作的秒数。command项处于Sleep
状态的进程表示其正在等待接受查询因此它并没有消耗任何资源。对于其他任何进程而言Time超过一定的秒数表明出现问题。
在上面的例子中唯一运行的查询是我们的show processlist命令。让我们来看看如果我们有一个写的很烂的查询是怎么样的:
mysql> show processlist;
+--------+--------+-----------------+-----------+---------+-------+--------------+----------------------------------+-----------+---------------+-----------+
| Id | User | Host | db | Command | Time | State | Info | Rows_sent | Rows_examined | Rows_read |
+--------+--------+-----------------+-----------+---------+-------+--------------+----------------------------------+-----------+---------------+-----------+
| 78233 | root | 127.0.0.1:37527 | example | Sleep | 18046 | | NULL | 6 | 6 | 6 |
| 84546 | root | 127.0.0.1:48593 | example | Sleep | 14809 | | NULL | 2 | 2 | 2 |
| 107083 | root | 127.0.0.1:56451 | example | Sleep | 17060 | | NULL | 1 | 121 | 121 |
| 132033 | root | 127.0.0.1:54642 | example | Query | 27 | Sending data | select max(subtotal) from orders | 0 | 0 | 0 |
| 133933 | root | 127.0.0.1:48679 | NULL | Query | 0 | NULL | show processlist | 0 | 0 | 0 |
| 134122 | root | 127.0.0.1:49264 | example | Sleep | 0 | | NULL | 0 | 0 | 0 |
+--------+--------+-----------------+-----------+---------+-------+--------------+----------------------------------+-----------+---------------+-----------+
6 rows in set (0.00 sec)
啊哈现在我们看到有一个查询运行了将近30秒。如果我们不想让它的进程继续运行可以将它的'Id'传递给kill命令:
mysql> kill 132033;
Query OK, 0 rows affected (0.00 sec)
mysql>
(注意 由于我们没有改变任何数据MySQL总是报告0行被影响。)
明智的使用kill命令能够清除积压的查询。然而要记住的是那不是一种永久的方法 - 如果这些查询来自你的程序,你需要去重写它们,或者将继续看到相同的问题不断出现。
### 另请参阅 ###
关于不同命令的MySQL文档:
- [https://dev.mysql.com/doc/refman/5.7/en/thread-commands.html][1]
--------------------------------------------------------------------------------
via: http://xmodulo.com/2014/07/find-kill-misbehaving-mysql-queries.html
译者:[hunanchenxingyu](https://github.com/hunanchenxingyu) 校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:https://dev.mysql.com/doc/refman/5.7/en/thread-commands.html