A study of Inline view and analytic function
show rel
release 1102000300
desc orders;
Name Null Type
------------ ---- ------------
ORDER_ID NUMBER(38)
CREATE_DATE DATE
CUSTOMER_ID NUMBER(19)
TOTAL_AMOUNT NUMBER(38,3)
insert into orders
select level, trunc(sysdate - mod(level, 31)), mod(level, 100), mod(level, 1000)
from dual connect by level <= 10000;
commit;
EXEC DBMS_STATS.GATHER_TABLE_STATS(user, 'ORDERS',cascade=>true);
Orders表裡有1萬個訂單,屬於100個客戶,這些訂單生成於最近31天內。求每個客戶訂單金額最高的訂單生成日期!先看inline view形式的SQL:
select orders.create_date, orders.customer_id, orders.total_amount
from orders,
(select max(total_amount) highest, customer_id
from orders group by customer_id) hh
where orders.total_amount=hh.highest;
-------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 16 (100)| |
|* 1 | HASH JOIN | | 1000 | 28000 | 16 (13)| 00:00:01 |
| 2 | VIEW | | 100 | 1300 | 8 (13)| 00:00:01 |
| 3 | HASH GROUP BY | | 100 | 700 | 8 (13)| 00:00:01 |
| 4 | TABLE ACCESS FULL| ORDERS | 10000 | 70000 | 7 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | ORDERS | 10000 | 146K| 7 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("ORDERS"."TOTAL_AMOUNT"="HH"."HIGHEST")
再觀察使用分析函式的SQL:
SELECT * FROM (
select create_date,
customer_id,
total_amount,
RANK() over(partition by customer_id order by total_amount desc) rank
from orders)
WHERE RANK=1;
-----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 8 (100)| |
|* 1 | VIEW | | 10000 | 468K| 8 (13)| 00:00:01 |
|* 2 | WINDOW SORT PUSHED RANK| | 10000 | 146K| 8 (13)| 00:00:01 |
| 3 | TABLE ACCESS FULL | ORDERS | 10000 | 146K| 7 (0)| 00:00:01 |
-----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("RANK"=1)
2 - filter(RANK() OVER ( PARTITION BY "CUSTOMER_ID" ORDER BY
INTERNAL_FUNCTION("TOTAL_AMOUNT") DESC )<=1)
在這裡,分析函式效能優於inline view,由於不用做hash連線。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/638844/viewspace-772455/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Prevents Merging An Inline ViewinlineView
- ORACLE HANDBOOK系列之一:Oracle分析函式(Analytic Function)Oracle函式Function
- 關於inline view內嵌檢視的學習inlineView
- flask 裝飾器 AssertionError: View function mapping is overwriting an existing endpoint functionFlaskErrorViewFunctionAPP
- 查詢已經刪除的procedure,view,functionViewFunction
- Analytic Functions in OracleFunctionOracle
- Study
- push_pred, a powerful hint to speed up you inline view query 10X timesinlineView
- Approx Analytic ArealightAPP
- CSS Variable StudyCSS
- How to Study OracleOracle
- RCE_STUDY
- javascript 中function(){},new function(),new Function(),Function 摘錄JavaScriptFunction
- inline 宣告inline
- App Extension Study 1APP
- display:inline,inline-block,block元素的區別inlineBloC
- gnu inline asminlineASM
- Stability Study with SAP Quality Management
- Study for Go ! Chapter two - ExpressionGoAPTExpress
- display:inline、block、inline-block的區別(轉的)inlineBloC
- 【Analytic】分析函式之MIN函式函式
- 【Analytic】分析函式之MAX函式函式
- 【Analytic】分析函式之AVG函式函式
- 【Analytic】分析函式之RANK函式函式
- 【Analytic】分析函式之COUNT函式函式
- Vertical-Align: 關於inline,inline-block文字排版inlineBloC
- $(function(){})與(function($){....})(jQuery)的區別FunctionjQuery
- inline用法詳解inline
- Inline Views (183)inlineView
- Learn 和 Study 的區別
- Git study Day02 分支Git
- activiti-study 整合 學習
- Oracle Linux JRE StudyOracleLinux
- linux find command studyLinux
- Start Study MSSQL 2005SQL
- Study Plan For Algorithms - Part1Go
- Study Plan For Algorithms - Part3Go
- Study Plan For Algorithms - Part4Go