从如下一条SQL语句开始聊聊。
SELECT * FROM table where condition1 = 0 and condition2 = 0 and condition3 = -1 and condition4 = -1 order by id asc LIMIT 1000000,10
问:遇到这种百万、千万级表的深度分页mysql如何工作?如何优化?
答:上面这条SQL,mysql无法直接跳过offset行,而是获取offset+n行,然后再放弃offset行。因此MySQL 查询分页 OFFSET 越深入,性能越差。
问:应该如何优化?
答:三种策略:
where id >= (select id from table where condition1=..... )
innert join (select id from table where condition1=.......)
记录上次查询的位置,where id > (上次的位置) limit 10
核心思想就是让 MySQL 尽可能扫描更少的页面,减少IO次数
留言与评论(共有 0 条评论) “” |