在展开下面的original sql 和 execution plan之前,要知道这个SQL的问题就在于占用大量的TEMP space

orignal SQL

SELECT
roster.IC_N AS icN, roster.WORK_SHIFT_C AS workShiftC, roster.EXTRA_SHIFT_C AS extraShiftC,
roster.GENERATED_SHIFT_C AS generatedShiftC, roster.RESERVE_SHIFT_C AS reserveShiftCode,
num.STAFF_N AS staffNo, grp.scheme_n AS schemeN, deploy.deploy_department_c AS dept,
code.deployment_c AS deploymentC
FROM STAFF_ROSTER roster, STAFF_WORK WORK1, STAFF_CATEGORY category, staff_roster_group grp, STAFF staff,
staff_number num, staff_cross_deploy deploy, staff_deployment_code code
WHERE
WORK1.IC_N = roster.IC_N
AND category.IC_N = roster.IC_N
AND CATEGORY.CATEGORY_C in ('SQC','RQC','WOS','CES','WCES','NTCES','HEP','SOSOS','SVCOS','CCOS','CCSOS','IGCMO','SOCMO','YOCMO','LS','LSUP','LSO','IGCMO2','SOCMO2','YOCMO2','IGCMO3','SOCMO3','YOCMO3')
AND staff.IC_N = roster.IC_N
AND staff.IC_N = num.IC_N
AND staff.IC_N = deploy.IC_N
AND staff.ic_n = grp.ic_n
AND staff.ic_n = code.ic_n(+)
AND code.DEPARTMENT_C(+) = 'C'
AND TO_CHAR(code.SHIFT_D(+), 'DD/MM/YYYY') = '11/03/2014'
AND deploy.WORK_DEPARTMENT_C = 'C'
AND work1.home_department_c = deploy.work_department_c
AND work1.home_department_c = roster.department_c
AND TO_CHAR(roster.SHIFT_D, 'DD/MM/YYYY') = '11/03/2014'
AND WORK1.START_DT <= roster.shift_d
AND ((WORK1.END_DT > roster.shift_d) OR (WORK1.END_DT IS NULL))
AND category.EFFECTIVE_D = (SELECT MAX(SC.EFFECTIVE_D) FROM STAFF_CATEGORY SC WHERE SC.IC_N = roster.IC_N AND SC.EFFECTIVE_D <= roster.SHIFT_D)
AND num.EFFECTIVE_D = (SELECT MAX(SN.EFFECTIVE_D) FROM STAFF_NUMBER SN WHERE SN.IC_N = roster.IC_N AND SN.EFFECTIVE_D <= roster.SHIFT_D)
AND TO_DATE('11/03/2014', 'dd/mm/yyyy') BETWEEN deploy.start_dt AND deploy.end_dt
AND grp.EFFECTIVE_D = (SELECT MAX(SG.EFFECTIVE_D) FROM STAFF_ROSTER_GROUP SG WHERE SG.IC_N = roster.IC_N AND SG.EFFECTIVE_D <= roster.SHIFT_D)

Dont be afraid

