2010年12月27日星期一

MySQL profile中'Sending data'很慢的问题

这几天,对MySQL(5.0.77)数据库中一个几百行记录的表(用于任务分配,读写比较频繁),进行的一个简单查询,从1s逐渐增大到15s。

mysql> select id from t_task limit 1;
+----------+
| id       |
+----------+
| 22865305 |
+----------+
1 row in set (15.24 sec)
Profiling的结果是:
 mysql> show profile for query 1;
+--------------------------------+-----------+
| Status                         | Duration  |
+--------------------------------+-----------+
| starting                       |  0.000014 |
| checking query cache for query |  0.000032 |
| Opening tables                 |  0.000012 |
| System lock                    |  0.000003 |
| Table lock                     |  0.000006 |
| init                           |  0.000010 |
| optimizing                     |  0.000003 |
| statistics                     |  0.000006 |
| preparing                      |  0.000004 |
| executing                      |  0.000003 |
| Sending data                   | 15.242734 |
| end                            |  0.000011 |
| query end                      |  0.000002 |
| freeing items                  |  0.000013 |
| logging slow query             |  0.000003 |
| logging slow query             |  0.000002 |
| cleaning up                    |  0.000002 |
+--------------------------------+-----------+
17 rows in set (0.00 sec)
从字面意思看,是慢在‘传输数据’这一步。

‘Sending data’的意义查不到具体的说明,这里http://forums.mysql.com/read.php?24,241461,242012#msg-242012有人说是:
Each time means the time elapsed between the previous event and the new event. So, the line:
| Sending data | 0.00016800 |
means that 0.00016800 seconds elapsed between "executing" and "Sending data". It is, it takes 0.00016800 seconds to execute the query.



可以在sql_select.cc里面看到源代码(在里面搜索Sending data),可以看到do_select的时间也算在‘sending data‘里面了。


所以这个‘sending data‘并不只是数据传输的时间,也包含select的时间。有人就这个语义含糊的问题给MySQL报过bug:http://bugs.mysql.com/bug.php?id=52492,但是不了了之。

这个问题最初怀疑是网卡有问题(这台机器之前网卡有问题,更新过驱动),但是新建一张表结构一样,数据量一样的表,做同样的查询,速度又很快。又怀疑是磁盘问题,因为根据pk的查询都很快(因为簇集索引所以都缓存在内存里面),vmstat和iostat都没有发现异常。

准备晚上升级MySQL,并使用HP BL460刀片机替换这台出过硬件故障的IBM x3650M2。