用WITH…AS改寫標量子查詢
示例1:
示例2:
標量子查詢完勝!
點選(此處)摺疊或開啟
- select prod_id,
-
prod_name,
prod_list_price,
(select max(quantity_sold)
from sales s
where s.prod_id = p.prod_id
and s.time_id > p.prod_eff_from ) as max_quantity_sold,
(select max(amount_sold)
from sales s
where s.prod_id = p.prod_id
and s.time_id < p.prod_eff_to ) as max_amount_sold
from products p -
Plan hash value: 1097661653
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 72 | 3168 | 3 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 15 | | |
| 2 | TABLE ACCESS BY INDEX ROWID| SALES | 5455 | 81825 | 1512 (1)| 00:00:19 |
|* 3 | INDEX RANGE SCAN | SALES_PK | 982 | | 530 (1)| 00:00:07 |
| 4 | SORT AGGREGATE | | 1 | 15 | | |
| 5 | TABLE ACCESS BY INDEX ROWID| SALES | 5455 | 81825 | 1512 (1)| 00:00:19 |
|* 6 | INDEX RANGE SCAN | SALES_PK | 982 | | 530 (1)| 00:00:07 |
| 7 | TABLE ACCESS FULL | PRODUCTS | 72 | 3168 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("S"."PROD_ID"=:B1 AND "S"."TIME_ID">:B2)
filter("S"."TIME_ID">:B1)
6 - access("S"."PROD_ID"=:B1 AND "S"."TIME_ID"<:B2)
filter("S"."TIME_ID"<:B1)
點選(此處)摺疊或開啟
- WITH s2 AS
-
(select p.rowid as rid,
max(case
when s.time_id > p.prod_eff_from then
s.quantity_sold
END) AS max_quantity_sold,
max(case
when s.time_id < p.prod_eff_to then
s.amount_sold
END) AS max_amount_sold
from sales s
inner join products p on p.prod_id = s.prod_id
group by p.rowid)
select prod_id,
prod_name,
prod_list_price,
s2.max_quantity_sold,
s2.max_amount_sold
from products p
left join s2 on s2.rid = p.rowid
-
Plan hash value: 983752392
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 7855K| 636M| 31653 (2)| 00:06:20 |
|* 1 | HASH JOIN OUTER | | 7855K| 636M| 31653 (2)| 00:06:20 |
| 2 | TABLE ACCESS FULL | PRODUCTS | 72 | 3384 | 3 (0)| 00:00:01 |
| 3 | VIEW | | 7855K| 284M| 31629 (2)| 00:06:20 |
| 4 | HASH GROUP BY | | 7855K| 322M| 31629 (2)| 00:06:20 |
|* 5 | HASH JOIN | | 7855K| 322M| 31408 (1)| 00:06:17 |
| 6 | TABLE ACCESS FULL| PRODUCTS | 72 | 1800 | 3 (0)| 00:00:01 |
| 7 | TABLE ACCESS FULL| SALES | 14M| 252M| 31365 (1)| 00:06:17 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("S2"."RID"(+)="P".ROWID)
5 - access("P"."PROD_ID"="S"."PROD_ID")
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22621861/viewspace-2077158/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MYSQL count標量子查詢改left joinMySql
- GreatSQL 最佳化技巧:將 MINUS 改寫為標量子查詢SQL
- [20200325]慎用標量子查詢.txt
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- [20211220]關於標量子查詢問題.txt
- Oracle常用抓取SQL-標量子查詢等OracleSQL
- mysql 標量子查詢和現金盤程式製作非法子查詢MySql
- [20180626]函式與標量子查詢14.txt函式
- [20180625]函式與標量子查詢13(補充)函式
- [20180612]函式與標量子查詢10.txt函式
- [20180611]函式與標量子查詢9.txt函式
- [20180607]函式與標量子查詢8.txt函式
- [20180602]函式與標量子查詢4.txt函式
- [20180602]函式與標量子查詢3.txt函式
- 20180601]函式與標量子查詢2.txt函式
- [20211214]18c標量子查詢unnest.txt
- 影響Oracle標量子查詢效能的三個因素Oracle
- 【TUNE_ORACLE】列出有標量子查詢的SQL參考OracleSQL
- [20210202]計算標量子查詢快取數量2.txt快取
- [20210201]19c計算標量子查詢快取數量.txt快取
- 遊標查詢
- 如何查詢上標
- 美團搜尋中查詢改寫技術的探索與實踐
- 391、Java框架46 -【Hibernate - 查詢HQL、查詢Criteria、查詢標準SQL】 2020.10.19Java框架SQL
- Linux 查詢佔用磁碟IO讀寫很高的程式方法Linux
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- 免費教你寫增刪改查介面
- PostgreSQL 原始碼解讀(24)- 查詢語句#9(查詢重寫)SQL原始碼
- SpringBoot整合Elasticsearch遊標查詢(scroll)Spring BootElasticsearch
- 模糊查詢區分大小寫嗎?
- SqlServer查詢資料改動歷史記錄SQLServer
- 二分查詢—包括查詢第一個目標元素和最後一個目標元素
- SSM整合_年輕人的第一個增刪改查_查詢SSM
- 量子計算將如何改變世界?
- 查詢埠號佔用
- pytest 用例查詢原理
- 流式查詢1. mybatis的遊標Cursor,分頁大資料查詢MyBatis大資料
- js查詢HTMLCollection物件中的下標JSHTML物件
- 前端工具Rome將用Rust改寫前端Rust