Click here to Skip to main content
15,916,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
1
SQL
select TOP 30 fName from names where fName like 'Sa%'

2
SQL
select * fName from names where fName like 'Sa%'

first:what is different between 1 and 2 In terms of speed?
second:in fact,i don't want SQL engine find all query result then return 30 top,i want to after finding 30 first result ,it stopped searching for saving time,is it possible or in SQL coding field?
thanks all
Posted

Hi,
your first query will give the exact 30 records which contains fName starts with 'Sa' (if your table 'names' contains more than 30 records with satisfied conditions)

&

the second query will give you all the records from the table 'names' which fulfill the above conditions.

As you required only 30 records so go for 1st query.
 
Share this answer
 
What comes to the differences in speed with statement one and two, the only certain thing is that in the first one only 30 or less records are transferred to the caller. In the second one all the rows are transferred which may be more than 30. Because of this there probably is less network traffic in the first one which may make the query faster.

What comes to the differences in actual execution, it depends. The key is, how early the TOP expression can be evaluated in the execution plan, the earlier the better. However this is decided by the database when the optimal execution plan is created.

A good rule of thumb is, never fetch more than needed. If you need just first 30 rows then use TOP to return those. However, you haven't specified any ordering so the top 30 records would be 'random' ones.

Another rule of thumb is that you should take care if indexing. Indexes are the key to fast data access and for good execution plans. Good indexing will help you to build fast statements.

So how would you know what plan the database has for fetching the data. If you use Sql Server Management Studio, you can utilize the execution plan explanation, either estimated or actual. By interpreting the plan you'll see how the statement will be executed and you're also able to pinpoint the problems in executions, for example missing indices etc.

For more info, see Display the Estimated Execution Plan[^]
 
Share this answer
 
Comments
Maciej Los 23-Aug-15 17:19pm    
Well explained, +5!
mohamadMahmodi 24-Aug-15 7:21am    
on my table is clustered Index and i know about execution plan but main question is that i need a command to tell SQL:"my dear sql!don't search all records for finding -like 'al%'- then return 30 first,please just fetch for me 30 first result and don't touch another"
Wendelius 24-Aug-15 9:47am    
As explained, this is a decision the database makes. The decision is affected by your conditions, the structure of the SQL statement and the amount of time used for calculating permutations.

If you have a clustered index, it's a good candidate for partial scans. In this sense the execution is stopped before every row is investigated. But are all the rows investigated that contain a value satisfying the condition even if you have a TOP expression depends on the ordering and where the TOP expression is located in the plan.

What I'm trying to say that it's impossible to give an exact answer. The only way to be sure is to investigate the plan. And remember that the plan is valid only for that exact statement with the exact same data and for a limited amount of time. In other words, the plan may be different tomorrow.
If you want to get only 30 records at time, please read about sql pagination.

Tutorial 25: Efficiently Paging Through Large Amounts of Data[^]
Paging Through a Query Result[^]

You can use LinqToSql[^] to get desired portion of records.
Effective Paging Using LINQ (Sample Code)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900