1

I noticed this discussion on NOT using ORDER BY in SQL query unless really necessary.

https://www.brentozar.com/archive/2019/10/how-to-think-like-the-sql-server-engine-adding-an-order-by/amp/

The author suggests that looking at "Query Bucks" which I think in my exec plan is "Estimated CPU cost", that the Sort is very expensive.

enter image description here

Here are the individual operations:

enter image description here

and

enter image description here

So, up to now I have been looking at exec plan and trying to make the most expensive "Table Scan 97%" more efficient.

BUT maybe I should move to sort completely out of the query and into my C#?

0

1 Answer 1

1

It's actually a very good question and can go a lot deeper but I'll give you the short version. It all boils down to your scenario.

If your requirements demand this dataset to be always sorted in the same way, consider creating an index to tune the query.

If your requirements don't demand any kind of sorting, don't bother yourself (and the engine).

If your requirements ask for a dynamic sort, there are lots of techniques you can try, sometimes even a dynamic query can save the day here.

If there's a dynamic sort thing in your app and the dataset is the same, just upload the data and let the app worry about sorting, with the added bonus of not hitting the SQL Server again just to bring the same dataset in a different order.

If you are actually getting paginated data from the DB and just fetching one page at a time from it so you must sort it in the query, there's no magic here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.