SQL Server Logical Query Processing

, 2018-04-12

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. 

8. SELECT
9. DISTINCT
11. TOP
1. FROM
2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. WITH CUBE/ROLLUP
7. HAVING
10. ORDER BY
12. OFFSET/FETCH

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

1.  FROM
2.  ON
3.  JOIN
4.  WHERE
5.  GROUP BY
6.  WITH CUBE/ROLLUP
7.  HAVING
8.  SELECT
9.  DISTINCT
10. ORDER BY
11. TOP
12. OFFSET/FETCH

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.
USE WideWorldImportersDW
GO
SELECT * FROM (
			SELECT DISTINCT TOP 10 s.[Salesperson Key] AS Salesperson, sum(s.profit) AS profit
			FROM fact.Sale s 
			INNER JOIN  fact.Sale s1 ON s1.[sale key] = s.[Sale Key]
			INNER JOIN Dimension.Employee e ON s.[Salesperson Key] = e.[Employee Key]
			WHERE e.[Is Salesperson] =1
			GROUP BY s. [Salesperson Key]
			HAVING COUNT (s.[Salesperson Key])>1
			ORDER BY Salesperson 
		) A 
ORDER BY a.[Salesperson]
OFFSET 7 ROWS;

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.
USE WideWorldImportersDW
GO
SELECT * FROM (
			SELECT DISTINCT TOP 10 s.[Salesperson Key] AS Salesperson, sum(s.profit) AS profit
			FROM fact.Sale s 
			INNER JOIN  fact.Sale s1 ON s1.[sale key] = s.[Sale Key]
			INNER JOIN Dimension.Employee e ON s.[Salesperson Key] = e.[Employee Key]
			WHERE e.[Is Salesperson] =1
			AND Salesperson = 15
			GROUP BY s. [Salesperson Key]
			HAVING COUNT (s.[Salesperson Key])>1
			ORDER BY Salesperson 
		) A 
ORDER BY a.[Salesperson]
OFFSET 7 ROWS;

The query failed with below error. 

Msg 207, Level 16, State 1, Line 49
Invalid column name 'Salesperson'.

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).
USE WideWorldImportersDW
GO
SELECT * FROM (
			SELECT DISTINCT TOP 10 s.[Salesperson Key] AS Salesperson, sum(s.profit) AS profit
			FROM fact.Sale s 
			INNER JOIN  fact.Sale s1 ON s1.[sale key] = s.[Sale Key]
			INNER JOIN Dimension.Employee e ON s.[Salesperson Key] = e.[Employee Key]
			WHERE e.[Is Salesperson] =1
			GROUP BY s. [Salesperson Key]
			HAVING COUNT (Salesperson)>1
			ORDER BY Salesperson 
		) A 
ORDER BY a.[Salesperson]
OFFSET 7 ROWS;

The query again failed with below error. 

Msg 207, Level 16, State 1, Line 49
Invalid column name 'Salesperson'.

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

Happy Learning!!

The post SQL Server Logical Query Processing appeared first on .

Rate

Share

Share

Rate

Related content

Database Mirroring FAQ: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup?

Question: Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? This question was sent to me via email. My reply follows. Can a 2008 SQL instance be used as the witness for a 2005 database mirroring setup? Databases to be mirrored are currently running on 2005 SQL instances but will be upgraded to 2008 SQL in the near future.

2009-02-23

1,567 reads

Networking - Part 4

You may want to read Part 1 , Part 2 , and Part 3 before continuing. This time around I'd like to talk about social networking. We'll start with social networking. Facebook, MySpace, and Twitter are all good examples of using technology to let...

2009-02-17

1,530 reads

Speaking at Community Events - More Thoughts

Last week I posted Speaking at Community Events - Time to Raise the Bar?, a first cut at talking about to what degree we should require experience for speakers at events like SQLSaturday as well as when it might be appropriate to add additional focus/limitations on the presentations that are accepted. I've got a few more thoughts on the topic this week, and I look forward to your comments.

2009-02-13

360 reads