当前位置:网站首页>Understanding the execution order of T-SQL query from the execution order of join on and where
Understanding the execution order of T-SQL query from the execution order of join on and where
2022-07-25 15:14:00 【The south wind knows what I mean】
Sample code
CREATE TABLE "SCOTT"."A" (
"PERSON_ID" NUMBER(5) NULL ,
"PERSON_NAME" VARCHAR2(255 BYTE) NULL
);
-- ----------------------------
-- Records of A
-- ----------------------------
INSERT INTO "SCOTT"."A" VALUES ('1', ' Zhang San ');
INSERT INTO "SCOTT"."A" VALUES ('2', ' Li Si ');
INSERT INTO "SCOTT"."A" VALUES ('3', ' Wang Wu ');
INSERT INTO "SCOTT"."A" VALUES ('4', ' Zhao Liu ');
INSERT INTO "SCOTT"."A" VALUES ('5', ' Zhou Qi ');
CREATE TABLE "SCOTT"."B" (
"PERSON_ID" NUMBER(5) NULL ,
"LOVE_FRUIT" VARCHAR2(255 BYTE) NULL
);
-- ----------------------------
-- Records of B
-- ----------------------------
INSERT INTO "SCOTT"."B" VALUES ('1', ' Banana ');
INSERT INTO "SCOTT"."B" VALUES ('2', ' Apple ');
INSERT INTO "SCOTT"."B" VALUES ('3', ' a mandarin orange ');
INSERT INTO "SCOTT"."B" VALUES ('4', ' pear ');
INSERT INTO "SCOTT"."B" VALUES ('8', ' peach ');
1.join on
SELECT * FROM A LEFT JOIN ORACLE.B ON A.PERSON_ID=B.PERSON_ID AND A.PERSON_ID=1;

2.where
SELECT * FROM A LEFT JOIN ORACLE.B ON A.PERSON_ID=B.PERSON_ID WHERE A.PERSON_ID=1;

Why are the results different ? It can be explained from the process of query logic processing
SQL Execution order
(1)FROM <left_table> <join_type> JOIN <right_table> ON <on_predicate>
(2)WHERE <where_predicate>
(3)GROUP BY <group_by_specification>
(4)HAVING <having_predicate>
(5)SELECT DISTINCT TOP(<top_specification>) <select_list>
(6)ORDER BY <order_by_list>
- FROM
- ON
- JOIN
- WHERE
- GROUP BY
- WITH CUBE or WITH ROLLUP
- HAVING
- SELECT
- DISTINCT ORDER BY TOP
·
When these steps are carried out , Each step produces a virtual table , The virtual table is used as input for the next step . These virtual tables are for callers ( Client applications or external queries ) Unavailable . Only the table generated in the last step is returned to the caller . If a clause is not specified in the query , The corresponding steps will be skipped
SELECT What has been done at each stage ?
(1)FROM Stage
FROM The stage identifies the source table of the query , And handle table operators . In queries involving join operations ( Various join), There are mainly the following steps :
a. Find the Cartesian product . No matter what kind of join operation it is , The first is to perform cross connect (cross join), Find the Cartesian product , Generate virtual tables VT1-J1.
b.ON Filter . This phase is very important for the VT1-J1 Screening , according to ON Clause , Let the predicate take the value true My line passed the test , Insert into VT1-J2.
c. Add external row . If you specify outer join, Also need to VT1-J2 No matching row found in , Add as an external line to VT1-J2 in , Generate VT1-J3.
Go through the above steps ,FROM The stage is over . In a nutshell ,FROM The stage is for preprocessing , The tables mentioned in the statement are processed according to the operators provided ( except join, also apply,pivot,unpivot)
(2)WHERE Stage
WHERE The stage is based on <where_predicate> Medium condition pair VT1 To filter , The line that makes the condition true will be inserted into VT2 in .
(3)GROUP BY Stage
GROUP The stage is listed according to the specified column name , take VT2 Group the rows in , Generate VT3. Finally, there is only one line per group .
(4)HAVING Stage
This stage is based on HAVING Predicate pair in clause VT3 And then select the groups , And insert the eligible group into VT4 in .
(5)SELECT Stage
This stage is the process of projection , Handle SELECT The elements mentioned in clause , produce VT5. This step is usually carried out in the following order
a. Calculation SELECT Expressions in the list , Generate VT5-1.
b. If you have any DISTINCT, Delete VT5-1 Duplicate lines in , Generate VT5-2
c. If you have any TOP, According to ORDER BY The logical order in which clauses are defined , from VT5-2 Select the number or percentage of rows in the signature , Generate VT5-3
(6)ORDER BY Stage
according to ORDER BY Clause , Yes VT5-3 The lines in the , Sort , Generate cursor VC6.
Example explanation
1.join on Execution process
SELECT * FROM A LEFT JOIN ORACLE.B ON A.PERSON_ID=B.PERSON_ID AND A.PERSON_ID=1;
Find the Cartesian product , produce 5*5=25 Bar record 
ON Filter (A.PERSON_ID=B.PERSON_ID AND A.PERSON_ID=1)
Add external row 
2.where Execution process
SELECT * FROM A LEFT JOIN ORACLE.B ON A.PERSON_ID=B.PERSON_ID WHERE A.PERSON_ID=1;
Find the Cartesian product , produce 5*5=25 Bar record 
ON Filter (A.PERSON_ID=B.PERSON_ID )
Add external row 
WHERE Stage (A.PERSON_ID=1)
3. The next one sql The results of the implementation of
SELECT * FROM A LEFT JOIN ORACLE.B ON A.PERSON_ID=B.PERSON_ID

边栏推荐
- iframe嵌套其它网站页面 全屏设置
- [C topic] Li Kou 206. reverse the linked list
- Hbck 修复问题
- 剑指Offer | 二进制中1的个数
- CGO is realy Cool!
- Splice a field of the list set into a single string
- Spark002 --- spark task submission, pass JSON as a parameter
- [C topic] Li Kou 88. merge two ordered arrays
- sql to linq 之存储过程偏
- Browser workflow (Simplified)
猜你喜欢

解决DBeaver SQL Client 连接phoenix查询超时

《三子棋》C语言数组应用 --n皇后问题雏形

图片裁剪cropper 示例

6线SPI传输模式探索

Use the command to check the WiFi connection password under win10 system

反射-笔记

Fast-lio: fast and robust laser inertial odometer based on tightly coupled IEKF

Nacos2.1.0 cluster construction

"How to use" agent mode

Boosting之GBDT源码分析
随机推荐
"Ask every day" what is volatile
SSM Advanced Integration
Spark002 --- spark task submission, pass JSON as a parameter
Debounce and throttle
Splice a field of the list set into a single string
简易轮播图和打地鼠
6月产品升级观察站
spark中saveAsTextFile如何最终生成一个文件
延迟加载源码剖析:
期货在线开户是否安全?去哪家公司手续费最低?
Object.prototype. Hasownproperty() and in
When using jetty to run items, an error is reported: form too large or form too many keys
"Ask every day" how locksupport realizes thread waiting and wakeup
基于OpenCV和YOLOv3的目标检测实例应用
[thread knowledge points] - spin lock
CGO is realy Cool!
sql to linq 之存储过程偏
如何更新更新数据库中的json值?
Introduction to raspberry Pie: initial settings of raspberry pie
js URLEncode函数