2017-10-09 62 views
0

爲什麼在這兩個查詢之間存在時間執行差異,即使它們從同一個表中檢索到相同數量的行?mysql查詢,=和IN之間的性能不同

select cognome, nome, lingua, count(*) 
from archivio.utente 
where cognome in ('rossi','pecchia','pirono') 
group by cognome, nome, lingua; 

... ...

… 
| Rossi    | Mario | it  |  1 | 
| Pironi    | Luigi | it  |  1 | 
| Pecchia    | Fabio | it  |  1 | 
+----------------------+---------+--------+----------+ 

779行中集合(0.03秒)

select cognome, nome, lingua, count(*) 
from archivio.utente 
where nome='corrado' 
group by cognome, nome, lingua; 

... ... ...

| Rossi    | Mario | it  |  1 | 
| Pironi    | Luigi | it  |  1 | 
| Pecchia    | Fabio | it  |  1 | 
+----------------------+---------+--------+----------+ 

7 37行中的組(0.47秒)

+2

也許第二個在冷藏緩存上運行(即它首先運行)。爲了比較時間,你需要多次運行查詢,並且要小心數據是否已經被讀入內存。 –

+2

您正在測試兩個不同的列。也許'cognome'被索引,'name'不是? – tadman

+1

他們不「檢索相同數量的行」; 779 vs 737. – HoneyBadger

回答

0

從MySQL文檔:

https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain-join-types

當我們在

只有在給定範圍中檢索到的行使用,使用索引以選擇行。 輸出行中的關鍵列指示使用哪個索引。

當我們使用

=

完整表掃描的行

因此,在一種情況下的所有行被檢索和比較的每個組合完成的,在另一種情況下只是一個範圍。

+1

你應該編輯問題,而不是發佈答案。 – NikNik