Search code examples
sql-servert-sqlquery-optimization

Does ORDER BY clause slows down the query?


Does the ORDER BY clause slows down the query performance ??? How much will it effect if the column is indexed as against when it is not.


Solution

  • @JamesZ is correct. There are many things to consider when adding the order by clause to your query. For instance if you did a select top 10 * from dbo.Table order by field with let's say 10,000,000 rows would cause the query to spill into tempdb as it spooled the entire table to tempdb and then after sorting by your non-indexed field would then return the 10 rows. If you did the same select without the sort, results would return almost immediately.

    It's very important to know how your tables are indexed before issuing an order by clause. CTRL-M is your friend in SSMS.