一次300多行SQL从7分15s到24s的SQL历程

环境:阿里云 ECS 11.2.0.1 ADG Oracle

环境分析

前几天优化了一条SQL搞定以后,以为晚上日终批量报表没有问题了,然后昨天想着检查一下,看awr报告,截取晚上1点到2点的时间段,赫然映入眼帘。SQL换了,四条完全类似的SQL语句,有300多行,着实吓了一条,和开发了解到,这四条SQL其实差不多算是一条SQL,只是最后取模的的值分为1,2,3,4,所以运行了4次,可是每一次都将近400s,


然后就手动的执行了其中的一条语句,取得是模为2的为例进行执行如下:
   
   
   
  1. select *
  2.   from (select a.*, rownum rn
  3.           from (select t7.processid,
  4.                        t7.taskname,
  5.                        t7.operatorcode,
  6.                        t7.orgcode,
  7.                        t7.orgcate,
  8.                        t7.protype,
  9.                        t7.procode,
  10.                        nvl(t1.prodaytotal, 0) prodaytotal,
  11.                        nvl(t2.prodaytreat, 0) prodaytreat,
  12.                        nvl(t3.prodayuntreat, 0) prodayuntreat,
  13.                        nvl(t4.prodaytotalnew, 0) prodaytotalnew,
  14.                        nvl(t5.prodaytreatnew, 0) prodaytreatnew,
  15.                        nvl(t6.prodayuntreatnew, 0) prodayuntreatnew,
  16.                        (select count(b.activationtime) daytasktotal
  17.                           from bpm.jbpm_processinfo a,
  18.                                VBPMEXTTASK          b,
  19.                                f_vsmsysdate         c
  20.                          where a.processid = t7.processid
  21.                            and a.id = b.PROCESSINFOID
  22.                            and b.operatorcode = t7.operatorcode
  23.                            and a.processid = b.processid
  24.                            and b.taskname = t7.taskname
  25.                            and trunc(b.activationtime, 'dd') = c.BUSINESSDATE) daytasktotal,
  26.                        (select count(b.activationtime) daytasktreat
  27.                           from bpm.jbpm_processinfo a,
  28.                                VBPMEXTTASK          b,
  29.                                f_vsmsysdate         c
  30.                          where a.processid = t7.processid
  31.                            and a.id = b.PROCESSINFOID
  32.                            and b.operatorcode = t7.operatorcode
  33.                            and a.processid = b.processid
  34.                            and b.taskname = t7.taskname
  35.                            and trunc(b.completetime, 'dd') = c.BUSINESSDATE) daytasktreat,
  36.                        (select count(b.activationtime) daytaskuntreat
  37.                           from bpm.jbpm_processinfo a,
  38.                                VBPMEXTTASK          b,
  39.                                f_vsmsysdate         c
  40.                          where a.processid = t7.processid 
  41.   and a.id = b.PROCESSINFOID
  42.                            and b.operatorcode = t7.operatorcode
  43.                            and a.processid = b.processid
  44.                            and b.taskname = t7.taskname
  45.                            and trunc(b.activationtime, 'dd') = c.BUSINESSDATE
  46.                            and trunc(b.completetime, 'dd') is null) daytaskuntreat,
  47.                        (select count(b.activationtime) daytasktotalnew
  48.                           from bpm.jbpm_processinfo a,
  49.                                VBPMEXTTASK          b,
  50.                                f_vsmsysdate         c
  51.                          where a.processid = t7.processid
  52.                            and b.operatorcode = t7.operatorcode
  53.                            and a.processid = b.processid
  54.                            and a.id = b.PROCESSINFOID
  55.                            and b.taskname = t7.taskname
  56.                            and trunc(b.activationtime, 'dd') = c.BUSINESSDATE
  57.                            and b.tasktypeflag = '00') daytasktotalnew,
  58.                        (select count(b.activationtime) daytasktreatnew
  59.                           from bpm.jbpm_processinfo a,
  60.                                VBPMEXTTASK          b,
  61.                                f_vsmsysdate         c
  62.                          where a.processid = t7.processid
  63.                            and b.operatorcode = t7.operatorcode
  64.                            and a.processid = b.processid
  65.                            and a.id = b.PROCESSINFOID
  66.                            and b.taskname = t7.taskname
  67.                            and trunc(b.completetime, 'dd') = c.BUSINESSDATE
  68.                            and b.tasktypeflag = '00') daytasktreatnew,
  69.                        (select count(b.activationtime) daytaskuntreatnew
  70.                           from bpm.jbpm_processinfo a,
  71.                                VBPMEXTTASK          b,
  72.                                f_vsmsysdate         c
  73.                          where a.processid = t7.processid
  74.                            and b.operatorcode = t7.operatorcode
  75.                            and a.processid = b.processid
  76.                            and a.id = b.PROCESSINFOID
  77.                            and b.taskname = t7.taskname
  78.                            and trunc(b.activationtime, 'dd') = c.BUSINESSDATE
  79.                            and trunc(b.completetime, 'dd') is null
  80.                            and b.tasktypeflag = '00') daytaskuntreatnew,
  81.                        (select count(b.activationtime) beforetasktotal
  82.                           from bpm.jbpm_processinfo a,
  83.                                VBPMEXTTASK          b,
  84.                                f_vsmsysdate         c
  85.                          where a.processid = t7.processid
  86.                            and b.operatorcode = t7.operatorcode
  87.                            and a.processid = b.processid
  88.                            and a.id = b.PROCESSINFOID
  89.                            and b.taskname = t7.taskname
  90.                            and trunc(b.activationtime, 'dd') <= c.BUSINESSDATE) beforetasktotal
  91.                   from (select a.processid,
  92.                                b.taskname,
  93.                                b.operatorcode,
  94.                                b.orgcode,
  95.                                c.orgcate,
  96.                                a.PROTYPE,
  97.                                a.productcode procode,
  98.                                COUNT(B.ACTIVATIONTIME) prodaytotal
  99.                           from bpm.jbpm_processinfo a,
  100.                                VBPMEXTTASK          b,
  101.                                f_vsmorg             c,
  102.                                f_smproduct          d,
  103.                                f_vsmsysdate         e
  104.                          where a.processid = b.processid
  105.                            and a.id = b.PROCESSINFOID
  106.                            and b.orgcode = c.orgcode
  107.                            and a.productcode = d.procode
  108.                            and trunc(b.activationtime, 'dd') = e.BUSINESSDATE
  109.                            and a.processid in ('financeApp',
  110.                                                'aheadRedemption',
  111.                                                'financeInno',
  112.                                                'financeCusMod',
  113.                                                'financeAccMod')
  114.                          GROUP BY a.processid,
  115.                                   b.taskname,
  116.                                   b.operatorcode,
  117.                                   b.orgcode,
  118.                                   c.orgcate,
  119.                                   a.protype,
  120.                                   a.productcode) t1,
  121.                        (select a.processid,
  122.                                b.taskname,
  123.                                b.operatorcode,
  124.                                b.orgcode,
  125.                                c.orgcate,
  126.                                a.protype,
  127.                                a.productcode procode,
  128.                                COUNT(B.ACTIVATIONTIME) prodaytreat
  129.                           from bpm.jbpm_processinfo a,
  130.                                VBPMEXTTASK          b,
  131.                                f_vsmorg             c,
  132.                                f_smproduct          d,
  133.                                f_vsmsysdate         e
  134.                          where a.processid = b.processid
  135.                            and a.id = b.PROCESSINFOID
  136.                            and b.orgcode = c.orgcode
  137.                            and a.productcode = d.procode
  138.                            and trunc(b.completetime, 'dd') = e.BUSINESSDATE
  139.                            and a.processid in ('financeApp',
  140.                                                'aheadRedemption',
  141.                                                'financeInno',
  142.                                                'financeCusMod',
  143.                                                'financeAccMod')
  144.                          GROUP BY a.processid,
  145.                                   b.taskname,
  146.                                   b.operatorcode,
  147.                                   b.orgcode,
  148.                                   c.orgcate,
  149.                                   a.protype,
  150.                                   a.productcode) t2,
  151.                        (select a.processid,
  152.                                b.taskname,
  153.                                b.operatorcode,
  154.                                b.orgcode,
  155.                                c.orgcate,
  156.                                a.protype,
  157.                                a.productcode procode,
  158.                                COUNT(B.ACTIVATIONTIME) prodayuntreat
  159.                           from bpm.jbpm_processinfo a,
  160.                                VBPMEXTTASK  b,
  161.                                f_vsmorg     c,
  162.                                f_smproduct  d,
  163.                                f_vsmsysdate e
  164.                          where a.processid = b.processid
  165.                            and b.orgcode = c.orgcode
  166.                            and a.productcode = d.procode
  167.                            and a.id = b.PROCESSINFOID
  168.                            and trunc(b.activationtime, 'dd') = e.BUSINESSDATE
  169.                            and a.processid in ('financeApp',
  170.                                                'aheadRedemption',
  171.                                                'financeInno',
  172.                                                'financeCusMod',
  173.                                                'financeAccMod')
  174.                            and b.completetime is null
  175.                          GROUP BY a.processid,
  176.                                   b.taskname,
  177.                                   b.operatorcode,
  178.                                   b.orgcode,
  179.                                   c.orgcate,
  180.                                   a.protype,
  181.                                   a.productcode) t3,
  182.                        (select a.processid,
  183.                                b.taskname,
  184.                                b.operatorcode,
  185.                                b.orgcode,
  186.                                c.orgcate,
  187.                                a.protype,
  188.                                a.productcode procode,
  189.                                COUNT(B.ACTIVATIONTIME) prodaytotalnew
  190.                           from bpm.jbpm_processinfo a,
  191.                                VBPMEXTTASK          b,
  192.                                credit.smorg         c,
  193.                                f_smproduct          d,
  194.                                f_vsmsysdate         e
  195.                          where a.processid = b.processid
  196.                            and b.orgcode = c.orgcode
  197.                            and a.id = b.PROCESSINFOID
  198.                            and a.productcode = d.procode
  199.                            and trunc(b.activationtime, 'dd') = e.BUSINESSDATE
  200.                            and a.processid in ('finan ceApp',
  201.                                                'aheadRedemption',
  202.                                                'financeInno',
  203.                                                'financeCusMod',
  204.                                                'financeAccMod')
  205.                            and b.tasktypeflag = '00'
  206.                          GROUP BY a.processid,
  207.                                   b.taskname,
  208.                                   b.operatorcode,
  209.                                   b.orgcode,
  210.                                   c.orgcate,
  211.                                   a.protype,
  212.                                   a.productcode) t4,
  213.                        (select a.processid,
  214.                                b.taskname,
  215.                                b.operatorcode,
  216.                                b.orgcode,
  217.                                c.orgcate,
  218.                                a.protype,
  219.                                a.productcode procode,
  220.                                COUNT(B.ACTIVATIONTIME) prodaytreatnew
  221.                           from bpm.jbpm_processinfo a,
  222.                                VBPMEXTTASK          b,
  223.                                credit.smorg         c,
  224.                                f_smproduct          d,
  225.                                f_vsmsysdate         e
  226.                          where a.processid = b.processid
  227.                            and b.orgcode = c.orgcode
  228.                            and a.productcode = d.procode
  229.                            and a.id = b.PROCESSINFOID
  230.                            and trunc(b.completetime, 'dd') = e.BUSINESSDATE
  231.                            and a.processid in ('financeApp',
  232.                                                'aheadRedemption',
  233.                                                'financeInno',
  234.                                                'financeCusMod',
  235.                                                'financeAccMod')
  236.                            and b.tasktypeflag = '00'
  237.                          GROUP BY a.processid,
  238.                                   b.taskname,
  239.                                   b.operatorcode,
  240.                                   b.orgcode,
  241.                                   c.orgcate,
  242.                                   a.protype,
  243.                                   a.productcode) t5,
  244.                        (select a.processid,
  245.                                b.taskname,
  246.                                b.operatorcode,
  247.                                b.orgcode,
  248.                                c.orgcate,
  249.                                a.protype,
  250.                                a.productcode procode,
  251.                                COUNT(B.ACTIVATIONTIME) prodayuntreatnew
  252.                           from bpm.jbpm_processinfo a,
  253.                                VBPMEXTTASK          b,
  254.                                credit.smorg         c,
  255.                                f_smproduct          d,
  256.                                f_vsmsysdate         e
  257.                          where a.processid = b.processid
  258.                            and b.orgcode = c.orgcode
  259.                            and a.productcode = d.procode
  260.                            and a.id = b.PROCESSINFOID
  261.                            and trunc(b.activationtime, 'dd') = e.BUSINESSDATE
  262.                            and trunc(b.completetime, 'dd') is null
  263.                            and a.processid in ('financeApp',
  264.                                                'aheadRedemption',
  265.                                                'financeInno',
  266.                                                'financeCusMod',
  267.                                                'financeAccMod')
  268.                            and b.tasktypeflag = '00'
  269.                          GROUP BY a.processid,
  270.                                   b.taskname,
  271.                                   b.operatorcode,
  272.                                   b.orgcode,
  273.                                   c.orgcate,
  274.                                   a.protype,
  275.                                   a.productcode) t6,
  276.                        (select br.name     processid,
  277.                                br.taskname,
  278.                                s.usercode  operatorcode,
  279.                                v2.PROCODE,
  280.                                v2.PROTYPE,
  281.                                fv.orgcode,
  282.                                fv.orgcate
  283.                           from bpmsteprole       br,
  284.                                f_vsmuser         s,
  285.                                f_vsmuserflowrole sr,
  286.                                f_vsmflowrole     r,
  287.                                f_vsmproduct      v2,
  288.                                f_smuserpro       v3,
  289.                                f_vsmorg          fv
  290.                          where s.id = sr.flowuserid
  291.                            and sr.flowroleid = r.id
  292.                            and r.flowrolecode = br.steprole
  293.                            and s.validflag in ('0', '1')
  294.                            and s.id = v3.userid
  295.                            and v2.id = v3.proid
  296.                            and s.ORGID = fv.ID
  297.                            AND sr.USEFLAG = '1') t7
  298.                  where t7.processid = t1.processid(+)
  299.                    and t7.processid = t2.processid(+)
  300.                    and t7.processid = t3.processid(+)
  301.                    and t7.processid = t4.processid(+)
  302.                    and t7.processid = t5.processid(+)
  303.                    and t7.processid = t6.processid(+)
  304.                    and t7.taskname = t1.taskname(+)
  305.                    and t7.taskname = t2.taskname(+)
  306.                    and t7.taskname = t3.taskname(+)
  307.                    and t7.taskname = t4.taskname(+)
  308.                    and t7.taskname = t5.taskname(+)
  309.                    and t7.taskname = t6.taskname(+)
  310.                    and t7.operatorcode = t1.operatorcode(+)
  311.                    and t7.operatorcode = t2.operatorcode(+)
  312.                    and t7.operatorcode = t3.operatorcode(+)
  313.                    and t7.operatorcode = t4.operatorcode(+)
  314.                    and t7.operatorcode = t5.operatorcode(+)
  315.                    and t7.operatorcode = t6.operatorcode(+)
  316.                    and t7.orgcode = t1.orgcode(+)
  317.                    and t7.orgcode = t2.orgcode(+)
  318.                    and t7.orgcode = t3.orgcode(+)
  319.                    and t7.orgcode = t4.orgcode(+)
  320.                    and t7.orgcode = t5.orgcode(+)
  321.                    and t7.orgcode = t6.orgcode(+)
  322.                    and t7.orgcate = t1.orgcate(+)
  323.                    and t7.orgcate = t2.orgcate(+)
  324.                    and t7.orgcate = t3.orgcate(+)
  325.                    and t7.orgcate = t4.orgcate(+)
  326.                    and t7.orgcate = t5.orgcate(+)
  327.                    and t7.protype = t6.protype(+)
  328.                    and t7.protype = t1.protype(+)
  329.                    and t7.protype = t2.protype(+)
  330.                    and t7.protype = t3.protype(+)
  331.                    and t7.protype = t4.protype(+)
  332.                    and t7.protype = t5.protype(+)
  333.                    and t7.protype = t6.protype(+)
  334.                    and t7.procode = t1.procode(+)
  335.                    and t7.procode = t2.procode(+)
  336.                    and t7.procode = t3.procode(+)
  337.                    and t7.procode = t4.procode(+)
  338.                    and t7.procode = t5.procode(+)
  339.                    and t7.procode = t6.procode(+)) a)
  340.  where mod(rn, 4) = 2
