21

There are so many sorting algorithms, but I want to know which algorithm is used in SQL Server when we use Order by and without Order by.

7
  • 1
    What possible value is there in knowing the answer? Commented May 4, 2011 at 7:52
  • 16
    There is nothing bad in knowing something new :-D Commented May 4, 2011 at 8:19
  • Oracle's sorting algorithms are a company secret and part of their competitive advantage. I guess the same holds for SQL Server. Commented May 4, 2011 at 9:15
  • 2
    Without ORDER BY is easy: in that case, there is no ordering - none at all. Commented May 4, 2011 at 11:25
  • 3
    The value in knowing (and it's why I arrived at this question) is whether it would be better to load data from the database using order by in the query, or just load the data with no ordering and then sort the data in your application. For example, if I load a list of objects, depending on whether C#'s List.Sort method is more efficient, I may be better off omitting the order by clause and just sort the data once I've loaded it into my application. Commented Oct 22, 2013 at 22:36

4 Answers 4

3

Yea, Elzo is right, SQL Server (and many other RDBMS) uses several different and complicated sorting algorithms. They aim to achieve a balance between memory usage, average response time, while maintaining high levels of resource concurrency. In a certain situation, the algorithm choice is based on the data types involved, the size of data to be sorted, or the number of sort keys specified, and so on.

Refer to this thread: What algorithms does SQL use?

Sign up to request clarification or add additional context in comments.

1 Comment

the question you link to doesn't cover sorting as far as I can see. I definitely don't remember it being covered in the internals book in the accepted answer there.
3

I guess it depends on the column that you choose to order BY. If is integer is a different algorithm than for strings. Another guess will be that having or not having indexes for that column will also be of vital importance.

This is the algorithm for text order by in Mysql.

The original filesort algorithm works as follows: Read all rows according to key or by table scanning. Rows that do not match the WHERE clause are skipped. For each row, store a pair of values in a buffer (the sort key and the row pointer). The size of the buffer is the value of the sort_buffer_size system variable. When the buffer gets full, run a qsort (quicksort) on it and store the result in a temporary file. Save a pointer to the sorted block. (If all pairs fit into the sort buffer, no temporary file is created.) Repeat the preceding steps until all rows have been read. Do a multi-merge of up to MERGEBUFF (7) regions to one block in another temporary file. Repeat until all blocks from the first file are in the second file. Repeat the following until there are fewer than MERGEBUFF2 (15) blocks left. On the last multi-merge, only the pointer to the row (the last part of the sort key) is written to a result file. Read the rows in sorted order by using the row pointers in the result file. To optimize this, we read in a big block of row pointers, sort them, and use them to read the rows in sorted order into a row buffer. The size of the buffer is the value of the read_rnd_buffer_size system variable. The code for this step is in the sql/records.cc source file.

Comments

2

If you don't use ORDER BY, then there is no implied or natural order. So no algorithm. This applies to most RDBMS. Only ORDER BY will give any ordering of results.

When you do use ORDER BY, it follows the column list, asc/desc, collation, expressions etc that you specify. The only non-intuitive rule I can think of is "NULLs first" for a column but otherwise sorts are straightforward in SQL Server.

1 Comment

I took it the OP was asking about how it actually does the sort (quicksort, merge sort etc)
-1

There are two different sort logics used by SQL Server to handle sorting! First one is the quick sort and another one is Merge sort. It begins sort in memory using Quick Sort algorithm but note that the memory requirement for this is at least 200% of its input rows size. Now if the memory grant is exceeded(even by a single byte), it spills entire immediate sort and remaining sort to disk. Then it will complete the sort on disk using Merge Sort algorithm.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.