mysql分页
详细参考:https://blog.csdn.net/justlpf/article/details/81507586
0、limit:最基本的方式(最差的方式)
SELECT * FROM articles WHERE category_id = 123 ORDER BY id LIMIT 50, 10
1、limit:尽量给出查询的大致范围
SELECT c1,c2,cn... FROM table WHERE id>=20000 LIMIT 10;
2、子查询法
SELECT c1,c2,cn... FROM table WHERE id>= (SELECT id FROM table LIMIT 20000,1) LIMIT 10;
3、JOIN分页
优化前SQL: SELECT c1,c2,cn... FROM member ORDER BY last_active LIMIT 50,5 优化后SQL: SELECT c1, c2, cn .. . FROM member t1 INNER JOIN (SELECT member_id FROM member ORDER BY last_active LIMIT 50, 5) t2
on t1.member_id =t2.member_id
-- USING (member_id)
join分页
-- 1
SELECT * FROM `content` AS t1 JOIN (SELECT id FROM `content` ORDER BY id desc LIMIT ".($page-1)*$pagesize.", 1) AS t2 WHERE t1.id <= t2.id ORDER BY t1.id desc LIMIT $pagesize;
-- 2
SELECT * FROM product a JOIN (select id from product limit 866613, 20) b ON a.ID = b.id
分别在于,优化前的SQL需要更多I/O浪费,因为先读索引,再读数据,然后抛弃无需的行。而优化后的SQL(子查询那条)只读索引(Cover index)就可以了,然后通过member_id读取需要的列。