[Solved] Using temp table for sorting data in SQL Server

EverSQL Database Performance Knowledge Base

Using temp table for sorting data in SQL Server

Database type:

Recently, I came across a pattern (not sure, could be an anti-pattern) of sorting data in a SELECT query. The pattern is more of a verbose and non-declarative way for ordering data. The pattern is to dump relevant data from actual table into temporary table and then apply orderby on a field on the temporary table. I guess, the only reason why someone would do that is to improve the performance (which I doubt) and no other benefit.

For e.g. Let's say, there is a user table. The table might contain rows in millions. We want to retrieve all the users whose first name starts with 'G' and sorted by first name. The natural and more declarative way to implement a SQL query for this scenario is:

More natural and declarative way

SELECT * FROM Users
WHERE NAME LIKE 'G%'
ORDER BY Name

Verbose way

SELECT * INTO TempTable
FROM Users
WHERE NAME LIKE 'G%'

SELECT * FROM TempTable
ORDER BY Name

With that context, I have few questions:

  1. Will there be any performance difference between two ways if there is no index on the first name field. If yes, which one would be better.

  2. Will there be any performance difference between two ways if there is an index on the first name field. If yes, which one would be better.

  3. Should not the SQL Server optimizer generate same execution plan for both the ways?

  4. Is there any benefit in writing a verbose way from any other persective like locking/blocking?

Thanks in advance.

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
CREATE INDEX users_idx_name ON Users (NAME);
The optimized query:
SELECT
        * 
    FROM
        Users 
    WHERE
        Users.NAME LIKE 'G%' 
    ORDER BY
        Users.Name

Related Articles



* original question posted on StackOverflow here.