SQL Server Logical Query Processing

Recently, while reviewing one of the stored procedure performance, I suggested a couple of changes to a developer to improve the performance of the SP. One of the suggestions was to replace DISTINCT with GROUP BY.

A few days later, he came back to me with a performance matrix to show that there was no performance improvement when he replaced the DISTINCT with GROUP BY.

I wished I could have informed him the reasons for replacing it during our initial discussion. But, it happened for a good purpose because the performance matrix double confirmed that we were not going to see performance improvement in our scenario by changing the clause.

Below are the two reasons to ask him to replace the DISTINCT with GROUP BY;

  • Logical Query Processing order
  • In some cases, GROUPBY performs better than DISTINCT.

Logical Query Processing or Binding Order

The “Logical Query Processing” or “Logical Binding Order” of a SELECT statement defines how a query should be processed and final result achieved. The following list shows the order in which the clauses are logically processed by SQL Server. 

Let’s arrange the order to make it easier to read.

Demonstration

Let’s use the below sample query to understand it.

  • If you look at the “ORDER BY” (order by salesperson) of the inner query, it is using alias “Salesperson” not “s.[Salesprson Key]” actual column name.  If I execute the above query, it runs successfully.

Here is the result of the query.

ReasonIf you follow the above logical processing order, the “ORDER BY” comes after “SELECT” clause.

  • Let’s use the alias name “Salesperson” in the WHERE clause (Salesperson =15) to filter out the data.

The query failed with below error. 

ReasonIn the logical processing order, the “WHERE” comes before “SELECT” clause that’s why it got failed.

  • Now, let’s use the alias name “Salesperson” in the HAVING clause (HAVING COUNT (Salesperson)>1).

The query again failed with below error. 

ReasonThe “HAVING” clause comes before “SELECT” clause.

Happy Learning!!

Leave a comment

Your email address will not be published. Required fields are marked *