mysql的Late row lookups(延迟row查找)

 Sql代码 复制代码 收藏代码

  1. CREATE TABLE `20130122handler` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `uid` int(11) NOT NULL,
  4. `content` varchar(50) NOT NULL,
  5. PRIMARY KEY (`id`),
  6. KEY `20130122handler_idx_uid` (`uid`)
  7. ) ENGINE=InnoDB
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排序分页的情况
要看第七页的内容,用
Sql代码 复制代码 收藏代码
  1. select SQL_NO_CACHE * from 20130122handler
  2. order by uid LIMIT 120,20
select SQL_NO_CACHE * from 20130122handler
order by uid LIMIT 120,20

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

查找20条数据,发现各种性能相当差
似乎mysql在这种情况下,要从20130122handler_idx_uid索引中读取2040条secondary记录,
然后执行2040次的主键查询,然后返回20条,所以浪费了2020次主键查询
可以考虑用这种手段,减少无用的row lookup
Sql代码 复制代码 收藏代码
  1. select SQL_NO_CACHE m.* from(
  2. select uid from 20130122handler
  3. ORDER BY uid LIMIT 2020,20) t,20130122handler m
  4. where t.uid=m.uid
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
用了
Sql代码 复制代码 收藏代码
  1. select SQL_NO_CACHE * from 20130122handler
  2. order by id LIMIT 120,20
  3. select SQL_NO_CACHE * from 20130122handler
  4. ORDER BY id LIMIT 2040,20
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

你可能感兴趣的:(mysql,的)