mysql - SQL_NO_CACHE 不起作用

标签 mysql sql

第一次运行这个sql,需要39秒,再次运行增加SQL_NO_CACHE,好像没有生效:

mysql> select count(*) from `deal_expired` where `site`=8&&`area`=122 && 
endtime<1310444996056;
+----------+
| count(*) |
+----------+
|      497 |
+----------+
1 row in set (39.55 sec)

mysql> select SQL_NO_CACHE count(*) from `deal_expired` where `site`=8&&`area`=
122 && endtime<1310444996056;
+----------+
| count(*) |
+----------+
|      497 |
+----------+
1 row in set (0.16 sec)

我尝试了多种方法,here

甚至重启mysql服务器或更改表名,但我仍然不能让39秒运行这条SQL

我换了另一个SQL,并在第一次运行时增加了SQL_NO_CACHE,问题是一样的:

mysql> select SQL_NO_CACHE count(*) from `deal_expired` where `site`=25&&`area`=
134 && endtime<1310483196227;
+----------+
| count(*) |
+----------+
|      315 |
+----------+
1 row in set (2.17 sec)

mysql> select SQL_NO_CACHE count(*) from `deal_expired` where `site`=25&&`area`=
134 && endtime<1310483196227;
+----------+
| count(*) |
+----------+
|      315 |
+----------+
1 row in set (0.01 sec)

是什么原因? 如何获得相同的 SQL 运行时?

我想找到一种方法来优化这个 SQL 以执行 39 秒

顺便说一句:RESET QUERY CACHE FLUSH QUERY CACHE FLUSH TABLES SET SESSION query_cache_type=off 不起作用

mysql状态缓存已关闭:

mysql> SHOW STATUS LIKE "Qcache%";
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Qcache_free_blocks      | 0     |
| Qcache_free_memory      | 0     |
| Qcache_hits             | 0     |
| Qcache_inserts          | 0     |
| Qcache_lowmem_prunes    | 0     |
| Qcache_not_cached       | 0     |
| Qcache_queries_in_cache | 0     |
| Qcache_total_blocks     | 0     |
+-------------------------+-------+
8 rows in set (0.04 sec)

mysql> select count(*) from `deal_expired` where `site`=25&&`area`=134 && endtime<1310
483196227;
+----------+
| count(*) |
+----------+
|      315 |
+----------+
1 row in set (0.01 sec)

mysql> SHOW STATUS LIKE "Qcache%";
+-------------------------+-------+
| Variable_name           | Value |
+-------------------------+-------+
| Qcache_free_blocks      | 0     |
| Qcache_free_memory      | 0     |
| Qcache_hits             | 0     |
| Qcache_inserts          | 0     |
| Qcache_lowmem_prunes    | 0     |
| Qcache_not_cached       | 0     |
| Qcache_queries_in_cache | 0     |
| Qcache_total_blocks     | 0     |
+-------------------------+-------+
8 rows in set (0.00 sec)

解释这个SQL,使用site+endtime复合索引(命名为site_endtime):

mysql> explain select count(*) from `deal_expired` where `site`=8&&`area`=122 && endti
me<1310444996056;
+--------+------+-------------------------------+--------------+---------+------
-+------+-------------+
| table  | type | possible_keys                 | key          | key_len | ref
 | rows | Extra       |
+--------+------+-------------------------------+--------------+---------+------
-+------+-------------+
| deal_expired | ref  | name,url,endtime,site_endtime | site_endtime |       4 | const
 |  353 | Using where |
+--------+------+-------------------------------+--------------+---------+------
-+------+-------------+
1 row in set (0.00 sec)

最佳答案

第一个查询应该使用 SQL_NO_CACHE 告诉 MySQL 不要将 结果 放入缓存。第二个查询使用缓存,告诉 MySQL 不要缓存该查询的结果,它什么也不做。

tl;dr - 反转您的查询。

关于mysql - SQL_NO_CACHE 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6666631/

相关文章:

php - PDO fetchAll 命令中未返回值

php - 我如何像我的 register.php 一样连接我的 login.php?

php - codeigniter 检查同一表中不同表中可用的 id

mysql - 迁移中精确 10 位数字的整数验证

java - 无法将 TableRowSorter 添加到 SwingWorker 生成的 JTable 中

javascript - 如何限制 Google Bigquery 中的作业数量

mysql - 为什么 MySQL 中 (x IS NULL != y IS NULL) 需要额外的括号?

mysql - 动态建表时如何避免SQL注入(inject)风险?

php - 如何实现和调用多个php函数?

SQL最快的 'GROUP BY'脚本