Oracle SQL查询优化(左联接,索引)

时间:2019-04-05 14:34:59

标签: sql oracle select optimization

在左联接集中添加表时,以下查询会卡住。如果排除了最后一个联接,查询将顺利运行。但是如果包含最后一个连接,则会被卡住。我也尝试过ALL_ROWS,FIRST_ROWS优化器提示。 cps_taskhis中预期有5500行显示为“ 20190404”。

注意:

  • 表cpsmgt.cps_orderhis.orderid是索引列。 {屏幕截图1}。

  • 表cpsmgt.cps_taskhis.orderid也是一个索引列,在最后一个左连接中将其连接到cpsmgt.cps_orderhis.orderid。 {屏幕截图2}

屏幕截图1: enter image description here

屏幕截图2: enter image description here 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'
    );

查询解释计划(SQL Developer):(将文件另存为html并在浏览器中打开)

https://drive.google.com/open?id=1KBnhETTzgNLRUh7djyfCN-Wo5QxPZTfb

重要说明:我正在尝试将输出插入到临时表中。如果相反,我使用create table as将此数据插入到新表中。相同的查询工作正常:-(

-- previous columns
   (
        SELECT 
            o.reason
        FROM
            cpsmgt.cps_orderhis o
        WHERE
            o.orderid = t.orderid
    ) reason_of_task,
-- rest of the query.

1 个答案:

答案 0 :(得分:0)

您是否尝试过以下提示?

/*+ INDEX(o IDX_CPS_ORDERHIS_3) USE_NL(o t)*/

OPTIMIZER可能没有选择正确的索引,而是选择USE_HASH作为执行计划。