点击蓝色“架构文摘”关注我哟
加个“星标”,每天上午 09:25,干货推送!

来源:cnblogs.com/geningchao/p/6649907.html
语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 LIMIT M,N 适应场景: 适用于数据量较少的情况(元组百/千级) 原因/缺点: 全表扫描,速度会很慢 且 有的数据库结果集返回不稳定(如某次返回1,2,3,另外的一次返回2,1,3). Limit限制的是从结果集的M位置处取出N条输出,其余抛弃.
语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 WHERE id_pk > (pageNum*10) LIMIT M 适应场景: 适用于数据量多的情况(元组数上万) 原因: 索引扫描,速度会很快. 有朋友提出: 因为数据查询出来并不是按照pk_id排序的,所以会有漏掉数据的情况,只能方法3
语句样式: MySQL中,可用如下方法: SELECT * FROM 表名称 WHERE id_pk > (pageNum*10) ORDER BY id_pk ASC LIMIT M 适应场景: 适用于数据量多的情况(元组数上万). 最好ORDER BY后的列对象是主键或唯一所以,使得ORDERBY操作能利用索引被消除但结果集是稳定的(稳定的含义,参见方法1) 原因: 索引扫描,速度会很快. 但MySQL的排序操作,只有ASC没有DESC(DESC是假的,未来会做真正的DESC,期待...).
语句样式: MySQL中,可用如下方法: PREPARE stmt_name FROM SELECT * FROM 表名称 WHERE id_pk > (?* ?) ORDER BY id_pk ASC LIMIT M 适应场景: 大数据量 原因: 索引扫描,速度会很快. prepare语句又比一般的查询语句快一点。
SELECT * FROM your_table WHERE pk>=1000 ORDER BY pk ASC LIMIT 0,20
SELECT * FROM your_table WHERE id <=
(SELECT id FROM your_table ORDER BY id desc LIMIT ($page-1)*$pagesize ORDER BY id desc
LIMIT $pagesize
SELECT * FROM your_table AS t1
JOIN (SELECT id FROM your_table ORDER BY id desc LIMIT ($page-1)*$pagesize AS t2
WHERE t1.id <= t2.id ORDER BY t1.id desc LIMIT $pagesize;
select * from product limit start, count
select * from product limit 10, 20 0.016秒
select * from product limit 100, 20 0.016秒
select * from product limit 1000, 20 0.047秒
select * from product limit 10000, 20 0.094秒
select * from product limit 400000, 20 3.229秒
select * from product limit 866613, 20 37.44秒
limit语句的查询时间与起始记录的位置成正比 mysql的limit语句是很方便,但是对记录很多的表并不适合直接使用。
select id from product limit 866613, 20 0.2秒
SELECT * FROM product WHERE ID > =(select id from product limit 866613, 1) limit 20
SELECT * FROM product a JOIN (select id from product limit 866613, 20) b ON a.ID = b.id
select id,title from collect limit 1000,10;
select id,title from collect limit 90000,10;
select id from collect order by id limit 90000,10;
select id,title from collect where id>=(select id from collect order by id limit 90000,1) limit 10;
select id from collect where vtype=1 limit 1000,10;
select id from t where vtype=1 order by id limit 90000,10;
select id from t where vtype=1 order by id limit 900000,10;
select id from collect order by id limit 90000,10;
select id from collect where vtype=1 limit 90000,10;
select id ,title from collect where vtype=1 limit 90000,10;
end
往期推荐
如有收获,点个在看,诚挚感谢
文章转载自架构文摘,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。






