views:

218

answers:

1

Hi

what is faster query ?

select Name from Worker

or

select TOP(1) Name from Worker

I have 1,000,000 records

thank's in advance

+8  A: 

If you don't have an ORDER BY or a DISTINCT, SELECT TOP(1) Name FROM Worker is faster.

The reason for this is that if you do happen to have an ORDER BY or a DISTINCT, the query has to go through the entire table to sort and filter out unwanted results. If it's a straight SELECT TOP, however, it can go to the first page, take the first row, and be done with it very quickly.

Eric
True. Of course if you have 1,000,000 records then selecting top 1 will be faster no matter what. An order by clause will just slow things down further and narrow the difference in speed a bit.
Steve Wortham
thank's alot !!
Gold
During execution the query might flow control because the client is unable to parse the result fast enough (consume the 1 mil. names result set). So even with ordering the TOP 1 *may* be faster, depending on how slow the client processing is.
Remus Rusanu