0

I have a convoluted MSSQL 2008 query, and the show plan has 40% of the work in a sort operation (there are several mil rows and the query does a top 15000), the rest of the plan is index seeks and scans.

If I remove the TOP clause, the query goes down to a second. But I need the TOP. What are some rules of thumb for smartly optimizing this SORT need?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Snowy
  • 5,942
  • 19
  • 65
  • 119

1 Answers1

0

All of the columns you sort on should be indexed. This alone shoud decrease the need for SQL Server to spend time sorting while executing the query (because they will already be sorted via the index)

The only downside is that more indexes = slower inserts.

Neil N
  • 24,862
  • 16
  • 85
  • 145
  • True, but indexes can't be guaranteed to be used -- especially if the `ORDER BY` includes columns wrapped in functions. – OMG Ponies Jul 07 '11 at 19:29