MySQL 怎麼用in,or,union all

首先測試 返回結果集只有一條的情況

測試表為用戶登錄日誌logs,value1是我們要進行測試的查詢欄位條件

表數據的總條數

關閉緩存之後開始查詢每個查詢執行5次

select * from logs where value1 in(3895,15869)

select * from logs where value1=3895 or value1=15869

select * from logs where value1=15869 union all select * from logs where value1=3895

然後show profiles看執行時間

然後計算了一下5次平均用時

用in的平均用時 0.00026185

Advertisements

用or的平均用時 0.00024945

用union all 的平均用時 0.0004072

總結 :從用時間看,如果每個條件返回的結果為一條的話,用union all 用時是最長的,用or用時最短。


歡迎添加 微信號 『xha911』 共同探討

Advertisements

你可能會喜歡