You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
对一个大数据量的表使用limit做分页。越往后分页,LIMIT语句的偏移量就会越大,速度也会明显变慢。如下: SELECT * FROM big_table WHERE name = 123 ORDER BY id LIMIT 10000, 20
此时,我们可以通过子查询的方式来提高分页效率: SELECT * FROM big_table WHERE id >= (SELECT id FROM big_table WHERE name = 123 ORDER BY id LIMIT 10000, 1) LIMIT 10
原因: limit10000,20的意思扫描满足条件的10020行,扔掉前面的10000行,返回最后的20行,问题就在这里。例如 LIMIT 500000 , 30 扫描了50万行,肯定会慢。所以采用上面的方式可以有效解决这个问题。
注:这里的is是已经索引过的。
欢迎各位兄弟姐妹积极留言呀。
The text was updated successfully, but these errors were encountered:
对一个大数据量的表使用limit做分页。越往后分页,LIMIT语句的偏移量就会越大,速度也会明显变慢。如下:
SELECT * FROM big_table WHERE name = 123 ORDER BY id LIMIT 10000, 20
此时,我们可以通过子查询的方式来提高分页效率:
SELECT * FROM big_table WHERE id >= (SELECT id FROM big_table WHERE name = 123 ORDER BY id LIMIT 10000, 1) LIMIT 10
原因: limit10000,20的意思扫描满足条件的10020行,扔掉前面的10000行,返回最后的20行,问题就在这里。例如 LIMIT 500000 , 30 扫描了50万行,肯定会慢。所以采用上面的方式可以有效解决这个问题。
注:这里的is是已经索引过的。
欢迎各位兄弟姐妹积极留言呀。
The text was updated successfully, but these errors were encountered: