Mysql换种写法速度提高10倍

t_house 表有 4万多条数据,现在仅仅只要查询10条
原先的sql语句是:

 select h.*, u.* from t_house  h
    left join t_user u
    on u.id = h.user_id
    order by h.create_time desc 
    limit 0,10

时间是: 9.958s ,因为它要去 遍历t_house表,虽然只查询10条数据,但却遍历了4万条

改进后的 sql语句:

 select h.*,u.* from 
    (
      select * from t_house 
      order by h.create_time desc 
      limit 0,10
    ) h
    left join t_user u 
    on u.id = h.user_id

时间是: 0.077s,这种写法的关键在于: 先 查询 t_house的10条数据,然后再去匹配 t_user 中的记录。

猜你喜欢

转载自blog.csdn.net/why_su/article/details/83012412