Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is faster query (select name .... or select top(1) name

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

like image 595
Gold Avatar asked Dec 30 '22 22:12

Gold


1 Answers

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.

like image 179
Eric Avatar answered Jan 01 '23 13:01

Eric