所谓延迟关联,就是本来通过一个比较耗时的条件搜索到全部数据的sql,分解为先通过该条件搜索到对应id,再用id内联原表取得所有字段值
例1:使用延迟关联方式,比直接搜索快一倍多
select * from house_rent where c_datetime>'2013-06-08' and title like "%宣庆街%"
(共 25 行, 查询花费 0.1435 秒)
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE house_rent ALL NULL NULL NULL NULL 24534 Using where
select * from house_rent as c join ( select id from house_rent where c_datetime>'2013-06-08' and title like "%宣庆街%" )as b on (b.id=c.id)
(共 25 行, 查询花费 0.0593 秒)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL NULL NULL NULL NULL 25
1 PRIMARY c eq_ref PRIMARY PRIMARY 4 b.id 1
2 DERIVED house_rent ALL NULL NULL NULL NULL 34071 Using where
例2:分页上的应用,一样比直接搜索快1倍
select * from house_rent order by uid,content_code limit 1200,10
(共 10 行, 查询花费 0.0465 秒)
select * from house_rent inner join (select id from house_rent order by uid,content_code limit 1200,10 ) as b using(id)
(共 27485 行, 查询花费 0.0260 秒)