SELECT * FROM table1 LIMIT offset, rows
1.简单查询LIMIT优化
假设表message表中有10万行记录,每次取1000条。
优化前:
SELECT message.* FROM message LIMIT 0,1000
SELECT message.* FROM message LIMIT 1000,1000
SELECT message.* FROM message LIMIT 2000,1000
……
SELECT message.* FROM message LIMIT 998000,1000
SELECT message.* FROM message LIMIT 999000,1000
优化后:
SELECT message.* FROM message WHERE uid>0 LIMIT 1000
SELECT message.* FROM message WHERE uid>1000 LIMIT 1000
SELECT message.* FROM message WHERE uid>2000 LIMIT 1000
……
SELECT message.* FROM message WHERE uid>998000 LIMIT 1000
SELECT message.* FROM message WHERE uid>999000 LIMIT 1000
例如,对于下述SQL语句
SELECT timerec FROM message WHERE evttype = 1 AND nodename = ‘node1‘ LIMIT 0,1000
……
SELECT timerec FROM message WHERE evttype = 1 AND nodename = ‘node1‘ LIMIT 999000,1000
……
优化方案:建立临时表(含自增主键)存储数十万行的查询结果,之后用第二节的方法分多次访问临时表、获取数据。
原文:https://www.cnblogs.com/xcn123/p/10884033.html