T-sql statement query execution order T-sql statement query execution order

T-sql statement query execution order

sql execution order

Reprinted from: http://www.cnblogs.com/knowledgesea/p/4177830.html

 

foreword

Database query execution is undoubtedly one of the necessary skills for programmers. However, the process of database query execution is colorful, but it is rarely understood. Today, my brother will take you to pretend to force you to fly. Let’s take a deeper look at this SQL query. The ins and outs, to lay a foundation for the performance optimization of the query, maybe you will encounter it in the interview.

This blog, abandoning the query optimization performance, as its basis, only explains the analysis of the query process.

The process described in this blog post is

1. The last identified sql statement shows the process of query execution

2. The previous flow chart

3. Do an example to gradually analyze in depth to help understand

4. Make a compelling summary

Processing steps of sql query statement, code list

copy code
-- Query combined fields
(5)select (5-2) distinct(5-3) top(<top_specification>)(5-1)<select_list>
-- even table
(1)from (1-J)<left_table><join_type> join <right_table> on <on_predicate>
        (1-A)<left_table><apply_type> apply <right_table_expression> as <alias>
        (1-P)<left_table> pivot (<pivot_specification>) as <alias>
        (1-U)<left_table> unpivot (<unpivot_specification>) as <alias>
--Query conditions
(2)where <where_pridicate>
--grouping
(3)group by <group_by_specification>
-- grouping conditions
(4)having<having_predicate>
--sort
(6)order by<order_by_list>
copy code

illustrate:

1. The order is 1-6, 6 large steps, and then subdivided, 5-1, 5-2, 5-3, from small to large, 1-J, 1-A, 1-P, 1- U, is the parallel order. If it's not clear enough, I'll take a look at the flow chart next.

2. During the execution process, multiple virtual tables (mentioned below) will be generated accordingly to match the final correct query.

Processing steps of sql query statement, flow chart

 

Instance preparation, creating tables, inserting data, and writing instance query statements to be analyzed

1. First create 2 tables

2. Create two tables and insert table data, the script is as follows 

  View Code 

3. Write the query statement we want to parse, that is, the example statement to be queried in this article.

copy code
select top(4)  status , max(m.id) as maxMemberID
from [dbo].[Member] as m right outer join [dbo].[Order] as o
on m.id=o.member_id 
where m.id>0 group by status having status>=0 order by maxMemberID asc
copy code

Step-by-step analysis of example sentences

The first step, start from from.

1.1. Load the left table

from [dbo].[Member] as m

Query result: all data in member table

1.2. This should be right outer join, but it is defined in sql and decomposed into 2 steps, namely join and right outer join. Expression keywords are executed sequentially from left to right.

join [dbo].[Order] as o

Query result: stored in virtual table vt1, which is a Cartesian set of two tables. Here you may not understand what a Cartesian set is. Let me give an example. I hope you don’t dislike it. It is the problem of children shaking hands. There are 3 students in class A (as three pieces of data in a table), and there are 3 students in class B. 2 students (as 2 pieces of data in another table). The children of class B and the children of class A have a party. First of all, everyone must make sure that they have a hand with the students of the other class, so the crossed set is ( 2*3=6) there are 6 different trajectories. This set of trajectories is the Cartesian set. If you still don't understand, let me say it again, that is, the first data in the m (5 data) table shakes hands with all the data in the o (7 data) table, there are 7 data, and so on, a total of 35 different data The data. The null value here is also to be added.

1.3, on filter

on m.id=o.member_id

Query result: Delete the unmatched rows from the 35 pieces of data in the Cartesian set in the previous step, and get 5 pieces of data, which are stored in the virtual table Vt2

1.4. Add an outer row

right outer join [dbo].[Order] as o

The query result is: the right table (order) is used as the reserved table, and the remaining data is re-added to the virtual table vt2 in the previous step, and the virtual table vt3 is generated.

The second part, enter the where stage

where m.id>0

Query result: Stored in the virtual table vt4, it is a result set whose filtering condition is true. A memory point is added here, that is, the filtering deletion of where is permanent, and the filtering deletion of on is temporary, because after filtering on, there are It may be possible to add external rows through outer and reload the data back, but not where.

The third part, group by grouping

group by status

Query result: Store in vt5, and start grouping with the value of the status column, that is, the status column. The same value is grouped into a group. The two nulls here are regarded as true in the three-valued logic. Three-valued logic: true, false, null. These three values, null is unknown, are the logical characteristics of the data. In some places, two nulls are equal to true, and in some places, they are false. There are a lot of explanations for this in Baidu.

 The fourth step, having filter

having status>=0 

Query result: filter the grouped data, and delete the ones that do not meet the conditions

The fifth step, select query to select the calculated column

5.1. Calculation expressions

select status , max(m.id)

Query result: Calculate the maximum m.id in each group from the grouped data, and list the columns to be filtered and displayed.

5.2, distinct filter repetition

5.3. How many rows are filtered by top combined with order by, but the data here is not sorted, just how many rows of data are listed.

The sixth part, order by sorting display.

A painful summary, the pretense is justified

