En teoría, utilizarán los mismos planes y funcionarán casi al mismo tiempo.
En la práctica,
SELECT TOP 1 Id FROM Table1 ORDER BY Id DESC
más probable que se utilice un PRIMARY KEY INDEX
.
Además, este es más extensible si decide seleccionar alguna columna junto con id
.
un plan real de MAX()
dice:
SELECT <- AGGREGATE <- TOP <- CLUSTERED INDEX SCAN
, mientras que el plan de TOP 1
dice:
SELECT <- TOP <- CLUSTERED INDEX SCAN
, i. mi. aggregate
se omite.
El agregado en realidad no hará nada aquí, ya que solo hay una fila.
PS Como @Mehrdad Afshari
@John Sansom
y señaló, en un campo no indexado MAX
es ligeramente más rápido (por supuesto, no 20
veces como dice optimizador):
-- 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.
De las respuestas aquí, con el índice no hay diferencia en el rendimiento. Sin embargo, he encontrado que 'MAX' /' MIN' tiene un par de ventajas. En primer lugar, como se indica a continuación, sin un índice 'MAX' será más eficaz, esto también se vuelve significativo cuando tiene uniones más complejas en su lugar. En segundo lugar, si hay valores NULL, 'SELECT MIN' devuelve un valor, pero' SELECT TOP 1 ... ASC' devuelve 'NULL' – icc97