[英]Oracle SQL Query Optimization (Left joins, indexes)
The following query gets stuck when a table is added in left join set. 在左连接集中添加表时,以下查询会卡住。 The query runs smooth if last join is excluded. 如果排除最后一个连接,则查询将平滑运行。 but gets stuck if last join is included. 但如果包含最后一次加入,则会卡住 I have tried ALL_ROWS, FIRST_ROWS optimizer hints as well. 我也尝试了ALL_ROWS,FIRST_ROWS优化器提示。 There are around 5500 expected rows from cps_taskhis for '20190404'. 对于'20190404',cps_taskhis有大约5500个预期行。
Notes: 笔记:
Table cpsmgt.cps_orderhis.orderid is indexed column. 表cpsmgt.cps_orderhis.orderid是索引列。 {screenshot 1}. {screenshot 1}。
Table cpsmgt.cps_taskhis.orderid is also an indexed column which is being joined to cpsmgt.cps_orderhis.orderid in last left join. 表cpsmgt.cps_taskhis.orderid也是一个索引列,它在最后一个左连接中连接到cpsmgt.cps_orderhis.orderid。 {screenshot 2} {screenshot 2}
Screen shot 2: 屏幕截图2: SQL Query SQL查询
SELECT /*FIRST_ROWS*/
t.taskid task_no,
t.orderid order_id,
DECODE(t.priority,50,'Low',100,'Medium',200,'High','Other') task_priority,
t.taskname task_name,
TO_CHAR(t.createtime,'HH24') creation_hour,
t.createtime create_time,
t.completedtime completed_time,
( to_timestamp(t.completedtime,'yyyy-mm-dd hh24:mi:ss') - to_timestamp(t.createtime,'yyyy-mm-dd hh24:mi:ss') ) time_difference,
op1.user_name initiator_login,
rd1.name role_assigned_maker,
op2.user_name checker_login,
rd2.name role_assigned_checker,
t.taskstate task_state,
t.tasktype task_type,
t.description task_description,
ord_comment.commenttext, -- 2048 byte varchar2
sim.msisdn target_msisdn,
o.reason reason_of_task, -- 2048 byte varchar2
DECODE(t.bussinesscategory,'0','Business Operation','1','Transaction and Action','2','Financial','3','Manual Task','4','Bulk','5'
,'Configuration','Others:'
|| t.bussinesscategory) businesscategory_of_task
FROM
cpsmgt.cps_taskhis t --PARTITION ( SYS_P30212 ) t
LEFT JOIN cpsmgt.cps_operator op1 ON op1.operator_id = t.createid
LEFT JOIN cpsmgt.cps_operator op2 ON op2.operator_id = t.ownerid
LEFT JOIN cpsmgt.cps_role_operator ro1 ON op1.operator_id = ro1.operator_id
AND op1.status <> '06'
LEFT JOIN cpssys.cps_role_def rd1 ON rd1.role_id = ro1.role_id
AND rd1.status = '30'
LEFT JOIN cpsmgt.cps_role_operator ro2 ON op2.operator_id = ro2.operator_id
AND op2.status <> '06'
LEFT JOIN cpssys.cps_role_def rd2 ON rd2.role_id = ro2.role_id
AND rd2.status = '30'
LEFT JOIN cpsmgt.cps_order_comment ord_comment ON t.orderid = ord_comment.orderid
LEFT JOIN cpsmgt.cps_sim_device sim ON t.on_identity_id = sim.identity_id
LEFT JOIN cpsmgt.cps_orderhis o ON t.orderid = o.orderid
WHERE
t.createtime BETWEEN TO_DATE(TO_CHAR(20190404)
|| ' 00:00:00','yyyy-mm-dd hh24:mi:ss') AND TO_DATE(TO_CHAR(20190404)
|| ' 23:59:59','yyyy-mm-dd hh24:mi:ss')
AND t.procdefid IN (
'IC_EditP2PMSISDNWorkflow',
'IC_ResetCustomerPinWorkflow',
'TC_TransactionConfirmWorkflow',
'IC_ChangeCustomerProductWorkflow',
'IC_EditG2PMSISDNWorkflow',
'IC_ChangeCustomerIdentityStatusWorkflow',
'TC_CancelRemittanceWorkflow',
'IC_ChangeCustomerMSISDNWorkflow',
'IC_ResetOrgOperatorPINWorkflow',
'IC_MigrateCustTrustL1Workflow',
'IC_MigrateCustTrustL2Workflow',
'IC_ChangeCustomerIdentityKYCWorkflow',
'IC_UnblockPaymentTransactionWorkflow',
'IC_ResetOrgOperatorPasswordWorkflow'
)
AND rd1.name IN (
'Complaints (Maker)',
'MFS 1344 Help Line',
'MFS 4444 Helpline',
'Operator user(back end user) maker',
'Complaints Checker',
'Operator user(back end user) completer'
)
AND rd2.name IN (
'Complaints (Maker)',
'MFS 1344 Help Line',
'MFS 4444 Helpline',
'Operator user(back end user) maker',
'Complaints Checker',
'Operator user(back end user) completer'
);
Query Explain Plan (SQL Developer): (save the file as html and open in browser) 查询说明计划(SQL Developer):(将文件另存为html并在浏览器中打开)
https://drive.google.com/open?id=1KBnhETTzgNLRUh7djyfCN-Wo5QxPZTfb https://drive.google.com/open?id=1KBnhETTzgNLRUh7djyfCN-Wo5QxPZTfb
Important Notes : I am trying to insert the output to a temporary table. 重要说明:我正在尝试将输出插入临时表。 If instead i insert this data to a new table using create table as. 相反,如果我使用create table as将此数据插入到新表中。 The same query is working fine :-( 相同的查询工作正常:-(
-- previous columns
(
SELECT
o.reason
FROM
cpsmgt.cps_orderhis o
WHERE
o.orderid = t.orderid
) reason_of_task,
-- rest of the query.
Have you tried the following hint? 你试过以下提示吗?
/*+ INDEX(o IDX_CPS_ORDERHIS_3) USE_NL(o t)*/
Maybe the OPTIMIZER does not choose the right index and choose USE_HASH for the execution plan. 也许OPTIMIZER没有选择正确的索引并为执行计划选择USE_HASH。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.