I have a question about how the TOP n clause works on the SELECT
statement.
Assume I have a table called TheTable that contains 10,000,000 rows
and no indexes.
Assume I execute the following SQL...
SELECT * TOP 100 FROM TheTable WHERE TheField = "12345"
Will SQL Server first do a TableScan to find ALL the rows that meet
the WHERE clause and THEN give me the TOP 100, or is it clever enough
to stop the TableScan when 100 matching rows have been found?
Thanks for any help.
RichardF> Assume I have a table called TheTable that contains 10,000,000 rows
> and no indexes.
Why would you ever have such a table?
> Will SQL Server first do a TableScan to find ALL the rows that meet
> the WHERE clause and THEN give me the TOP 100, or is it clever enough
> to stop the TableScan when 100 matching rows have been found?
Run your query, turn on execution plan, and check it out...|||Thanks for your 'help'.
It is a hypothetical table.
My question is about how SQL Server acts on the TOP n clause when one
of the fields involved in the WHERE clause is not indexed.
RichardF
On Thu, 10 Mar 2005 12:53:14 -0500, "Aaron [SQL Server MVP]"
<ten.xoc@.dnartreb.noraa> wrote:
>Why would you ever have such a table?
>
>Run your query, turn on execution plan, and check it out...
>|||> Thanks for your 'help'.
I'm trying to teach you how to analyze these things yourself. Teach someone
to fish, rather than just handing them a fish, that kind of thing. Sorry if
that help isn't good enough for you.
If it is, then create a smaller table, select a smaller percentage, and the
direction the engine takes (which you can view in Query Analyzer if you turn
execution plan on) should be roughly equivalent.
Without more information, I think it is impossible for anyone to give you a
black and white answer. There are dozens of variables aside from index
existence that contribute to whether a scan is used and when the engine
knows to stop searching. So you are probably better off testing against the
kind of data you are working with anyway.|||If the query is as simple as this, then SQL-Server will start a table
scan. During the table scan the matching rows will be returned. If a
100th row is found, the table scan will stop.
You can test this by creating this hypothetical table, and filling it
with an ascending value for "TheField". Then you can measure the
performance difference between
SELECT TOP 1 * FROM TheTable WHERE TheField=1 -- Should be fast
and
SELECT TOP 1 * FROM TheTable WHERE TheField=10000000 -- Could be slow
Hope this helps,
Gert-Jan
RichardF wrote:
> I have a question about how the TOP n clause works on the SELECT
> statement.
> Assume I have a table called TheTable that contains 10,000,000 rows
> and no indexes.
> Assume I execute the following SQL...
> SELECT * TOP 100 FROM TheTable WHERE TheField = "12345"
> Will SQL Server first do a TableScan to find ALL the rows that meet
> the WHERE clause and THEN give me the TOP 100, or is it clever enough
> to stop the TableScan when 100 matching rows have been found?
> Thanks for any help.
> RichardF|||Like Gert-Jan and Aaron said: You have to experiment. From what I could
tell, SQL Server stops searching the table when it finds enough rows to
satisfy the TOP X requirement. If you leave off of TOP requirement (or
there aren't enough rows to fulfill the TOP X) then the entire table will be
scanned.
Of course if this was a production table that was being queried quite often,
you could want an index. :)
-- Create temp table
-- =================
CREATE TABLE #test (col int NOT NULL)
GO
-- This will take awhile... (5 million rows in my test database)
-- ========================
INSERT INTO #test
SELECT CHECKSUM(NEWID()) FROM sysobjects S1,sysobjects S2,sysobjects
S3,sysobjects S4,sysobjects S5
GO
-- Start experimenting. Turn on "Show Execution Plan' and "Show Server
Trace"
-- ========================================
==============================
SELECT TOP 0 col FROM #test WHERE col = '779668530'
SELECT TOP 1 col FROM #test WHERE col = '779668530'
SELECT TOP 2 col FROM #test WHERE col = '779668530'
SELECT col FROM #test WHERE col = '779668530'
-- Find out which values of "col" are repeated
-- For use in the previous "experimenting" calls
-- ========================================
=====
SELECT *, count(*) FROM #test
group by col having count(*) > 1
"RichardF" <noone@.nowhere.com> wrote in message
news:sm1131tr8v8muj14huhtrtvnjevdkkvirf@.
4ax.com...
>I have a question about how the TOP n clause works on the SELECT
> statement.
> Assume I have a table called TheTable that contains 10,000,000 rows
> and no indexes.
> Assume I execute the following SQL...
> SELECT * TOP 100 FROM TheTable WHERE TheField = "12345"
> Will SQL Server first do a TableScan to find ALL the rows that meet
> the WHERE clause and THEN give me the TOP 100, or is it clever enough
> to stop the TableScan when 100 matching rows have been found?
> Thanks for any help.
> RichardF