SQLite應用之路---SQL查詢優化
temp6: 969596條數據
//注意時間單位ms和s
1.in和 exists
//外表大於子表的時候,使用in//外表小於字表的時候,使用exists
select * from temp1 where eid in (select eid from temp6)//1.92s
select * from temp1 where exists (select eid from temp6 where eid = temp1.eid)//66.48ms
select * from temp6 where eid in (select eid from temp1)//98.25ms
select * from temp6 where exists (select eid from temp1 where eid = temp6.eid)//2.40s
2.limit
//offset 越大,執行時間越長。select * from temp6 limit 900000,100//1.25s
select * from temp6 limit 1,100//7.12ms
//下面兩句並沒有網上說的效果
select * from temp6 where eid >= (select eid from temp6 order by eid limit 800000,1) limit 100//515.14ms
select * from temp6 limit 800000,100//474.13ms
3.union 和 union all
//在不考慮重複數據的時候,union all 比union效率高。//union all 兩張表的時候,應該將大表放在左邊
select * from temp6 union select * from temp1//187.17s
select * from temp1 union select * from temp6//190.30s
select * from temp6 union all select * from temp1//8.39s
select * from temp1 union all select * from temp6//42.03s
4.join
//join兩張表的時候,應該把小表放在左邊select * from temp1 a join temp6 b on a.eid = b.eid//54.21ms
select * from temp6 a join temp1 b on a.eid = b.eid//1.12s