More on TOP

Published Mar 23 2019 05:07 AM 151 Views
Microsoft
First published on MSDN on Aug 01, 2007

Last week I wrote about a special case of the TOP operator known as ROWCOUNT TOP .  This week I'll take a look at some other interesting TOP scenarios.  In general, TOP is a fairly mundane operator.  It simply counts and returns the specified number of rows.  SQL Server 2005 does include two enhancements to TOP that were not present in SQL Server 2000.  First, in SQL Server 2000, we can only specify an integer constant for the number of rows to return.  In SQL Server 2005, we can specify an arbitrary expression, including an expression containing T-SQL variables or parameters.  Second, SQL Server 2000 only permits TOP in a SELECT statement (though it does support ROWCOUNT TOP in INSERT, UPDATE, and DELETE statements).  SQL Server 2005 permits TOP with SELECT, INSERT, UPDATE, and DELETE statements.


In this post, I'm going to focus only on some simple examples involving SELECT statements.  Let's create a small table to get started:


CREATE TABLE T (A INT, B INT)
CREATE CLUSTERED INDEX TA ON T(A)


SET NOCOUNT ON
DECLARE @i INT
SET @i = 0
WHILE @i < 100
BEGIN
INSERT T VALUES (@i, @i)
SET @i = @i + 1
END
SET NOCOUNT OFF


The plan for the simplest possible TOP query should not need any explanation:


SELECT TOP 5 * FROM T


Rows   Executes
5      1        |--Top(TOP EXPRESSION:((5)))
5      1             |--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]))


TOP is often used in conjunction with ORDER BY.  Combining TOP with ORDER BY adds determinism to the set of rows returned.  Without the ORDER BY, the set of rows returned depends on the query plan and may even vary from execution to execution.  If we have an index to provide order, we continue to get a simple query plan (notice the ORDERED FORWARD keywords):


SELECT TOP 5 * FROM T ORDER BY A


Rows   Executes
5      1        |--Top(TOP EXPRESSION:((5)))
5      1             |--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]), ORDERED FORWARD)


If we do not have an index to provide order, SQL Server must sort the data:


SELECT TOP 5 * FROM T ORDER BY B


Rows   Executes
5      1        |--Sort(TOP 5, ORDER BY:([tempdb].[dbo].[T].[B] ASC))
100    1             |--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]))


Note that without an index to find the top 5 rows, SQL Server must scan all 100 rows in the input table.  Also note that the sort is actually a "top sort."  A top sort generally uses less memory than a regular sort as it only needs to identify and sort the top few rows rather than sorting the entire input.


Now let's consider what happens if we request the top 5% of rows.  To figure out the actual number of rows to return, SQL Server must count all of the rows and calculate 5%.  This makes queries that use TOP PERCENT less efficient than queries that use TOP with an absolute row count.


SELECT TOP 5 PERCENT * FROM T


Rows   Executes
5      1        |--Top(TOP EXPRESSION:((5.000000000000000e+000)) PERCENT)
5      1             |--Table Spool
100    1                 |--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]))


Like the prior example, SQL Server must scan all 100 rows in the input table.  In this example, SQL Server uses an eager spool which reads and counts all of the input rows before returning any rows.  The top then requests the row count from the spool, calculates 5%, and proceeds like any other top.


If SQL Server must sort, the sort can also provide the count of input rows.  However, only a regular sort can provide this count.  A top sort must know the number of rows to return from the start.


SELECT TOP 5 PERCENT * FROM T ORDER BY B


Rows   Executes
5      1        |--Top(TOP EXPRESSION:((5.000000000000000e+000)) PERCENT)
5      1             |--Sort(ORDER BY:([tempdb].[dbo].[T].[B] ASC))
100    1                  |--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]))


TOP WITH TIES is also incompatible with top sort.  TOP WITH TIES cannot know for certain how many rows will be returned until the number of ties is determined.  For this example, let's add a tie for the fifth row returned:


INSERT T VALUES (4, 4)


SELECT TOP 5 WITH TIES * FROM T ORDER BY B


Rows   Executes
6      1        |--Top(TOP EXPRESSION:((5)))
7      1             |--Sort(ORDER BY:([tempdb].[dbo].[T].[B] ASC))
101    1                  |--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]))


Although the above plan does not seem to reflect that we have a TOP WITH TIES, the argument column of SHOWPLAN_ALL or STATISTICS PROFILE includes this information:  "TIE COLUMNS:([T].[B])".  This information is also available in the graphical and XML plans on SQL Server 2005.  Note that the TOP actually returns one extra row.  When a TOP N WITH TIES reaches the Nth row, it keeps a copy of the tie columns for this row (in this example B==4) and compares each subsequent row to that row.  As long as there are rows that match, it keeps returning them.  Because the top must compare subsequent rows until it finds a non-match to the Nth row, the top retrieves one more row from the sort than it returns.


