我第一次运行这个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秒
BTW: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)
答案 0 :(得分:39)
第一个查询应该使用SQL_NO_CACHE告诉MySQL不要将结果放入缓存中。第二个查询使用缓存,并告诉MySQL不要缓存该查询的结果,该查询无效。
tl; dr - 撤消您的查询。
答案 1 :(得分:13)
答案“如何获得相同的SQL运行时?”是 - 你不能。 如果您的查询读取某些行,则它们将被缓存,具体取决于所使用的存储引擎,这些行位于OS缓存(myisam)或缓冲池(innodb)中。如果缓存行,则第二次运行相同的查询要快得多,因为MySQL不必从磁盘读取。
答案 2 :(得分:7)
我的印象是,包含在当前运行时计算的任何类型的SQL函数都不会缓存。您是否尝试过以下类似的事情?
select count(*), now() from `deal_expired` where `site`=8&&`area`=122 && endtime<1310444996056;
答案 3 :(得分:0)
RESET QUERY CACHE
(您需要RELOAD权限)虽然刚刚阅读了上面的链接,但这可能也不起作用:(