In teoria, useranno gli stessi piani e correranno quasi contemporaneamente.
In pratica,
SELECT TOP 1 Id FROM Table1 ORDER BY Id DESC
sarà più probabilmente usare un PRIMARY KEY INDEX
.
Inoltre, questo è più estendibile se si decide di selezionare un'altra colonna insieme a id
.
Un vero e proprio piano sulla MAX()
dice:
SELECT <- AGGREGATE <- TOP <- CLUSTERED INDEX SCAN
, mentre piano per TOP 1
dice:
SELECT <- TOP <- CLUSTERED INDEX SCAN
, i. e. aggregate
è stato omesso.
Aggregato in realtà non farà nulla qui, in quanto vi è una sola riga.
PS Come @Mehrdad Afshari
e @John Sansom
notato, su un campo non indicizzato MAX
è leggermente più veloce (naturalmente, non 20
volte ottimizzatore dice):
-- 18,874,368 rows
SET LANGUAGE ENGLISH
SET STATISTICS TIME ON
SET STATISTICS IO ON
PRINT 'MAX'
SELECT MAX(id) FROM master
PRINT 'TOP 1'
SELECT TOP 1 id FROM master ORDER BY id DESC
PRINT 'MAX'
SELECT MAX(id) FROM master
PRINT 'TOP 1'
SELECT TOP 1 id FROM master ORDER BY id DESC
PRINT 'MAX'
SELECT MAX(id) FROM master
PRINT 'TOP 1'
SELECT TOP 1 id FROM master ORDER BY id DESC
PRINT 'MAX'
SELECT MAX(id) FROM master
PRINT 'TOP 1'
SELECT TOP 1 id FROM master ORDER BY id DESC
PRINT 'MAX'
SELECT MAX(id) FROM master
PRINT 'TOP 1'
SELECT TOP 1 id FROM master ORDER BY id DESC
Changed language setting to us_english.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
MAX
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 20 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 447, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5452 ms, elapsed time = 2766 ms.
TOP 1
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 2, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 6813 ms, elapsed time = 3449 ms.
MAX
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 44, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5359 ms, elapsed time = 2714 ms.
TOP 1
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 6766 ms, elapsed time = 3379 ms.
MAX
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5406 ms, elapsed time = 2726 ms.
TOP 1
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 6780 ms, elapsed time = 3415 ms.
MAX
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 85, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5392 ms, elapsed time = 2709 ms.
TOP 1
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 10, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 6766 ms, elapsed time = 3387 ms.
MAX
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 5374 ms, elapsed time = 2708 ms.
TOP 1
SQL Server Execution Times:
CPU time = 0 ms, elapsed time = 1 ms.
(строк обработано: 1)
Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
SQL Server Execution Times:
CPU time = 6797 ms, elapsed time = 3494 ms.
Dalle risposte qui, con la indice non c'è differenza nelle prestazioni. Comunque ho trovato 'MAX' /' MIN' ha un paio di vantaggi. Innanzitutto, come indicato di seguito senza un indice "MAX", sarà più performante, anche questo diventa significativo quando si hanno giunzioni più complesse. In secondo luogo se ci sono valori NULL allora 'SELECT MIN' restituisce un valore ma' SELECT TOP 1 ... ASC' restituisce 'NULL' – icc97