MySQL慢查询 - "等待查询缓存锁定"

Ric*_*h C 21 mysql

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

这里是配置文件(注意,该操作的时间实际上是从陈述如下行这里):

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 |
+--------------------------------+----------+
Run Code Online (Sandbox Code Playgroud)

这是表格:

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"
Run Code Online (Sandbox Code Playgroud)

这是查询:

mysql> select close from prvol where date = '20100203';
Run Code Online (Sandbox Code Playgroud)

编辑:运行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 |
+----------------------+-----------+
Run Code Online (Sandbox Code Playgroud)

编辑:包括解释结果.

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)
Run Code Online (Sandbox Code Playgroud)

Ric*_*h C 12

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

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

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


小智 10

这是MySQL的一个已知问题.这里描述得很好:

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

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