This blog reference: "Microsoft SQL Server 2008 Technology Insider: T-SQL Query", thank you for reading, (C#).NET Technology Sharing QQ Group: 232458226, welcome to join.

foreword

Database query execution is undoubtedly one of the necessary skills for programmers. However, the process of database query execution is colorful, but it is rarely understood. Today, my brother will take you to pretend to force you to fly. Let’s take a deeper look at this SQL query. The ins and outs, to lay a foundation for the performance optimization of the query, maybe you will encounter it in the interview.

This blog, abandoning the query optimization performance, as its basis, only explains the analysis of the query process.

The process described in this blog post is

1. The last identified sql statement shows the process of query execution

2. The previous flow chart

3. Do an example to gradually analyze in depth to help understand

4. Make a compelling summary

Processing steps of sql query statement, code list

copy code
-- Query combined fields
(5)select (5-2) distinct(5-3) top(<top_specification>)(5-1)<select_list>
-- even table
(1)from (1-J)<left_table><join_type> join <right_table> on <on_predicate>
        (1-A)<left_table><apply_type> apply <right_table_expression> as <alias>
        (1-P)<left_table> pivot (<pivot_specification>) as <alias>
        (1-U)<left_table> unpivot (<unpivot_specification>) as <alias>
--Query conditions
(2)where <where_pridicate>
--grouping
(3)group by <group_by_specification>
-- grouping conditions
(4)having<having_predicate>
--sort
(6)order by<order_by_list>
copy code

illustrate:

1. The order is 1-6, 6 large steps, and then subdivided, 5-1, 5-2, 5-3, from small to large, 1-J, 1-A, 1-P, 1- U, is the parallel order. If it's not clear enough, I'll take a look at the flow chart next.

2. During the execution process, multiple virtual tables (mentioned below) will be generated accordingly to match the final correct query.

Processing steps of sql query statement, flow chart

 

Instance preparation, creating tables, inserting data, and writing instance query statements to be analyzed

1. First create 2 tables

2. Create two tables and insert table data, the script is as follows 

  View Code 

3. Write the query statement we want to parse, that is, the example statement to be queried in this article.

copy code
select top(4)  status , max(m.id) as maxMemberID
from [dbo].[Member] as m right outer join [dbo].[Order] as o
on m.id=o.member_id 
where m.id>0 group by status having status>=0 order by maxMemberID asc
copy code

Step-by-step analysis of example sentences

The first step, start from from.

1.1. Load the left table

from [dbo].[Member] as m

Query result: all data in member table

1.2. This should be right outer join, but it is defined in sql and decomposed into 2 steps, namely join and right outer join. Expression keywords are executed sequentially from left to right.

join [dbo].[Order] as o

Query result: stored in virtual table vt1, which is a Cartesian set of two tables. Here you may not understand what a Cartesian set is. Let me give an example. I hope you don’t dislike it. It is the problem of children shaking hands. There are 3 students in class A (as three pieces of data in a table), and there are 3 students in class B. 2 students (as 2 pieces of data in another table). The children of class B and the children of class A have a party. First of all, everyone must make sure that they have a hand with the students of the other class, so the crossed set is ( 2*3=6) there are 6 different trajectories. This set of trajectories is the Cartesian set. If you still don't understand, let me say it again, that is, the first data in the m (5 data) table shakes hands with all the data in the o (7 data) table, there are 7 data, and so on, a total of 35 different data The data. The null value here is also to be added.

1.3, on filter

on m.id=o.member_id

Query result: Delete the unmatched rows from the 35 pieces of data in the Cartesian set in the previous step, and get 5 pieces of data, which are stored in the virtual table Vt2

1.4. Add an outer row

right outer join [dbo].[Order] as o

The query result is: the right table (order) is used as the reserved table, and the remaining data is re-added to the virtual table vt2 in the previous step, and the virtual table vt3 is generated.

The second part, enter the where stage

where m.id>0

Query result: Stored in the virtual table vt4, it is a result set whose filtering condition is true. A memory point is added here, that is, the filtering deletion of where is permanent, and the filtering deletion of on is temporary, because after filtering on, there are It may be possible to add external rows through outer and reload the data back, but not where.

The third part, group by grouping

group by status

Query result: Store in vt5, and start grouping with the value of the status column, that is, the status column. The same value is grouped into a group. The two nulls here are regarded as true in the three-valued logic. Three-valued logic: true, false, null. These three values, null is unknown, are the logical characteristics of the data. In some places, two nulls are equal to true, and in some places, they are false. There are a lot of explanations for this in Baidu.

 The fourth step, having filter

having status>=0 

Query result: filter the grouped data, and delete the ones that do not meet the conditions

The fifth step, select query to select the calculated column

5.1. Calculation expressions

select status , max(m.id)

Query result: Calculate the maximum m.id in each group from the grouped data, and list the columns to be filtered and displayed.

5.2, distinct filter repetition

5.3. How many rows are filtered by top combined with order by, but the data here is not sorted, just how many rows of data are listed.

The sixth part, order by sorting display.

A painful summary, the pretense is justified

This blog reference: "Microsoft SQL Server 2008 Technology Insider: T-SQL Query", thank you for reading, (C#).NET Technology Sharing QQ Group: 232458226, welcome to join.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324460230&siteId=291194637