count()
select count(*)堤尾、select count(1)效率
select count(*) 于 select count(1)在不加條件的情況下效率相同鲫懒,同時使用了索引。
select count(*)與select count(1)在增加條件(where)的情況下使用了全表掃表。
select count() 與select count()在為條件建立索引的情況加,使用索引,效率提高萧落。
總結(jié):在不使用where的情況下默認使用主鍵索引,如使用where洗贰,則需合理創(chuàng)建索引提高效率铐尚。如果是select count(col),則可在col添加索引提高效率哆姻。
limit
- select * from student limit 10, 10;
- select * from user limit 10000,10;
執(zhí)行發(fā)現(xiàn)2的效率不如1宣增,得出結(jié)論:當數(shù)據(jù)量偏移量較小的時候可以直接使用limit,當數(shù)據(jù)量偏移量較大的時候矛缨。
優(yōu)化
簡單的使用limit不會使用索引
使用order by id優(yōu)化
添加where定位offset
select * from student where id > 3 limit 4