MySQL慢查询 - “等待查询缓存锁定”

时间:2011-03-18 19:35:16

标签: mysql

我在与运行5.5的服务器相同的机器上的简单表上运行简单查询。从一个2000万行表中返回~7000行需要22秒。在分析时,大部分时间被多个"等待查询缓存锁定"。什么是"等待查询缓存锁定"为什么这个查询需要这么长时间?这与我设置服务器的方式有关吗?

以下是配置文件(请注意,操作的时间实际上来自下面的行here):

mysql> show profile for query 4;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000015 |
| Waiting for query cache lock   | 0.000003 |
| checking query cache for query | 0.000045 |
| checking permissions           | 0.000006 |
| Opening tables                 | 0.000027 |
| System lock                    | 0.000007 |
| Waiting for query cache lock   | 0.000032 |
| init                           | 0.000018 |
| optimizing                     | 0.000008 |
| statistics                     | 0.033109 |
| preparing                      | 0.000019 |
| executing                      | 0.000002 |
| Sending data                   | 4.575480 |
| Waiting for query cache lock   | 0.000005 |
| Sending data                   | 5.527728 |
| Waiting for query cache lock   | 0.000005 |
| Sending data                   | 5.743041 |
| Waiting for query cache lock   | 0.000004 |
| Sending data                   | 6.191706 |
| end                            | 0.000007 |
| query end                      | 0.000005 |
| closing tables                 | 0.000028 |
| freeing items                  | 0.000008 |
| Waiting for query cache lock   | 0.000002 |
| freeing items                  | 0.000182 |
| Waiting for query cache lock   | 0.000002 |
| freeing items                  | 0.000002 |
| storing result in query cache  | 0.000004 |
| logging slow query             | 0.000001 |
| logging slow query             | 0.000002 |
| cleaning up                    | 0.000003 |
+--------------------------------+----------+

这是表格:

mysql> SHOW CREATE TABLE prvol;

"Table","Create Table"
"prvol","CREATE TABLE `prvol` (
  `ticker` varchar(10) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `close` float unsigned DEFAULT NULL,
  KEY `Index 1` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1"

以下是查询:

mysql> select close from prvol where date = '20100203';

编辑:使用SQL_NO_CACHE运行后,所有时间现在都在执行中。在2.4GHz,3GB的ram机器上,这个尺寸的桌子是否正常?

+----------------------+-----------+
| Status               | Duration  |
+----------------------+-----------+
| starting             |  0.000052 |
| checking permissions |  0.000007 |
| Opening tables       |  0.000027 |
| System lock          |  0.000008 |
| init                 |  0.000019 |
| optimizing           |  0.000008 |
| statistics           |  0.034766 |
| preparing            |  0.000011 |
| executing            |  0.000002 |
| Sending data         | 22.071324 |
| end                  |  0.000012 |
| query end            |  0.000005 |
| closing tables       |  0.000020 |
| freeing items        |  0.000170 |
| logging slow query   |  0.000001 |
| logging slow query   |  0.000003 |
| cleaning up          |  0.000004 |
+----------------------+-----------+

编辑:包括解释结果。

mysql> explain extended select cp from prvol where date = '20100208';
+----+-------------+-------+------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key     | key_len | ref   |rows  | filtered | Extra       |
+----+-------------+-------+------+---------------+---------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | prvol | ref  | Index 1       | Index 1 | 4       | const |6868  |   100.00 | Using where |
+----+-------------+-------+------+---------------+---------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.08 sec)

2 个答案:

答案 0 :(得分:11)

我解决了我的慢查询问题。总结一下这个问题,从20毫升行,1.7GB索引表中查询7000行需要22秒。问题是缓存太小,查询必须为每个查询转到磁盘。我认为磁盘访问速度会比我看到的要快,因为我要从索引列开始,因此从磁盘读取的数据量应该很小。但我猜测在磁盘上访问InnoDB存储会有很多开销。

在my.ini文件中设置innodb_buffer_pool_size=1024M后,初始查询会花费很长时间,但所有后续查询都会在一秒钟内完成。

不幸的是,分析并没有真正帮助。

答案 1 :(得分:9)

这是MySQL的一个已知问题。这里描述得非常好:

https://web.archive.org/web/20160129162137/http://www.psce.com/blog/kb/how-query-cache-can-cause-performance-problems/

查询缓存可以帮助你很多,但同时它可能成为瓶颈。