origanl EXECUTION plan

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1872515827 ---------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 267 | | 64452 (12)| 00:12:54 |
| 1 | NESTED LOOPS | | 1 | 267 | | 64452 (12)| 00:12:54 |
| 2 | NESTED LOOPS | | 1 | 245 | | 64433 (12)| 00:12:54 |
| 3 | NESTED LOOPS | | 1 | 224 | | 64432 (12)| 00:12:54 |
| 4 | NESTED LOOPS OUTER | | 1 | 201 | | 64431 (12)| 00:12:54 |
| 5 | NESTED LOOPS | | 1 | 177 | | 64428 (12)| 00:12:54 |
|* 6 | HASH JOIN | | 1 | 150 | | 64427 (12)| 00:12:54 |
|* 7 | HASH JOIN | | 1 | 123 | | 43227 (12)| 00:08:39 |
|* 8 | HASH JOIN | | 1 | 96 | | 21602 (12)| 00:04:20 |
| 9 | TABLE ACCESS BY INDEX ROWID | STAFF_ROSTER | 1 | 28 | | 3 (0)| 00:00:01 |
| 10 | NESTED LOOPS | | 11 | 759 | | 138 (1)| 00:00:02 |
| 11 | NESTED LOOPS | | 44 | 1804 | | 19 (0)| 00:00:01 |
|* 12 | TABLE ACCESS BY INDEX ROWID| STAFF_CROSS_DEPLOY | 44 | 1364 | | 19 (0)| 00:00:01 |
|* 13 | INDEX RANGE SCAN | STAFF_CROSS_DEPLOY_NNDX | 73 | | | 9 (0)| 00:00:01 |
|* 14 | INDEX UNIQUE SCAN | STAFF_PK | 1 | 10 | | 0 (0)| 00:00:01 |
|* 15 | INDEX RANGE SCAN | STAFF_ROSTER_PK | 1 | | | 2 (0)| 00:00:01 |
| 16 | VIEW | VW_SQ_2 | 7227 | 190K| | 21463 (12)| 00:04:18 |
| 17 | HASH GROUP BY | | 7227 | 268K| 234M| 21463 (12)| 00:04:18 |
| 18 | MERGE JOIN | | 5097K| 184M| | 1944 (27)| 00:00:24 |
| 19 | SORT JOIN | | 7227 | 127K| | 39 (8)| 00:00:01 |
| 20 | INDEX FAST FULL SCAN | STAFF_NUMBER_PK | 7227 | 127K| | 37 (3)| 00:00:01 |
|* 21 | SORT JOIN | | 14107 | 275K| 792K| 1731 (20)| 00:00:21 |
|* 22 | INDEX FAST FULL SCAN | STAFF_ROSTER_IDX1 | 14107 | 275K| | 1638 (21)| 00:00:20 |
| 23 | VIEW | VW_SQ_3 | 7283 | 192K| | 21624 (12)| 00:04:20 |
| 24 | HASH GROUP BY | | 7283 | 270K| 236M| 21624 (12)| 00:04:20 |
| 25 | MERGE JOIN | | 5136K| 186M| | 1954 (27)| 00:00:24 |
| 26 | SORT JOIN | | 7283 | 128K| | 47 (7)| 00:00:01 |
| 27 | INDEX FAST FULL SCAN | STAFF_ROSTER_GROUP_PK | 7283 | 128K| | 45 (3)| 00:00:01 |
|* 28 | SORT JOIN | | 14107 | 275K| 792K| 1731 (20)| 00:00:21 |
|* 29 | INDEX FAST FULL SCAN | STAFF_ROSTER_IDX1 | 14107 | 275K| | 1638 (21)| 00:00:20 |
| 30 | VIEW | VW_SQ_1 | 7137 | 188K| | 21200 (12)| 00:04:15 |
| 31 | HASH GROUP BY | | 7137 | 264K| 231M| 21200 (12)| 00:04:15 |
| 32 | MERGE JOIN | | 5033K| 182M| | 1927 (27)| 00:00:24 |
| 33 | SORT JOIN | | 7137 | 125K| | 24 (13)| 00:00:01 |
| 34 | TABLE ACCESS FULL | STAFF_CATEGORY | 7137 | 125K| | 22 (5)| 00:00:01 |
|* 35 | SORT JOIN | | 14107 | 275K| 792K| 1731 (20)| 00:00:21 |
|* 36 | INDEX FAST FULL SCAN | STAFF_ROSTER_IDX1 | 14107 | 275K| | 1638 (21)| 00:00:20 |
|* 37 | TABLE ACCESS BY INDEX ROWID | STAFF_WORK | 1 | 27 | | 1 (0)| 00:00:01 |
|* 38 | INDEX UNIQUE SCAN | STAFF_WORK_UNDX | 1 | | | 0 (0)| 00:00:01 |
| 39 | TABLE ACCESS BY INDEX ROWID | STAFF_DEPLOYMENT_CODE | 1 | 24 | | 3 (0)| 00:00:01 |
|* 40 | INDEX RANGE SCAN | STAFF_DEPLOYMENT_CODE_PK | 1 | | | 2 (0)| 00:00:01 |
|* 41 | INDEX RANGE SCAN | STAFF_NUMBER_PK | 1 | 23 | | 1 (0)| 00:00:01 |
|* 42 | INDEX RANGE SCAN | STAFF_ROSTER_GROUP_PK | 1 | 21 | | 1 (0)| 00:00:01 |
| 43 | INLIST ITERATOR | | | | | | |
|* 44 | INDEX UNIQUE SCAN | STAFF_CATEGORY_PK | 1 | 22 | | 19 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id):
--------------------------------------------------- 6 - access("ITEM_1"="ROSTER"."IC_N" AND "ITEM_2"=ROWID)
7 - access("ITEM_5"="ROSTER"."IC_N" AND "ITEM_6"=ROWID)
8 - access("ITEM_3"="ROSTER"."IC_N" AND "ITEM_4"=ROWID)
12 - filter("DEPLOY"."END_DT">=TO_DATE(' 2014-03-11 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND
"DEPLOY"."START_DT"<=TO_DATE(' 2014-03-11 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
13 - access("DEPLOY"."WORK_DEPARTMENT_C"='C')
14 - access("STAFF"."IC_N"="DEPLOY"."IC_N")
15 - access("STAFF"."IC_N"="ROSTER"."IC_N" AND "ROSTER"."DEPARTMENT_C"='C')
filter(TO_CHAR(INTERNAL_FUNCTION("ROSTER"."SHIFT_D"),'DD/MM/YYYY')='11/03/2014')
21 - access("SN"."EFFECTIVE_D"<="ROSTER"."SHIFT_D")
filter("SN"."EFFECTIVE_D"<="ROSTER"."SHIFT_D")
22 - filter(TO_CHAR(INTERNAL_FUNCTION("ROSTER"."SHIFT_D"),'DD/MM/YYYY')='11/03/2014')
28 - access("SG"."EFFECTIVE_D"<="ROSTER"."SHIFT_D")
filter("SG"."EFFECTIVE_D"<="ROSTER"."SHIFT_D")
29 - filter(TO_CHAR(INTERNAL_FUNCTION("ROSTER"."SHIFT_D"),'DD/MM/YYYY')='11/03/2014')
35 - access("SC"."EFFECTIVE_D"<="ROSTER"."SHIFT_D")
filter("SC"."EFFECTIVE_D"<="ROSTER"."SHIFT_D")
36 - filter(TO_CHAR(INTERNAL_FUNCTION("ROSTER"."SHIFT_D"),'DD/MM/YYYY')='11/03/2014')
37 - filter("WORK1"."START_DT"<="ROSTER"."SHIFT_D" AND ("WORK1"."END_DT">"ROSTER"."SHIFT_D" OR "WORK1"."END_DT"
IS NULL))
38 - access("WORK1"."IC_N"="ROSTER"."IC_N" AND "WORK1"."HOME_DEPARTMENT_C"='C')
40 - access("STAFF"."IC_N"="CODE"."IC_N"(+) AND "CODE"."DEPARTMENT_C"(+)='C')
filter(TO_CHAR(INTERNAL_FUNCTION("CODE"."SHIFT_D"(+)),'DD/MM/YYYY')='11/03/2014')
41 - access("STAFF"."IC_N"="NUM"."IC_N" AND "NUM"."EFFECTIVE_D"="VW_COL_1")
filter("NUM"."EFFECTIVE_D"="VW_COL_1")
42 - access("STAFF"."IC_N"="GRP"."IC_N" AND "GRP"."EFFECTIVE_D"="VW_COL_1")
filter("GRP"."EFFECTIVE_D"="VW_COL_1")
44 - access("CATEGORY"."IC_N"="ROSTER"."IC_N" AND ("CATEGORY"."CATEGORY_C"='CCOS' OR
"CATEGORY"."CATEGORY_C"='CCSOS' OR "CATEGORY"."CATEGORY_C"='CES' OR "CATEGORY"."CATEGORY_C"='HEP' OR
"CATEGORY"."CATEGORY_C"='IGCMO' OR "CATEGORY"."CATEGORY_C"='IGCMO2' OR "CATEGORY"."CATEGORY_C"='IGCMO3' OR
"CATEGORY"."CATEGORY_C"='LS' OR "CATEGORY"."CATEGORY_C"='LSO' OR "CATEGORY"."CATEGORY_C"='LSUP' OR
"CATEGORY"."CATEGORY_C"='NTCES' OR "CATEGORY"."CATEGORY_C"='RQC' OR "CATEGORY"."CATEGORY_C"='SOCMO' OR
"CATEGORY"."CATEGORY_C"='SOCMO2' OR "CATEGORY"."CATEGORY_C"='SOCMO3' OR "CATEGORY"."CATEGORY_C"='SOSOS' OR
"CATEGORY"."CATEGORY_C"='SQC' OR "CATEGORY"."CATEGORY_C"='SVCOS' OR "CATEGORY"."CATEGORY_C"='WCES' OR
"CATEGORY"."CATEGORY_C"='WOS' OR "CATEGORY"."CATEGORY_C"='YOCMO' OR "CATEGORY"."CATEGORY_C"='YOCMO2' OR
"CATEGORY"."CATEGORY_C"='YOCMO3') AND "CATEGORY"."EFFECTIVE_D"="VW_COL_1") 91 rows selected.

execution plan

OK. 不用纠结于复杂的SQL 逻辑,只要知道hash 占用大量的TEMP,解决办法就是避免HASH.

而存在hash的原因就是因为 subquery 被 unnest了,那么我们可以避免 unnest,方法是设置下面几个参数之一,或者用hint

SQL > alter session set "_optimizer_cost_based_transformation" = OFF ;

SQL > alter session set "_gby_hash_aggregation_enabled" = FALSE ;

SQL > alter session set "_unnest_subquery"=FALSE;

通过设置参数后得到的execution plan如下

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2603715670 ---------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 186 | 202 (1)| 00:00:03 |
| 1 | NESTED LOOPS | | 1 | 186 | 196 (1)| 00:00:03 |
| 2 | NESTED LOOPS | | 1 | 164 | 177 (1)| 00:00:03 |
| 3 | NESTED LOOPS | | 1 | 143 | 176 (1)| 00:00:03 |
| 4 | NESTED LOOPS | | 1 | 120 | 175 (1)| 00:00:03 |
| 5 | NESTED LOOPS OUTER | | 11 | 1023 | 164 (1)| 00:00:02 |
| 6 | NESTED LOOPS | | 11 | 759 | 138 (1)| 00:00:02 |
| 7 | NESTED LOOPS | | 44 | 1804 | 19 (0)| 00:00:01 |
|* 8 | TABLE ACCESS BY INDEX ROWID| STAFF_CROSS_DEPLOY | 44 | 1364 | 19 (0)| 00:00:01 |
|* 9 | INDEX RANGE SCAN | STAFF_CROSS_DEPLOY_NNDX | 73 | | 9 (0)| 00:00:01 |
|* 10 | INDEX UNIQUE SCAN | STAFF_PK | 1 | 10 | 0 (0)| 00:00:01 |
| 11 | TABLE ACCESS BY INDEX ROWID | STAFF_ROSTER | 1 | 28 | 3 (0)| 00:00:01 |
|* 12 | INDEX RANGE SCAN | STAFF_ROSTER_PK | 1 | | 2 (0)| 00:00:01 |
| 13 | TABLE ACCESS BY INDEX ROWID | STAFF_DEPLOYMENT_CODE | 1 | 24 | 3 (0)| 00:00:01 |
|* 14 | INDEX RANGE SCAN | STAFF_DEPLOYMENT_CODE_PK | 1 | | 2 (0)| 00:00:01 |
|* 15 | TABLE ACCESS BY INDEX ROWID | STAFF_WORK | 1 | 27 | 1 (0)| 00:00:01 |
|* 16 | INDEX UNIQUE SCAN | STAFF_WORK_UNDX | 1 | | 0 (0)| 00:00:01 |
|* 17 | INDEX RANGE SCAN | STAFF_NUMBER_PK | 1 | 23 | 1 (0)| 00:00:01 |
| 18 | SORT AGGREGATE | | 1 | 18 | | |
|* 19 | INDEX RANGE SCAN | STAFF_NUMBER_PK | 1 | 18 | 2 (0)| 00:00:01 |
|* 20 | INDEX RANGE SCAN | STAFF_ROSTER_GROUP_PK | 1 | 21 | 1 (0)| 00:00:01 |
| 21 | SORT AGGREGATE | | 1 | 18 | | |
|* 22 | INDEX RANGE SCAN | STAFF_ROSTER_GROUP_PK | 1 | 18 | 2 (0)| 00:00:01 |
| 23 | INLIST ITERATOR | | | | | |
|* 24 | INDEX UNIQUE SCAN | STAFF_CATEGORY_PK | 1 | 22 | 19 (0)| 00:00:01 |
| 25 | SORT AGGREGATE | | 1 | 18 | | |
|* 26 | INDEX RANGE SCAN | STAFF_CATEGORY_PK | 1 | 18 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------

但是用hint的方式去避免unnest 则没有成功,需要测试。

另外疑惑的地方是,max 这种函数在subquery里应该是不unnest的,为什么这里unnest了?

总结一下, 这个case的问题就在于temp占用高,而hash是典型的占用空间多,所以应该自然而然的想到其它join。

优化实例- not use hash to avoid temp space issue的更多相关文章

  1. MySQL优化实例

    这周就要从泰笛离职了,在公司内部的wiki上,根据公司实际的项目,写了一些mysql的优化方法,供小组里的小伙伴参考下,没想到大家的热情很高,还专门搞了个ppt讲解了一下. 举了三个大家很容易犯错的地 ...

  2. mysql 优化实例之索引创建

    mysql 优化实例之索引创建 优化前: pt-query-degist分析结果: # Query 23: 0.00 QPS, 0.00x concurrency, ID 0x78761E301CC7 ...

  3. mysql sql优化实例

    mysql sql优化实例 优化前: pt-query-degist分析结果: # Query 3: 0.00 QPS, 0.00x concurrency, ID 0xDC6E62FA021C85B ...

  4. (转载)Android项目实战(二十八):使用Zxing实现二维码及优化实例

    Android项目实战(二十八):使用Zxing实现二维码及优化实例 作者:听着music睡 字体:[增加 减小] 类型:转载 时间:2016-11-21我要评论 这篇文章主要介绍了Android项目 ...

  5. mysql库表优化实例

    一.SQL优化 1.优化SQL一般步骤 1.1 查看SQL执行频率 SHOW STATUS LIKE 'Com_%'; Com_select:执行SELECT操作的次数,一次查询累加1.其他类似 以下 ...

  6. Android ListView性能优化实例讲解

    前言: 对于ListView,大家绝对都不会陌生,只要是做过Android开发的人,哪有不用ListView的呢? 只要是用过ListView的人,哪有不关心对它性能优化的呢? 关于如何对ListVi ...

  7. iOS 优化实例

    一.接口请求优化 在工程项目中,多个一级界面包含状态,如:服务入口的动态配置,未读消息数量,图片文字等,因此产品设计要每次切换 tab 时都请求数据,及时的更新页面状态.在实际开发中,频繁的调用接口, ...

  8. Nginx+PHP优化实例

    1.PHP-FPM高负载的解决办法 http://blog.haohtml.com/archives/11162 2.Nginx优化配置 http://blog.haohtml.com/archive ...

  9. 弹出框优化实例(alert和confirm)

    在项目过程中会遇到需要使用自己定义的弹出框的情况.以前用过ymprompt,但是它太复杂而且不好自己操控.所以自己写了一个弹出框实例. 主要有两类弹出框alert和confirm.基于jQuery a ...

随机推荐

  1. js返回上一层

    Javascript 返回上一页 1. Javascript 返回上一页 history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). wind ...

  2. [Swift通天遁地]三、手势与图表-(1)监听屏幕上触摸事件的各种状态

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. TypeScript `infer` 关键字

    考察如下类型: type PromiseType<T> = (args: any[]) => Promise<T>; 那么对于符合上面类型的一个方法,如何得知其 Prom ...

  4. Coursera公开课-Machine_learing:编程作业7

    这周的编程作业主要是两方面内容. 1.K-means聚类. 2.PCA(Principle Component Analys)主成分分析. 方式主要是通过对图像的聚类实现压缩图像,后来发现PCA也可以 ...

  5. MVC系列学习(七)-模板页

    1.新建一个MVC项目,选择基本 2.查看文件 看到VS为我们生成了一些东西 布局页面,Layout 指定了模板页 3.开始实例 首先控制器中的代码如下: 视图中代码如下: 1.在/Views/_Vi ...

  6. T-SQL查询基础

    今天来带大家了解下在sql server 中的查询机制 使用select语句进行查询 1.查询所有的数据行和列 select * from student 2.查询部分行和列 select scode ...

  7. iOS动画——CoreAnimation

    CoreAnimation在我之前的UIKit动画里面简单的提了一句CoreAnimation动画,其实大家别看它类库名种有个animation,实际上animation在这个库中只占有很小的地位. ...

  8. body全屏css/网页全屏设置/全屏样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. React Native真机调试安卓版

    React Native真机调试安卓版 一.准备工作 1.1 环境搭建 React Native中文网的文档已经非常清晰地描述了,按照步骤即可.http://reactnative.cn/docs/0 ...

  10. JS——百度背景图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...