执行计划如下:
    
    
    
  1. 36679 rows selected.
  2. Elapsed: 00:07:15.54
  3. Execution Plan
  4. ----------------------------------------------------------
  5. Plan hash value: 2469110542
  6. -------------------------------------------------------------------------------------------------------------------------------
  7. | Id  | Operation     | Name      | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
  8. -------------------------------------------------------------------------------------------------------------------------------
  9. |   0 | SELECT STATEMENT     |      |   183K|    44M|       |   268K (1)| 00:53:39 |
  10. |   1 |  SORT AGGREGATE     |      |     1 |    84 |       |   |      |
  11. |   2 |   NESTED LOOPS     |      |       |       |       |   |      |
  12. |   3 |    NESTED LOOPS     |      |     1 |    84 |       |   401 (0)| 00:00:05 |
  13. |   4 |     NESTED LOOPS     |      |     1 |    68 |       |   399 (0)| 00:00:05 |
  14. |   5 |      TABLE ACCESS FULL     | SMSYSDATE      |     1 |     8 |       |     3 (0)| 00:00:01 |
  15. |*  6 |      TABLE ACCESS BY INDEX ROWID     | JBPM_EXT_TASK      |     1 |    60 |       |   396 (0)| 00:00:05 |
  16. |*  7 |       INDEX RANGE SCAN     | EXTTASK_OPTCODE      |   574 |       |       |     4 (0)| 00:00:01 |
  17. |*  8 |     INDEX UNIQUE SCAN     | SYS_C0011919      |     1 |       |       |     1 (0)| 00:00:01 |
  18. |*  9 |    TABLE ACCESS BY INDEX ROWID     | JBPM_PROCESSINFO      |     1 |    16 |       |     2 (0)| 00:00:01 |
  19. |  10 |  SORT AGGREGATE     |      |     1 |    95 |       |   |      |
  20. |  11 |   NESTED LOOPS     |      |       |       |       |   |      |
  21. |  12 |    NESTED LOOPS     |      |     1 |    95 |       |   401 (0)| 00:00:05 |
  22. |  13 |     NESTED LOOPS     |      |     1 |    79 |       |   399 (0)| 00:00:05 |
  23. |  14 |      TABLE ACCESS FULL     | SMSYSDATE      |     1 |     8 |       |     3 (0)| 00:00:01 |
  24. |* 15 |      TABLE ACCESS BY INDEX ROWID     | JBPM_EXT_TASK      |     1 |    71 |       |   396 (0)| 00:00:05 |
  25. |* 16 |       INDEX RANGE SCAN     | EXTTASK_OPTCODE      |   574 |       |       |     4 (0)| 00:00:01 |
  26. |* 17 |     INDEX UNIQUE SCAN     | SYS_C0011919      |     1 |       |       |     1 (0)| 00:00:01 |
  27. |* 18 |    TABLE ACCESS BY INDEX ROWID     | JBPM_PROCESSINFO      |     1 |    16 |       |     2 (0)| 00:00:01 |
  28. |  19 |  SORT AGGREGATE     |      |     1 |    95 |       |   |      |
  29. |  20 |   NESTED LOOPS     |      |       |       |       |   |      |
  30. |  21 |    NESTED LOOPS     |      |     1 |    95 |       |   401 (0)| 00:00:05 |
  31. |  22 |     NESTED LOOPS     |      |     1 |    79 |       |   399 (0)| 00:00:05 |
  32. |  23 |      TABLE ACCESS FULL     | SMSYSDATE      |     1 |     8 |       |     3 (0)| 00:00:01 |
  33. |* 24 |      TABLE ACCESS BY INDEX ROWID     | JBPM_EXT_TASK      |     1 |    71 |       |   396 (0)| 00:00:05 |
  34. |* 25 |       INDEX RANGE SCAN     | EXTTASK_OPTCODE      |   574 |       |       |     4 (0)| 00:00:01 |
  35. |* 26 |     INDEX UNIQUE SCAN     | SYS_C0011919      |     1 |       |       |     1 (0)| 00:00:01 |
  36. |* 27 |    TABLE ACCESS BY INDEX ROWID     | JBPM_PROCESSINFO      |     1 |    16 |       |     2 (0)| 00:00:01 |
  37. |  28 |  SORT AGGREGATE     |      |     1 |    87 |       |   |      |
  38. |  29 |   NESTED LOOPS     |      |       |       |       |   |      |
  39. |  30 |    NESTED LOOPS     |      |     1 |    87 |       |   401 (0)| 00:00:05 |
  40. |  31 |     NESTED LOOPS     |      |     1 |    71 |       |   399 (0)| 00:00:05 |
  41. |  32 |      TABLE ACCESS FULL     | SMSYSDATE      |     1 |     8 |       |     3 (0)| 00:00:01 |
  42. |* 33 |      TABLE ACCESS BY INDEX ROWID     | JBPM_EXT_TASK      |     1 |    63 |       |   396 (0)| 00:00:05 |
  43. |* 34 |       INDEX RANGE SCAN     | EXTTASK_OPTCODE      |   574 |       |       |     4 (0)| 00:00:01 |
  44. |* 35 |     INDEX UNIQUE SCAN     | SYS_C0011919      |     1 |       |       |     1 (0)| 00:00:01 |
  45. |* 36 |    TABLE ACCESS BY INDEX ROWID     | JBPM_PROCESSINFO      |     1 |    16 |       |     2 (0)| 00:00:01 |
  46. |  37 |  SORT AGGREGATE     |      |     1 |    98 |       |   |      |
  47. |  38 |   NESTED LOOPS     |      |       |       |       |   |      |
  48. |  39 |    NESTED LOOPS     |      |     1 |    98 |       |   401 (0)| 00:00:05 |
  49. |  40 |     NESTED LOOPS     |      |     1 |    82 |       |   399 (0)| 00:00:05 |
  50. |  41 |      TABLE ACCESS FULL     | SMSYSDATE      |     1 |     8 |       |     3 (0)| 00:00:01 |
  51. |* 42 |      TABLE ACCESS BY INDEX ROWID     | JBPM_EXT_TASK      |     1 |    74 |       |   396 (0)| 00:00:05 |
  52. |* 43 |       INDEX RANGE SCAN     | EXTTASK_OPTCODE      |   574 |       |       |     4 (0)| 00:00:01 |
  53. |* 44 |     INDEX UNIQUE SCAN     | SYS_C0011919      |     1 |       |       |     1 (0)| 00:00:01 |
  54. |* 45 |    TABLE ACCESS BY INDEX ROWID     | JBPM_PROCESSINFO      |     1 |    16 |       |     2 (0)| 00:00:01 |
  55. |  46 |  SORT AGGREGATE     |      |     1 |    98 |       |   |      |
  56. |  47 |   NESTED LOOPS     |      |       |       |       |   |      |
  57. |  48 |    NESTED LOOPS     |      |     1 |    98 |       |   401 (0)| 00:00:05 |
  58. |  49 |     NESTED LOOPS     |      |     1 |    82 |       |   399 (0)| 00:00:05 |
  59. |  50 |      TABLE ACCESS FULL     | SMSYSDATE      |     1 |     8 |       |     3 (0)| 00:00:01 |
  60. |* 51 |      TABLE ACCESS BY INDEX ROWID     | JBPM_EXT_TASK      |     1 |    74 |       |   396 (0)| 00:00:05 |
  61. |*
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值