当我第一次运行这个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)我尝试过多种方法,这里
甚至重新启动mysql服务器或更改表名,但我仍然不能让这个SQL运行39秒。
我替换了另一个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)发布于 2011-07-12 15:27:08
“如何获得相同的SQL运行时?”是-你不能。如果查询读取某些行,则它们将被缓存,取决于正在使用的存储引擎,这些行要么在OS缓存(myisam)中,要么在缓冲池(innodb)中。如果缓存了行,那么第二次运行相同的查询要快得多,因为MySQL不必从磁盘读取。
发布于 2013-01-18 00:08:55
第一个查询应该使用SQL_NO_CACHE告诉MySQL不要将结果放入缓存中。第二个查询使用缓存,并告诉MySQL不缓存该查询的结果,而该查询不执行任何操作。
反转你的查询。
发布于 2011-12-29 15:31:51
我的印象是,包含在当前运行时计算的任何类型的SQL函数都不会缓存。你试过做下面这样的事情吗?
select count(*), now() from `deal_expired` where `site`=8&&`area`=122 && endtime<1310444996056;https://stackoverflow.com/questions/6666631
复制相似问题