日期:2014-05-16  浏览次数:20763 次

mysql的Late row lookups(延迟row查找)
CREATE TABLE `20130122handler` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `content` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `20130122handler_idx_uid` (`uid`)
) ENGINE=InnoDB

里面有60w数据,现在模拟按uid排序分页的情况
要看第七页的内容,用
select SQL_NO_CACHE * from 20130122handler
order by uid LIMIT 120,20

查找20条数据,基本就是瞬间的事情
假设用户比较变态,直接点到了102页,用
select SQL_NO_CACHE * from 20130122handler
order by uid LIMIT 2020,20

查找20条数据,发现各种性能相当差
似乎mysql在这种情况下,要从20130122handler_idx_uid索引中读取2040条secondary记录,
然后执行2040次的主键查询,然后返回20条,所以浪费了2020次主键查询
可以考虑用这种手段,减少无用的row lookup
select SQL_NO_CACHE m.* from(
 select uid from 20130122handler
 ORDER BY uid LIMIT 2020,20) t,20130122handler m
where t.uid=m.uid

这是因为20130122handler_idx_uid是secondary索引,所以要row lookup
用了
select SQL_NO_CACHE * from 20130122handler
order by id LIMIT 120,20
select SQL_NO_CACHE * from 20130122handler
ORDER BY id LIMIT 2040,20

差别就不明显了
这篇文章写得不错
http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/
mysql网站上也有讨论过这个问题
http://lists.mysql.com/mysql/227244
1 楼 boygirl 2013-01-30  
难啊