2009-12-20

查询性能的跳跃

今天遇到一个很奇怪的问题,两句差不多的sql,性能却有很大差别。

$ mysql test -e 'flush status;set profiling=1;select t from t where t like "%a%" order by id desc limit 9085,1;show profiles;show status;' > a.txt
$ mysql test -e 'flush status;set profiling=1;select t from t where t like "%a%" order by id desc limit 9086,1;show profiles;show status;' > b.txt
$ diff a.txt b.txt
2c2
< sportsman
---
> sportscasts
4c4
< 1 0.07678300 select t from t where t like "%a%" order by id desc limit 9085,1
---
> 1 0.24102700 select t from t where t like "%a%" order by id desc limit 9086,1
11c11
< Bytes_sent 297
---
> Bytes_sent 299
155c155
< Connections 219
---
> Connections 220
170c170
< Handler_read_prev 18633
---
> Handler_read_prev 18634
224c224
< Key_read_requests 1630
---
> Key_read_requests 2064
247c247
< Queries 1779817
---
> Queries 1779824
296c296
< Uptime 1419010
---
> Uptime 1419015

其中t上有索引(id,t)。limit的偏移量小于9085,或者大于9086时看上去查询时间是和偏移量成正比的。
不知道为什么会有一个跳跃,感觉可能是触发了某个开关,导致两者的查询方式有区别。不过从状态信息上没看出来。

上面用的是5.1.40,换成5.1.41发现limit 9086,1变成0.12秒了,正常了一点,不过从change log上没有看上任何相关的东西。

0 条评论: