mysql小技巧

1.在知道查询结果只有一条的情况下在后面加上 limit 1 会大幅提升速度;

例如:test表1000w条数据

select * from test where name = 'Carl1' 执行时间为10.510s;

select * from test where name = 'Carl1' limit 1 执行时间为0.008s;

因为加了limit 1后找到结果就立刻返回了,而第一条sql会扫描完再返回结果,因此如果知道了结果返回的条数n,或者只需要一定的条数n,在后面加上limit n或者limit 小于n的数都能大幅提高速度;

例如select * from test where name ='Carl1' or name = 'Carl2' limit 2也能提高速度;

posted on 2019-05-29 15:11  风再起时9302  阅读(91)  评论(0)    收藏  举报

导航