以 AdventureWorks2019.Person.Person 單一 Table 來記錄
下述語法,由內而外邏輯上會
- 篩選 BusinessEntityID 1 到 10 資料
- 篩選 BusinessEntityID 1 到 5 資料
- 篩選 BusinessEntityID 1 資料
SELECT T2.*
FROM
(
SELECT T1.*
FROM
(
SELECT *
FROM Person.Person
WHERE BusinessEntityID BETWEEN 1 AND 10
) AS T1
WHERE T1.BusinessEntityID BETWEEN 1 AND 5
) AS T2
WHERE T2.BusinessEntityID = 1
觀察執行計畫發現,直接篩選 BusinessEntityID 1 資料
範例二:內部先執行
下述語法,由內而外邏輯上會
- Person.Person 和 Person.Person 進行 UNION ALL 形成 T2
- T1 和 T2 進行 JOIN 並篩選 BusinessEntityID 1 資料
SELECT T1.*
FROM Person.Person AS T1
JOIN
(
SELECT * FROM Person.Person
UNION ALL
SELECT * FROM Person.Person
) AS T2 ON T1.BusinessEntityID = T2.BusinessEntityID
WHERE T1.BusinessEntityID = 1
觀察執行計畫發現,先篩選 T1 BusinessEntityID 1 資料後,再跑 Nested Loops 進去 T2 篩選資料,篩選完才 UNION ALL,三個 Person.Person Table 都只抓出 1 筆資料而已
沒有留言:
張貼留言