mysql limit 优化
limit m,n:当偏移量很大时,可以使用索引或者子查询优化,提高查询效率
查询测试语句:select age,distance from test3 where age>24 and distance<800 order by age,distance limit 200000,2;
*************************
索引优化
测试表
test2:id、name、age、distance,数据量1000万,建有索引age_distance(age,distance)
test4:id、name、age、distance,与test2数据相同,无索引
test2建有索引age_name(age、distance):查询耗时0.05秒,
test4无索引查询:耗时2.73秒,可以看出,使用覆盖索引提高了查询效率
test2上查询非索引列,耗时约2.04秒,远大于0.05秒;应使用覆盖索引查询
*************************
子查询优化
查询非索引列时,可以用子查询进行优化
*************************
order by id
如果根据id列排序,则会使用主键进行检索
test2、test4使用的时间相同