Have you ever come across a situation where a SELECT query with a TOP clause will perform well most of the time, but as soon as you change the TOP value the same query is 10 to 20 times slower? In this tip I will show you why this may happen and ways to prevent this.
The TOP clause specifies that only the first set of rows will be returned from the query result. In this tip I will show how to make the query results predictable by avoiding “Sort Warning”.
--Source code provided by: www.sqlworkshops.comSET NOCOUNT ON
CREATE TABLE tab7 (c1 INT PRIMARY KEY CLUSTERED, c2 INT, c3 CHAR(2000))GOBEGIN TRANGODECLARE @i INT
SET @i=1WHILE @i<=50000BEGIN
INSERT INTO tab7 VALUES (@i,RAND()*200000,'a')SET @i=@i+1END
COMMIT TRANGO
UPDATE STATISTICS tab7 WITH fullscan
GO
SET STATISTICS time ONGO--Source code provided by: www.sqlworkshops.comSELECT num_of_reads, num_of_bytes_read,num_of_writes, num_of_bytes_writtenFROM sys.dm_io_virtual_file_stats(DB_ID('tempdb'), 1) GOSELECT TOP 100 c1, c2,c3FROM tab7WHERE c1<30000ORDER BY c2
GOSELECT num_of_reads, num_of_bytes_read,num_of_writes, num_of_bytes_writtenFROM sys.dm_io_virtual_file_stats(DB_ID('tempdb'), 1)GO
CPU time = 124 ms, elapsed time = 91 ms Before Query execution num_of_reads num_of_bytes_read num_of_writes num_of_bytes_written -------------------- -------------------- -------------------- -------------------- 725864 46824931328 793589 51814416384 After Query execution num_of_reads num_of_bytes_read num_of_writes num_of_bytes_written -------------------- -------------------- -------------------- -------------------- 725864 46824931328 793589 51814416384
SELECT num_of_reads, num_of_bytes_read,num_of_writes, num_of_bytes_writtenFROM sys.dm_io_virtual_file_stats(DB_ID('tempdb'), 1) GOSELECT TOP 101 c1, c2,c3FROM tab7WHERE c1<30000ORDER BY c2
GOSELECT num_of_reads, num_of_bytes_read,num_of_writes, num_of_bytes_writtenFROM sys.dm_io_virtual_file_stats(DB_ID('tempdb'), 1)GO
CPU time = 376 ms, elapsed time = 726 ms Before Query execution num_of_reads num_of_bytes_read num_of_writes num_of_bytes_written -------------------- -------------------- -------------------- -------------------- 725864 46824931328 793589 51814416384 After Query execution num_of_reads num_of_bytes_read num_of_writes num_of_bytes_written -------------------- -------------------- -------------------- -------------------- 726880 46881570816 795356 51928072192
SELECT TOP 101 c1, c2,CONVERT(VARCHAR(4500),c3)FROM tab7WHERE c1<30000ORDER BY c2
SELECT num_of_reads, num_of_bytes_read, num_of_writes, num_of_bytes_writtenFROM sys.dm_io_virtual_file_stats(DB_ID('tempdb'), 1)GODECLARE @i INT
SET @i=101SELECT TOP(@i) c1, c2,CONVERT(VARCHAR(5000),c3)FROM tab7WHERE c1<30000ORDER BY c2
GOSELECT num_of_reads, num_of_bytes_read,num_of_writes, num_of_bytes_written
FROM sys.dm_io_virtual_file_stats(DB_ID('tempdb'), 1)GO
CPU time = 267 ms, elapsed time = 124 ms Before Query execution num_of_reads num_of_bytes_read num_of_writes num_of_bytes_written -------------------- -------------------- -------------------- -------------------- 729075 47004737536 799045 52165402624 After Query execution num_of_reads num_of_bytes_read num_of_writes num_of_bytes_written -------------------- -------------------- -------------------- -------------------- 729075 47004737536 799045 52165402624
No comments:
Post a Comment