Finally, there are a couple of special cases.  The optimizer knows that TOP 0 and TOP 0 PERCENT never return any rows and replaces any query plan with a TOP 0 with a constant scan:


SELECT TOP 0 * FROM T


|--Constant Scan


The optimizer also knows that TOP 100 PERCENT always returns all rows and removes the top operator from the query plan:


SELECT TOP 100 PERCENT * FROM T


|--Clustered Index Scan(OBJECT:([tempdb].[dbo].[T].[TA]))


Both of these optimizations require that the number of rows is a constant.  For example, using an expression that includes a T-SQL variable or parameter will prevent the optimization.  Both optimizations also work with INSERT, UPDATE, and DELETE statements.


Note that using TOP to work around SQL language restrictions on the use of ORDER BY in sub-selects or in views or to force specific plan evaluation orders is not recommended.

%3CLINGO-SUB%20id%3D%22lingo-sub-383314%22%20slang%3D%22en-US%22%3EMore%20on%20TOP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-383314%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Aug%2001%2C%202007%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3ELast%20week%20I%20wrote%20about%20a%20special%20case%20of%20the%20TOP%20operator%20known%20as%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fcraigfr%2Farchive%2F2007%2F07%2F25%2Frowcount-top.aspx%22%20title%3D%22ROWCOUNT%20Top%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20ROWCOUNT%20TOP%20%3C%2FA%3E%20.%26nbsp%3B%20This%20week%20I'll%20take%20a%20look%20at%20some%20other%20interesting%20TOP%20scenarios.%26nbsp%3B%20In%20general%2C%20TOP%20is%20a%20fairly%20mundane%20operator.%26nbsp%3B%20It%20simply%20counts%20and%20returns%20the%20specified%20number%20of%20rows.%26nbsp%3B%20SQL%20Server%202005%20does%20include%20two%20enhancements%20to%20TOP%20that%20were%20not%20present%20in%20SQL%20Server%202000.%26nbsp%3B%20First%2C%20in%20SQL%20Server%202000%2C%20we%20can%20only%20specify%20an%20integer%20constant%20for%20the%20number%20of%20rows%20to%20return.%26nbsp%3B%20In%20SQL%20Server%202005%2C%20we%20can%20specify%20an%20arbitrary%20expression%2C%20including%20an%20expression%20containing%20T-SQL%20variables%20or%20parameters.%26nbsp%3B%20Second%2C%20SQL%20Server%202000%20only%20permits%20TOP%20in%20a%20SELECT%20statement%20(though%20it%20does%20support%20ROWCOUNT%20TOP%20in%20INSERT%2C%20UPDATE%2C%20and%20DELETE%20statements).%26nbsp%3B%20SQL%20Server%202005%20permits%20TOP%20with%20SELECT%2C%20INSERT%2C%20UPDATE%2C%20and%20DELETE%20statements.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIn%20this%20post%2C%20I'm%20going%20to%20focus%20only%20on%20some%20simple%20examples%20involving%20SELECT%20statements.%26nbsp%3B%20Let's%20create%20a%20small%20table%20to%20get%20started%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ECREATE%20TABLE%20T%20(A%20INT%2C%20B%20INT)%20%3CBR%20%2F%3E%20CREATE%20CLUSTERED%20INDEX%20TA%20ON%20T(A)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESET%20NOCOUNT%20ON%20%3CBR%20%2F%3E%20DECLARE%20%40i%20INT%20%3CBR%20%2F%3E%20SET%20%40i%20%3D%200%20%3CBR%20%2F%3E%20WHILE%20%40i%20%26lt%3B%20100%20%3CBR%20%2F%3E%20BEGIN%20%3CBR%20%2F%3E%20INSERT%20T%20VALUES%20(%40i%2C%20%40i)%20%3CBR%20%2F%3E%20SET%20%40i%20%3D%20%40i%20%2B%201%20%3CBR%20%2F%3E%20END%20%3CBR%20%2F%3E%20SET%20NOCOUNT%20OFF%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20plan%20for%20the%20simplest%20possible%20TOP%20query%20should%20not%20need%20any%20explanation%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%205%20*%20FROM%20T%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ERows%26nbsp%3B%26nbsp%3B%20Executes%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%7C--Top(TOP%20EXPRESSION%3A((5)))%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ETOP%20is%20often%20used%20in%20conjunction%20with%20ORDER%20BY.%26nbsp%3B%20Combining%20TOP%20with%20ORDER%20BY%20adds%20determinism%20to%20the%20set%20of%20rows%20returned.%26nbsp%3B%20Without%20the%20ORDER%20BY%2C%20the%20set%20of%20rows%20returned%20depends%20on%20the%20query%20plan%20and%20may%20even%20vary%20from%20execution%20to%20execution.%26nbsp%3B%20If%20we%20have%20an%20index%20to%20provide%20order%2C%20we%20continue%20to%20get%20a%20simple%20query%20plan%20(notice%20the%20ORDERED%20FORWARD%20keywords)%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%205%20*%20FROM%20T%20ORDER%20BY%20A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ERows%26nbsp%3B%26nbsp%3B%20Executes%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%7C--Top(TOP%20EXPRESSION%3A((5)))%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D)%2C%20ORDERED%20FORWARD)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIf%20we%20do%20not%20have%20an%20index%20to%20provide%20order%2C%20SQL%20Server%20must%20sort%20the%20data%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%205%20*%20FROM%20T%20ORDER%20BY%20B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ERows%26nbsp%3B%26nbsp%3B%20Executes%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%7C--Sort(TOP%205%2C%20ORDER%20BY%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BB%5D%20ASC))%20%3CBR%20%2F%3E%20100%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENote%20that%20without%20an%20index%20to%20find%20the%20top%205%20rows%2C%20SQL%20Server%20must%20scan%20all%20100%20rows%20in%20the%20input%20table.%26nbsp%3B%20Also%20note%20that%20the%20sort%20is%20actually%20a%20%22top%20sort.%22%26nbsp%3B%20A%20top%20sort%20generally%20uses%20less%20memory%20than%20a%20regular%20sort%20as%20it%20only%20needs%20to%20identify%20and%20sort%20the%20top%20few%20rows%20rather%20than%20sorting%20the%20entire%20input.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENow%20let's%20consider%20what%20happens%20if%20we%20request%20the%20top%205%25%20of%20rows.%26nbsp%3B%20To%20figure%20out%20the%20actual%20number%20of%20rows%20to%20return%2C%20SQL%20Server%20must%20count%20all%20of%20the%20rows%20and%20calculate%205%25.%26nbsp%3B%20This%20makes%20queries%20that%20use%20TOP%20PERCENT%20less%20efficient%20than%20queries%20that%20use%20TOP%20with%20an%20absolute%20row%20count.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%205%20PERCENT%20*%20FROM%20T%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ERows%26nbsp%3B%26nbsp%3B%20Executes%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%7C--Top(TOP%20EXPRESSION%3A((5.000000000000000e%2B000))%20PERCENT)%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Table%20Spool%20%3CBR%20%2F%3E%20100%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ELike%20the%20prior%20example%2C%20SQL%20Server%20must%20scan%20all%20100%20rows%20in%20the%20input%20table.%26nbsp%3B%20In%20this%20example%2C%20SQL%20Server%20uses%20an%20eager%20spool%20which%20reads%20and%20counts%20all%20of%20the%20input%20rows%20before%20returning%20any%20rows.%26nbsp%3B%20The%20top%20then%20requests%20the%20row%20count%20from%20the%20spool%2C%20calculates%205%25%2C%20and%20proceeds%20like%20any%20other%20top.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EIf%20SQL%20Server%20must%20sort%2C%20the%20sort%20can%20also%20provide%20the%20count%20of%20input%20rows.%26nbsp%3B%20However%2C%20only%20a%20regular%20sort%20can%20provide%20this%20count.%26nbsp%3B%20A%20top%20sort%20must%20know%20the%20number%20of%20rows%20to%20return%20from%20the%20start.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%205%20PERCENT%20*%20FROM%20T%20ORDER%20BY%20B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ERows%26nbsp%3B%26nbsp%3B%20Executes%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%7C--Top(TOP%20EXPRESSION%3A((5.000000000000000e%2B000))%20PERCENT)%20%3CBR%20%2F%3E%205%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BB%5D%20ASC))%20%3CBR%20%2F%3E%20100%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ETOP%20WITH%20TIES%20is%20also%20incompatible%20with%20top%20sort.%26nbsp%3B%20TOP%20WITH%20TIES%20cannot%20know%20for%20certain%20how%20many%20rows%20will%20be%20returned%20until%20the%20number%20of%20ties%20is%20determined.%26nbsp%3B%20For%20this%20example%2C%20let's%20add%20a%20tie%20for%20the%20fifth%20row%20returned%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EINSERT%20T%20VALUES%20(4%2C%204)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%205%20WITH%20TIES%20*%20FROM%20T%20ORDER%20BY%20B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ERows%26nbsp%3B%26nbsp%3B%20Executes%20%3CBR%20%2F%3E%206%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%7C--Top(TOP%20EXPRESSION%3A((5)))%20%3CBR%20%2F%3E%207%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Sort(ORDER%20BY%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BB%5D%20ASC))%20%3CBR%20%2F%3E%20101%26nbsp%3B%26nbsp%3B%26nbsp%3B%201%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EAlthough%20the%20above%20plan%20does%20not%20seem%20to%20reflect%20that%20we%20have%20a%20TOP%20WITH%20TIES%2C%20the%20argument%20column%20of%20SHOWPLAN_ALL%20or%20STATISTICS%20PROFILE%20includes%20this%20information%3A%20%26nbsp%3B%22TIE%20COLUMNS%3A(%5BT%5D.%5BB%5D)%22.%26nbsp%3B%20This%20information%20is%20also%20available%20in%20the%20graphical%20and%20XML%20plans%20on%20SQL%20Server%202005.%26nbsp%3B%20Note%20that%20the%20TOP%20actually%20returns%20one%20extra%20row.%26nbsp%3B%20When%20a%20TOP%20N%20WITH%20TIES%20reaches%20the%20Nth%20row%2C%20it%20keeps%20a%20copy%20of%20the%20tie%20columns%20for%20this%20row%20(in%20this%20example%20B%3D%3D4)%20and%20compares%20each%20subsequent%20row%20to%20that%20row.%26nbsp%3B%20As%20long%20as%20there%20are%20rows%20that%20match%2C%20it%20keeps%20returning%20them.%26nbsp%3B%20Because%20the%20top%20must%20compare%20subsequent%20rows%20until%20it%20finds%20a%20non-match%20to%20the%20Nth%20row%2C%20the%20top%20retrieves%20one%20more%20row%20from%20the%20sort%20than%20it%20returns.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EFinally%2C%20there%20are%20a%20couple%20of%20special%20cases.%20%26nbsp%3BThe%20optimizer%20knows%20that%20TOP%200%20and%20TOP%200%20PERCENT%20never%20return%20any%20rows%20and%20replaces%20any%20query%20plan%20with%20a%20TOP%200%20with%20a%20constant%20scan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%200%20*%20FROM%20T%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Constant%20Scan%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20optimizer%20also%20knows%20that%20TOP%20100%20PERCENT%20always%20returns%20all%20rows%20and%20removes%20the%20top%20operator%20from%20the%20query%20plan%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ESELECT%20TOP%20100%20PERCENT%20*%20FROM%20T%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7C--Clustered%20Index%20Scan(OBJECT%3A(%5Btempdb%5D.%5Bdbo%5D.%5BT%5D.%5BTA%5D))%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EBoth%20of%20these%20optimizations%20require%20that%20the%20number%20of%20rows%20is%20a%20constant.%26nbsp%3B%20For%20example%2C%20using%20an%20expression%20that%20includes%20a%20T-SQL%20variable%20or%20parameter%20will%20prevent%20the%20optimization.%26nbsp%3B%20Both%20optimizations%20also%20work%20with%20INSERT%2C%20UPDATE%2C%20and%20DELETE%20statements.%3C%2FP%3E%3CBR%20%2F%3E%3CP%3ENote%20that%20using%20TOP%20%3CA%20href%3D%22http%3A%2F%2Fblogs.msdn.com%2Fqueryoptteam%2Farchive%2F2006%2F03%2F24%2F560396.aspx%22%20title%3D%22TOP%20100%20Percent%20ORDER%20BY%20Considered%20Harmful.%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20to%20work%20around%20SQL%20language%20restrictions%20on%20the%20use%20of%20ORDER%20BY%20in%20sub-selects%20or%20in%20views%20%3C%2FA%3E%20or%20%3CA%20href%3D%22http%3A%2F%2Fsqlblog.com%2Fblogs%2Fadam_machanic%2Farchive%2F2006%2F10%2F03%2F267.aspx%22%20title%3D%22Exploring%20the%20secrets%20of%20intermediate%20materialization%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20to%20force%20specific%20plan%20evaluation%20orders%20%3C%2FA%3E%20is%20not%20recommended.%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-383314%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Aug%2001%2C%202007%20Last%20week%20I%20wrote%20about%20a%20special%20case%20of%20the%20TOP%20operator%20known%20as%20ROWCOUNT%20TOP.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-383314%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerQueryProcessing%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 05:07 AM
Updated by: