• 异常sql处理


    下面是在awr报告里面看到的有问题的sql,是9个变量的,在应用前台属于关联查询,在sqlplus里面手工执行检查实际执行情况如下:
    1. SELECT /*+ GATHER_PLAN_STATISTICS */ *
    2. FROM SAMS_CHECKINOUT sc
    3. INNER JOIN (
    4. SELECT badgenumber,NAME,deptid
    5. FROM SAMS_USERINFO
    6. UNION
    7. SELECT badgenumber
    8. ,NAME
    9. ,deptid
    10. FROM SAMS_USERINFO_DIMISSION sd
    11. WHERE 1 = 1
    12. AND sd.deptid IN (
    13. SELECT Deptid
    14. FROM SAMS_DEPARTMENTS T2 start WITH T2.Deptid = '360710' connect BY prior T2.DEPTID = T2.SUPDEPTID
    15. )
    16. ) su ON su.badgenumber = sc.badgenumber
    17. INNER JOIN SAMS_DEPARTMENTS sd ON sd.deptid = su.deptid
    18. LEFT JOIN SAMS_ICLOCK sl ON sl.sn = sc.sn
    19. WHERE 1 = 1
    20. AND sc.checktime >= to_date('2017-03-01', 'yyyy-MM-dd')
    21. AND sc.checktime <= to_date('2017-03-22', 'yyyy-MM-dd') + 1
    22. AND sd.deptid IN (
    23. SELECT Deptid
    24. FROM SAMS_DEPARTMENTS T2 start WITH T2.Deptid = '360710' connect BY prior T2.DEPTID = T2.SUPDEPTID
    25. )
    26. AND (
    27. su.badgenumber = '36071000000600'
    28. OR su.NAME LIKE '%36071000000600%'
    29. OR sl.sn = '36071000000600'
    30. )
    31. ORDER BY sc.checktime,su.NAME DESC;
    32. /*+ GATHER_PLAN_STATISTICS */
    33. SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(NULL, NULL, 'IOSTATS'));
    34. -------------------------------------------------------------------------------------------------------------------------------------------------
    35. | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads |
    36. -------------------------------------------------------------------------------------------------------------------------------------------------
    37. | 0 | SELECT STATEMENT | | 2 | | 16 |00:00:22.70 | 18614 | 2897 |
    38. | 1 | SORT ORDER BY | | 2 | 40 | 16 |00:00:22.70 | 18614 | 2897 |
    39. |* 2 | FILTER | | 2 | | 16 |00:00:22.67 | 18614 | 2897 |
    40. |* 3 | HASH JOIN OUTER | | 2 | 40 | 10826 |00:00:22.53 | 18614 | 2897 |
    41. | 4 | NESTED LOOPS | | 2 | 802 | 10826 |00:00:17.98 | 15790 | 2897 |
    42. | 5 | NESTED LOOPS | | 2 | 1472 | 11598 |00:00:00.08 | 4216 | 45 |
    43. | 6 | NESTED LOOPS | | 2 | 92 | 2304 |00:00:00.04 | 666 | 8 |
    44. | 7 | NESTED LOOPS | | 2 | 3 | 38 |00:00:00.01 | 84 | 0 |
    45. | 8 | VIEW | VW_NSO_2 | 2 | 3 | 38 |00:00:00.01 | 28 | 0 |
    46. | 9 | HASH UNIQUE | | 2 | 3 | 38 |00:00:00.01 | 28 | 0 |
    47. |* 10 | CONNECT BY WITH FILTERING (UNIQUE) | | 2 | | 38 |00:00:00.01 | 28 | 0 |
    48. | 11 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 1 | 2 |00:00:00.01 | 6 | 0 |
    49. |* 12 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 2 | 1 | 2 |00:00:00.01 | 4 | 0 |
    50. | 13 | NESTED LOOPS | | 6 | 2 | 36 |00:00:00.01 | 22 | 0 |
    51. | 14 | CONNECT BY PUMP | | 6 | | 38 |00:00:00.01 | 0 | 0 |
    52. | 15 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 38 | 2 | 36 |00:00:00.01 | 22 | 0 |
    53. |* 16 | INDEX RANGE SCAN | SUPDEPTID_IDX | 38 | 2 | 36 |00:00:00.01 | 18 | 0 |
    54. | 17 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 38 | 1 | 38 |00:00:00.01 | 56 | 0 |
    55. |* 18 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 38 | 1 | 38 |00:00:00.01 | 18 | 0 |
    56. | 19 | VIEW | | 38 | 31 | 2304 |00:00:00.11 | 582 | 8 |
    57. | 20 | SORT UNIQUE | | 38 | | 2304 |00:00:00.11 | 582 | 8 |
    58. | 21 | UNION-ALL PARTITION | | 38 | | 2304 |00:00:00.02 | 582 | 8 |
    59. |* 22 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 38 | 68 | 1368 |00:00:00.01 | 168 | 0 |
    60. |* 23 | HASH JOIN | | 38 | 3 | 936 |00:00:00.07 | 414 | 8 |
    61. |* 24 | VIEW | VW_NSO_1 | 38 | 3 | 38 |00:00:00.01 | 288 | 0 |
    62. | 25 | SORT UNIQUE | | 38 | 3 | 722 |00:00:00.01 | 288 | 0 |
    63. |* 26 | CONNECT BY WITH FILTERING (UNIQUE)| | 38 | | 722 |00:00:00.01 | 288 | 0 |
    64. | 27 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 38 | 1 | 38 |00:00:00.01 | 86 | 0 |
    65. |* 28 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 38 | 1 | 38 |00:00:00.01 | 48 | 0 |
    66. | 29 | NESTED LOOPS | | 114 | 2 | 684 |00:00:00.01 | 202 | 0 |
    67. | 30 | CONNECT BY PUMP | | 114 | | 722 |00:00:00.01 | 0 | 0 |
    68. | 31 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 722 | 2 | 684 |00:00:00.01 | 202 | 0 |
    69. |* 32 | INDEX RANGE SCAN | SUPDEPTID_IDX | 722 | 2 | 684 |00:00:00.01 | 126 | 0 |
    70. |* 33 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 38 | 44 | 936 |00:00:00.06 | 126 | 8 |
    71. |* 34 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 2304 | 16 | 11598 |00:00:00.27 | 3550 | 37 |
    72. |* 35 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 11598 | 9 | 10826 |00:00:22.08 | 11574 | 2852 |
    73. | 36 | TABLE ACCESS FULL | SAMS_ICLOCK | 2 | 6306 | 12624 |00:00:00.08 | 2824 | 0 |
    74. -------------------------------------------------------------------------------------------------------------------------------------------------



    下面是查询到的绑定变量值,可以通过查看v$sql_bind_capture视图来查看变量的实际值,如果时间比较久,可以使用如下的语句查看历史的绑定变量信息
    1. :1 360710 VARCHAR2(32) 23-MAR-17
    2. :2 2017-03-01 VARCHAR2(32) 23-MAR-17
    3. :3 2017-03-23 VARCHAR2(32) 23-MAR-17
    4. :4 360710 VARCHAR2(32) 23-MAR-17
    5. :5 36071000000600 VARCHAR2(32) 23-MAR-17
    6. :6 %36071000000600% VARCHAR2(32) 23-MAR-17
    7. :7 36071000000600 VARCHAR2(32) 23-MAR-17
    8. :8 10 NUMBER 23-MAR-17
    9. :9 0 NUMBER 23-MAR-17
    1. select NAME,VALUE_STRING,DATATYPE_STRING,LAST_CAPTURED from dba_hist_sqlbind where sql_id='99vaabs5ptktb' and LAST_CAPTURED between
    2. to_date('2017-03-23 09:00:00','yyyy-mm-dd hh24:mi:ss') and to_date('2017-03-23 12:00:00','yyyy-mm-dd hh24:mi:ss');
    以下是开启了autotrace 选项跟踪的手工执行情况,从执行效率上看是没有问题的。
    1. ---------------------------------------------------------------------------------------------------------------------------------------------
    2. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
    3. ---------------------------------------------------------------------------------------------------------------------------------------------
    4. | 0 | SELECT STATEMENT | | 40 | 24280 | 1315 (1)| 00:00:16 | | |
    5. | 1 | SORT ORDER BY | | 40 | 24280 | 1315 (1)| 00:00:16 | | |
    6. |* 2 | FILTER | | | | | | | |
    7. |* 3 | HASH JOIN OUTER | | 40 | 24280 | 1314 (1)| 00:00:16 | | |
    8. | 4 | NESTED LOOPS | | 802 | 263K| 1231 (1)| 00:00:15 | | |
    9. | 5 | NESTED LOOPS | | 1472 | 263K| 1231 (1)| 00:00:15 | | |
    10. | 6 | NESTED LOOPS | | 92 | 18860 | 104 (1)| 00:00:02 | | |
    11. | 7 | NESTED LOOPS | | 3 | 432 | 10 (10)| 00:00:01 | | |
    12. | 8 | VIEW | VW_NSO_2 | 3 | 66 | 7 (15)| 00:00:01 | | |
    13. | 9 | HASH UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    14. |* 10 | CONNECT BY WITH FILTERING (UNIQUE) | | | | | | | |
    15. | 11 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    16. |* 12 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    17. | 13 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    18. | 14 | CONNECT BY PUMP | | | | | | | |
    19. | 15 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    20. |* 16 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    21. | 17 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 122 | 1 (0)| 00:00:01 | | |
    22. |* 18 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 0 (0)| 00:00:01 | | |
    23. | 19 | VIEW | | 31 | 1891 | 60 (0)| 00:00:01 | | |
    24. | 20 | UNION-ALL PARTITION | | | | | | | |
    25. |* 21 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 68 | 4148 | 4 (0)| 00:00:01 | | |
    26. |* 22 | HASH JOIN | | 3 | 309 | 10 (10)| 00:00:01 | | |
    27. |* 23 | VIEW | VW_NSO_1 | 3 | 126 | 7 (15)| 00:00:01 | | |
    28. | 24 | SORT UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    29. |* 25 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | | |
    30. | 26 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    31. |* 27 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    32. | 28 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    33. | 29 | CONNECT BY PUMP | | | | | | | |
    34. | 30 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    35. |* 31 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    36. |* 32 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 44 | 2684 | 3 (0)| 00:00:01 | | |
    37. |* 33 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 16 | | 3 (0)| 00:00:01 | | |
    38. |* 34 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 9 | 1179 | 19 (0)| 00:00:01 | ROWID | ROWID |
    39. | 35 | TABLE ACCESS FULL | SAMS_ICLOCK | 6306 | 1668K| 83 (0)| 00:00:02 | | |
    40. ---------------------------------------------------------------------------------------------------------------------------------------------
    41. Predicate Information (identified by operation id):
    42. ---------------------------------------------------
    43. 2 - filter("SU"."BADGENUMBER"=U'36071000000600' OR "SU"."NAME" LIKE U'%36071000000600%' AND "SU"."NAME" IS NOT NULL AND
    44. "SU"."NAME" IS NOT NULL OR "SL"."SN"=U'36071000000600')
    45. 3 - access("SL"."SN"(+)="SC"."SN")
    46. 10 - access("T2"."SUPDEPTID"=PRIOR "T2"."DEPTID")
    47. 12 - access("T2"."DEPTID"=U'360710')
    48. 16 - access("connect$_by$_pump$_017"."prior T2.DEPTID "="T2"."SUPDEPTID")
    49. 18 - access("SD"."DEPTID"="DEPTID")
    50. 21 - access("DEPTID"="SD"."DEPTID")
    51. 22 - access("SD"."DEPTID"="DEPTID")
    52. 23 - filter("DEPTID"="SD"."DEPTID")
    53. 25 - access("T2"."SUPDEPTID"=PRIOR "T2"."DEPTID")
    54. 27 - access("T2"."DEPTID"=U'360710')
    55. 31 - access("connect$_by$_pump$_006"."prior T2.DEPTID "="T2"."SUPDEPTID")
    56. 32 - access("DEPTID"="SD"."DEPTID")
    57. 33 - access("SU"."BADGENUMBER"="SC"."BADGENUMBER")
    58. 34 - filter("SC"."CHECKTIME">=TIMESTAMP' 2017-03-01 00:00:00' AND "SC"."CHECKTIME"<=TIMESTAMP' 2017-03-24 00:00:00')
    59. Statistics
    60. ----------------------------------------------------------
    61. 1 recursive calls
    62. 0 db block gets
    63. 8938 consistent gets
    64. 2467 physical reads
    65. 0 redo size
    66. 7901 bytes sent via SQL*Net to client
    67. 520 bytes received via SQL*Net from client
    68. 2 SQL*Net roundtrips to/from client
    69. 120 sorts (memory)
    70. 0 sorts (disk)
    71. 8 rows processed
    从执行计划和表的数据量等方面判断如果sql的开销有问题,应该出现在表SAMS_CHECKINOUT上面,下面检查该表上面索引的创建语句看是否有问题
    1. CREATE INDEX "SAMS"."IDX_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("BADGENUMBER")
    2. CREATE INDEX "SAMS"."IDX1_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("CHECKTIME", "BADGENUMBER", "ID")
    3. CREATE INDEX "SAMS"."INDEX_SN_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("SN")
    4. CREATE INDEX "SAMS"."IDX2_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("PROV_BRANCH_CODE")
    5. CREATE INDEX "SAMS"."IDX3_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" (TO_CHAR("CHECKTIME",'YYYY-MM'), "BADGENUMBER")
    6. CREATE INDEX "SAMS"."IDX4_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("PROV_BRANCH_CODE", "BADGENUMBER", "CHECKTIME")



     
     
    从上图可以看到,实际测试出来的执行计划跟awr报告上不同。



    现在要对sql做测试
    1. SELECT /*+ gather_plan_statistics */ *
    2. FROM (SELECT A.*, ROWNUM RN
    3. FROM (select sd.deptnumber,
    4. sd.deptname,
    5. su.badgenumber,
    6. su.name,
    7. to_char(sc.checktime, 'YYYY-MM-DD HH24:MI:SS') as CHECKTIME,
    8. case
    9. when rtrim(sc.verifycode) = '0' then
    10. '密码'
    11. when rtrim(sc.verifycode) = '1' then
    12. '指纹'
    13. when rtrim(sc.verifycode) = '2' then
    14. '补签'
    15. when rtrim(sc.verifycode) = '15' then
    16. '面部'
    17. when rtrim(sc.verifycode) = 'ZW' then
    18. '指纹'
    19. when rtrim(sc.verifycode) = 'RL' then
    20. '面部'
    21. when rtrim(sc.verifycode) = 'YD' then
    22. '移动打卡(GPS)'
    23. when rtrim(sc.verifycode) = 'EJ' then
    24. '国寿E家'
    25. end as verifycode,
    26. sl.sn || '(' || sl.alias || ')' as devicename,
    27. to_char(sc.insystime, 'YYYY-MM-DD HH24:MI:SS') as INSYSTIME
    28. from SAMS_CHECKINOUT sc
    29. inner join (select badgenumber, name, deptid
    30. from SAMS_USERINFO
    31. union all
    32. select badgenumber, name, deptid
    33. from SAMS_USERINFO_DIMISSION sd
    34. where 1 = 1
    35. and sd.deptid in
    36. (select Deptid
    37. from SAMS_DEPARTMENTS T2
    38. start with T2.Deptid = '360710'
    39. connect by prior T2.DEPTID = T2.SUPDEPTID)) su
    40. on su.badgenumber = sc.badgenumber
    41. inner join SAMS_DEPARTMENTS sd
    42. on s d.deptid = su.deptid
    43. left join SAMS_ICLOCK sl
    44. on sl.sn = sc.sn
    45. where 1 = 1
    46. and sc.checktime >= to_date('2017-03-01', 'yyyy-MM-dd')
    47. and sc.checktime <= to_date('2017-03-23', 'yyyy-MM-dd') + 1
    48. and sd.deptid in
    49. (select Deptid
    50. from SAMS_DEPARTMENTS T2
    51. start with T2.Deptid = '360710'
    52. connect by prior T2.DEPTID = T2.SUPDEPTID)
    53. and (su.badgenumber = '36071000000600' or su.name LIKE '%36071000000600%' or sl.sn = '36071000000600')
    54. order by sc.checktime, su.name desc) A
    55. WHERE ROWNUM <= 10)
    56. WHERE RN > 0;

    我们通过/*+ gather_plan_statistics */ 收集的相关执行计划及其统计信息与该SQLAWR报告中的执行计划不同,且逻辑读的数量与AWR报告中的数值也相差巨大。因此,为了更准确的判断问题,按以下方法测试。

    1、SQL在生产库(SAMS库的实例 1上,实例名为sams1 )上,在SQLPLUS中执行。

    2、执行后,在同一SQLPLUS窗口中,立即执行以下命令:

    select  * from table(dbms_xplan.display_cursor('','','allstats projection last'));

    结果如下:


    1. PLAN_TABLE_OUTPUT
    2. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    3. | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | OMem | 1Mem | Used-Mem |
    4. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    5. | 0 | SELECT STATEMENT | | 1 | | 8 |00:00:31.98 | 11130 | 3323 | | | |
    6. |* 1 | VIEW | | 1 | 2 | 8 |00:00:31.98 | 11130 | 3323 | | | |
    7. |* 2 | COUNT STOPKEY | | 1 | | 8 |00:00:31.98 | 11130 | 3323 | | | |
    8. | 3 | VIEW | | 1 | 2 | 8 |00:00:31.98 | 11130 | 3323 | | | |
    9. |* 4 | SORT ORDER BY STOPKEY | | 1 | 2 | 8 |00:00:31.98 | 11130 | 3323 | 2048 | 2048 | 2048 (0)|
    10. |* 5 | FILTER | | 1 | | 8 |00:00:31.57 | 11130 | 3323 | | | |
    11. |* 6 | HASH JOIN OUTER | | 1 | 2 | 5816 |00:00:30.90 | 11130 | 3323 | 1617K| 1078K| 1678K (0)|
    12. | 7 | NESTED LOOPS | | 1 | 802 | 5816 |00:00:33.75 | 9263 | 3320 | | | |
    13. | 8 | NESTED LOOPS | | 1 | 1472 | 6140 |00:00:00.90 | 3136 | 77 | | | |
    14. | 9 | NESTED LOOPS | | 1 | 92 | 1160 |00:00:00.11 | 353 | 18 | | | |
    15. | 10 | NESTED LOOPS | | 1 | 3 | 19 |00:00:00.01 | 43 | 0 | | | |
    16. | 11 | VIEW |VW_NSO_2 | 1 | 3 | 19 |00:00:00.01 | 15 | 0 | | | |
    17. | 12 | HASH UNIQUE | | 1 | 3 | 19 |00:00:00.01 | 15 | 0 | 1263K| 1263K| 1372K (0)|
    18. |* 13 | CONNECT BY WITH FILTERING (UNIQUE) | | 1 | | 19 |00:00:00.01 | 15 | 0 | 2048 | 2048 | 2048 (0)|
    19. | 14 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 1 | 1 |00:00:00.01 | 3 | 0 | | | |
    20. |* 15 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | 1 | 1 |00:00:00.01 | 2 | 0 | | | |
    21. | 16 | NESTED LOOPS | | 3 | 2 | 18 |00:00:00.01 | 12 | 0 | | | |
    22. | 17 | CONNECT BY PUMP | | 3 | | 19 |00:00:00.01 | 0 | 0 | | | |
    23. | 18 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 19 | 2 | 18 |00:00:00.01 | 12 | 0 | | | |
    24. |* 19 | INDEX RANGE SCAN | SUPDEPTID_IDX | 19 | 2 | 18 |00:00:00.01 | 9 | 0 | | | |
    25. | 20 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 28 | 0 | | | |
    26. |* 21 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 9 | 0 | | | |
    27. | 22 | VIEW | | 19 | 31 | 1160 |00:00:00.13 | 310 | 18 | | | |
    28. | 23 | UNION-ALL PARTITION | | 19 | | 1160 |00:00:00.13 | 310 | 18 | | | |
    29. |* 24 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 19 | 68 | 692 |00:00:00.08 | 84 | 10 | | | |
    30. |* 25 | HASH JOIN | | 19 | 3 | 468 |00:00:00.08 | 226 | 8 | 1393K| 1393K| 445K (0)|
    31. |* 26 | VIEW | VW_NSO_1 | 19 | 3 | 19 |00:00:00.01 | 163 | 0 | | | |
    32. | 27 | SORT UNIQUE | | 19 | 3 | 361 |00:00:00.01 | 163 | 0 | 2048 | 2048 | 2048 (0)|
    33. |* 28 | CONNECT BY WITH FILTERING (UNIQUE)| | 19 | | 361 |00:00:00.01 | 163 | 0 | 2048 | 2048 | 2048 (0)|
    34. | 29 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 43 | 0 | | | |
    35. |* 30 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 24 | 0 | | | |
    36. | 31 | NESTED LOOPS | | 57 | 2 | 342 |00:00:00.01 | 120 | 0 | | | |
    37. | 32 | CONNECT BY PUMP | | 57 | | 361 |00:00:00.01 | 0 | 0 | | | |
    38. | 33 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 361 | 2 | 342 |00:00:00.01 | 120 | 0 | | | |
    39. |* 34 | INDEX RANGE SCAN | SUPDEPTID_IDX | 361 | 2 | 342 |00:00:00.01 | 63 | 0 | | | |
    40. |* 35 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 19 | 44 | 468 |00:00:00.07 | 63 | 8 | | | |
    41. |* 36 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 1160 | 16 | 6140 |00:00:00.62 | 2783 | 59 | | | |
    42. |* 37 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 6140 | 9 | 5816 |00:00:30.00 | 6127 | 3243 | | | |
    43. | 38 | TABLE ACCESS FULL | SAMS_ICLOCK | 1 | 6313 | 6328 |00:00:00.03 | 1867 | 3 | | | |
    44. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    1、在目录下创建一个脚本文件,用来获取更加相信的信息。

    2、SQLPLUS中,执行以下命令:@sql_rpt  3271368959 1 24114 24115 99vaabs5ptktb 

    4、执行完成后,在该目录下生成一个HTML文档,拿到更加详细的sql统计信息附带表的数据信息

     

    注:命令参数的说明:

            3271368959  为数据库IDDBID

                              1  为 实例号instance_number

                              24114 为 快照的开始snap_id

                              24115为 快照的结束 snap_id

                 99vaabs5ptktb 为 相关SQLsql_id

     




     初步分析如下:

    1、SQL执行一次的逻辑读为11130块次,其中第37步的逻辑读为6127块次,占了一半还多。而该步的操作是根据前面的获取到的ROWID,回表SAMS_CHECKINOUT获取"SC".“CHECKTIME"[TIMESTAMP,11], "SC"."VERIFYCODE"[CHARACTER,4], "SC"."SN"[NVARCHAR2,40], "SC"."INSYSTIME"[TIMESTAMP,11]四列的内容。

    2、38步对SAMS_ICLOCK表的全表扫描,对整个SQL的逻辑读也有较大贡献。但这个不是问题的关键

     

    另外索引上有两个想法:

    1、新建组合索引或改造已有索引,按如下顺序构建组合索引:

    BADGENUMBER, CHECKTIME, SN, VERIFYCODE, INSYSTIME

    2、在表SAMS_ICLOCK上创建组合索引,列名及顺序如下:

    SN, ALIAS

     

    这两个索引先暂时不创建,先从其他方面入手


    由于在测试过程中,其生成的执行计划从未与AWR中显示的执行计划一致过。所以,这也许是造成不能模拟出2亿个块次逻辑读的一个原因。因此,把有问题的SQL的执行计划绑定到的测试SQL上。然后执行该测试SQL,并观察和分析测试SQL的执行过程和结果来做出进一步的处理。

    为完成上述想法,需要用到ORACLESQL PROFILE在不改变SQL文本的前提下,改变其执行计划。操作方法如下:

            1、在SQLPLUS中,生成问题SQL的创建SQL PROFILE的脚本。该脚本执行后,会要求分别输入SQL_IDPLAN_HASH_VALUE的值。而我们问题SQLSQL_ID99vaabs5ptktbPLAN_HASH_VALUE的值是4243346097。脚本执行完成后,会在运行SQLPLUS的当前目录中生成一个脚本文件。其名称在执行脚本过程中的结尾有显示。为描述方便,简称该生成的S脚本文件为“问题SQL脚本”。

              2、再次执行该脚本,只不过这次输入测试SQLSQL_IDPLAN_HASH_VALUE。其SQL_ID3kys9xsdjrm3bPLAN_HASH_VALUE的值为561269195。为描述方便,简称该生成的脚本文件为“测试SQL脚本”

              3、在文本编辑工具中分别打开上述两个脚本,将问题SQL脚本中出现在以下特征文字之间的文字(不包含特征文字 )复制并覆盖掉测试SQL脚本中同样位置的原文字:

             h := SYS.SQLPROF_ATTR(

            ………

            ……….

            ……….

    :signature := DBMS_SQLTUNE.SQLTEXT_TO_SIGNATURE(sql_txt);

               4、将测试SQL脚本另存为一个文件(后缀名为.sql

               5、在SQLPLUS中执行第4步另存后的脚本。

               6、在SQLPLUS中原封不动的执行原测试SQL(注:执行前设置SQLPLUS格式,以避免格式混乱。比如 set lines 200    set pagesize 100 )

               7、执行 select  * from table(dbms_xplan.display_cursor('','','allstats projection last'));

         

    如果正常生成脚本,没有报错信息出现在屏幕上,就是生成脚本成功。比如出现下面的提示就是正常的:

    Execute coe_xfr_sql_profile_99vaabs5ptktb_4243346097.sql 
    on TARGET system in order to create a custom SQL Profile 
    with plan 4243346097 linked to adjusted sql_text. 


    COE_XFR_SQL_PROFILE completed.”

     

    如果出现

    “ERROR at line 1: 
    ORA-20100: SQL_TEXT for SQL_ID 3kys9xsdjrm3b was not found in memory (gv$sqltext_with_newlines) or AWR (dba_hist_sqltext).
    ORA-06512: at line 3”

    这样的信息就是生成脚本出错了。

    错误的原因是3kys9xsdjrm3b这条SQL_IDSQL已经不在内存中,而且也没有被捕获到AWR中。解决的方法就是立即把这条SQL重跑一次,执行select  * from table(dbms_xplan.display_cursor('','','allstats projection last')); 就可以看到该sql的id和plan hash value,然后再执行这个脚本。




    我们把绑定变量替换为实际的值,运行后发现没有发生2亿多个块次的逻辑读。而且,把各种特殊值都试过,也没有重现发生2亿多个块次的逻辑读。

    在测试过程中,发现这些执行计划,和从AWR中提取出来的执行计划就没有一样过。由于计划不同,访问相关对象的方法和路径也就不同,就算是用这样的方法模拟出了2亿个块次的逻辑读,也不是我们真正要解决的那个问题。因此,目前的方向是首先模似出一致的执行计划。

    或者说,现在怀疑问题可能与执行计划有关。 简单粗暴的办法是把该SQLCURSOR清除,让数据库重新生成一个新的执行计划。这个操作之前已经做过,将整个的共享池缓存清楚,让sql重新生成执行计划,结果并没有效果。而且数据库打了最新的补丁,也同样没有效果,基本可以排除BUG的顾虑。

                   



    我之前操作有误,把执行计划固定到问题sql上了,现在删除掉了

    1. SQL> SELECT name from dba_sql_profiles;
    2. NAME
    3. ------------------------------
    4. coe_99vaabs5ptktb_4243346097
    5. SQL>  BEGIN 
    6.   2    DBMS_SQLTUNE.DROP_SQL_PROFILE(name => 'coe_99vaabs5ptktb_4243346097'); 
    7.   3  END; 
    8.   4  /
    9. PL/SQL procedure successfully completed.
    10. SQL>  SELECT name, created,category,sql_Text from dba_sql_profiles ORDER BY created DESC;
    11. no rows selected

    2017年3月29日 星期三 16:06:42
    1. SQL>SELECT /*+ gather_plan_statistics */ * FROM (SELECT A.*, ROWNUM RN
    2. FROM (select sd.deptnumber, 2
    3. 3 sd.deptname,
    4. 4 su.badgenumber,
    5. 5 su.name,
    6. 6 to_char(sc.checktime, 'YYYY-MM-DD HH24:MI:SS') as CHECKTIME,
    7. 7 case
    8. 8 when rtrim(sc.verifycode) = '0' then 'A'
    9. 9 when rtrim(sc.verifycode) = '1' then 'B'
    10. 10 when rtrim(sc.verifycode) = '2' then 'D'
    11. 11 when rtrim(sc.verifycode) = '15' then 'C'
    12. 12 when rtrim(sc.verifycode) = 'ZW' then 'B'
    13. 13 when rtrim(sc.verifycode) = 'RL' then 'C'
    14. 14 when rtrim(sc.verifycode) = 'YD' then 'E'
    15. 15 when rtrim(sc.verifycode) = 'EJ' then 'F'
    16. 16 end as verifycode,
    17. 17 sl.sn || '(' || sl.alias || ')' as devicename,
    18. 18 to_char(sc.insystime, 'YYYY-MM-DD HH24:MI:SS') as INSYSTIME
    19. 19 from SAMS_CHECKINOUT sc
    20. 20 inner join (select badgenumber, name, deptid
    21. 21 from SAMS_USERINFO
    22. 22 union all
    23. 23 select badgenumber, name, deptid
    24. 24 from SAMS_USERINFO_DIMISSION sd
    25. 25 where 1 = 1
    26. 26 and sd.deptid in
    27. 27 (select Deptid
    28. 28 from SAMS_DEPARTMENTS T2
    29. 29 start with T2.Deptid = '360710'
    30. 30 connect by prior T2.DEPTID = T2.SUPDEPTID)) su
    31. 31 on su.badgenumber = sc.badgenumber
    32. 32 inner join SAMS_DEPARTMENTS sd
    33. 33 on sd.deptid = su.deptid
    34. 34 left join SAMS_ICLOCK sl
    35. 35 on sl.sn = sc.sn
    36. 36 where 1 = 1
    37. 37 and sc.checktime >= to_date('2017-03-01', 'yyyy-MM-dd')
    38. 38 and sc.checktime <= to_date('2017-03-23', 'yyyy-MM-dd') + 1
    39. 39 and sd.deptid in
    40. 40 (select Deptid
    41. 41 from SAMS_DEPARTMENTS T2
    42. 42 start with T2.Deptid = '360710'
    43. 43 connect by prior T2.DEPTID = T2.SUPDEPTID)
    44. 44 and (su.badgenumber = '36071000000600' or su.name LIKE '%36071000000600%' or sl.sn = '36071000000600')
    45. 45 order by sc.checktime, su.name desc) A
    46. 46 WHERE ROWNUM <= 10)
    47. 47 WHERE RN > 0;
    48. select * from table(dbms_xplan.display_cursor('','','allstats projection last'));
    49. DEPTNUMBER
    50. --------------------------------------------------------------------------------
    51. DEPTNAME
    52. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    53. BADGENUMBER NAME CHECKTIME V
    54. ------------------------------------------------ ------------------------------------------------ ------------------- -
    55. DEVICENAME INSYSTIME RN
    56. ---------------------------------------------------------------------------------------------------------------------------- ------------------- ----------
    57. 360710002002
    58. 黄陂营销服务部职场
    59. 36071000000600 张小明 2017-03-04 08:05:01 B
    60. 6538992529510(宁都) 2017-03-04 08:05:02 1
    61. 360710002002
    62. 黄陂营销服务部职场
    63. 36071000000600 张小明 2017-03-06 09:19:32 B
    64. 6538992529510(宁都) 2017-03-06 09:19:31 2
    65. 360710002002
    66. 黄陂营销服务部职场
    67. 36071000000600 张小明 2017-03-12 09:24:00 B
    68. 6538992529510(宁都) 2017-03-12 09:23:59 3
    69. 360710002002
    70. 黄陂营销服务部职场
    71. 36071000000600 张小明 2017-03-15 09:14:30 B
    72. 6538992529510(宁都) 2017-03-15 09:14:29 4
    73. 360710002002
    74. 黄陂营销服务部职场
    75. 36071000000600 张小明 2017-03-16 08:22:50 B
    76. 6538992529510(宁都) 2017-03-16 08:22:50 5
    77. 360710002002
    78. 黄陂营销服务部职场
    79. 36071000000600 张小明 2017-03-16 09:59:05 B
    80. 6538992529510(宁都) 2017-03-16 09:59:04 6
    81. 360710002002
    82. 黄陂营销服务部职场
    83. 36071000000600 张小明 2017-03-18 08:55:11 B
    84. 6538992529510(宁都) 2017-03-18 08:55:09 7
    85. 360710002002
    86. 黄陂营销服务部职场
    87. 36071000000600 张小明 2017-03-21 14:57:57 B
    88. 6538992529510(宁都) 2017-03-21 14:57:57 8
    89. 8 rows selected.
    90. SQL>
    91. PLAN_TABLE_OUTPUT
    92. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    93. SQL_ID bw0b67268pva8, child number 1
    94. -------------------------------------
    95. SELECT /*+ gather_plan_statistics */ * FROM (SELECT A.*, ROWNUM RN
    96. FROM (select sd.deptnumber, sd.deptname,
    97. su.badgenumber, su.name,
    98. to_char(sc.checktime, 'YYYY-MM-DD HH24:MI:SS') as
    99. CHECKTIME, case when
    100. rtrim(sc.verifycode) = '0' then 'A' when
    101. rtrim(sc.verifycode) = '1' then 'B' when
    102. rtrim(sc.verifycode) = '2' then 'D' when
    103. rtrim(sc.verifycode) = '15' then 'C' when
    104. rtrim(sc.verifycode) = 'ZW' then 'B' when
    105. rtrim(sc.verifycode) = 'RL' then 'C' when
    106. rtrim(sc.verifycode) = 'YD' then 'E' when
    107. rtrim(sc.verifycode) = 'EJ' then 'F' end as
    108. verifycode, sl.sn || '(' || sl.alias || ')' as
    109. devicename, to_char
    110. Plan hash value: 3623936353
    111. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    112. | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | OMem | 1Mem | Used-Mem |
    113. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    114. | 0 | SELECT STATEMENT | | 1 | | 8 |00:00:00.15 | 11988 | 1 | | | |
    115. |* 1 | VIEW | | 1 | 10 | 8 |00:00:00.15 | 11988 | 1 | | | |
    116. |* 2 | COUNT STOPKEY | | 1 | | 8 |00:00:00.15 | 11988 | 1 | | | |
    117. | 3 | VIEW | | 1 | 14 | 8 |00:00:00.15 | 11988 | 1 | | | |
    118. |* 4 | SORT ORDER BY STOPKEY | | 1 | 14 | 8 |00:00:00.15 | 11988 | 1 | 2048 | 2048 | 2048 (0)|
    119. |* 5 | FILTER | | 1 | | 8 |00:00:00.07 | 11988 | 1 | | | |
    120. |* 6 | HASH JOIN RIGHT OUTER | | 1 | 14 | 5818 |00:00:00.12 | 11988 | 1 | 1361K| 1361K| 1487K (0)|
    121. | 7 | TABLE ACCESS FULL | SAMS_ICLOCK | 1 | 6332 | 6336 |00:00:00.07 | 376 | 0 | | | |
    122. | 8 | NESTED LOOPS | | 1 | 5442 | 5818 |00:00:00.04 | 11612 | 1 | | | |
    123. | 9 | NESTED LOOPS | | 1 | 11210 | 8062 |00:00:00.01 | 3593 | 0 | | | |
    124. | 10 | NESTED LOOPS | | 1 | 590 | 1174 |00:00:00.01 | 374 | 0 | | | |
    125. | 11 | NESTED LOOPS | | 1 | 19 | 19 |00:00:00.01 | 44 | 0 | | | |
    126. | 12 | VIEW | VW_NSO_2 | 1 | 19 | 19 |00:00:00.01 | 16 | 0 | | | |
    127. | 13 | HASH UNIQUE | | 1 | 19 | 19 |00:00:00.01 | 16 | 0 | 1263K| 1263K| 1369K (0)|
    128. |* 14 | CONNECT BY WITH FILTERING (UNIQUE) | | 1 | | 19 |00:00:00.01 | 16 | 0 | 2048 | 2048 | 2048 (0)|
    129. | 15 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 1 | 1 |00:00:00.01 | 3 | 0 | | | |
    130. |* 16 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | 1 | 1 |00:00:00.01 | 2 | 0 | | | |
    131. | 17 | NESTED LOOPS | | 3 | 2 | 18 |00:00:00.01 | 13 | 0 | | | |
    132. | 18 | CONNECT BY PUMP | | 3 | | 19 |00:00:00.01 | 0 | 0 | | | |
    133. | 19 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 19 | 2 | 18 |00:00:00.01 | 13 | 0 | | | |
    134. |* 20 | INDEX RANGE SCAN | SUPDEPTID_IDX | 19 | 2 | 18 |00:00:00.01 | 9 | 0 | | | |
    135. | 21 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 28 | 0 | | | |
    136. |* 22 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 9 | 0 | | | |
    137. | 23 | VIEW | | 19 | 31 | 1174 |00:00:00.01 | 330 | 0 | | | |
    138. | 24 | UNION-ALL PARTITION | | 19 | | 1174 |00:00:00.01 | 330 | 0 | | | |
    139. |* 25 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 19 | 69 | 706 |00:00:00.01 | 85 | 0 | | | |
    140. |* 26 | HASH JOIN | | 19 | 3 | 468 |00:00:00.01 | 245 | 0 | 1393K| 1393K| 716K (0)|
    141. |* 27 | VIEW | VW_NSO_1 | 19 | 3 | 19 |00:00:00.01 | 182 | 0 | | | |
    142. | 28 | SORT UNIQUE | | 19 | 3 | 361 |00:00:00.01 | 182 | 0 | 2048 | 2048 | 2048 (0)|
    143. |* 29 | CONNECT BY WITH FILTERING (UNIQUE)| | 19 | | 361 |00:00:00.01 | 182 | 0 | 2048 | 2048 | 2048 (0)|
    144. | 30 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 43 | 0 | | | |
    145. |* 31 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 19 | 1 | 19 |00:00:00.01 | 24 | 0 | | | |
    146. | 32 | NESTED LOOPS | | 57 | 2 | 342 |00:00:00.01 | 139 | 0 | | | |
    147. | 33 | CONNECT BY PUMP | | 57 | | 361 |00:00:00.01 | 0 | 0 | | | |
    148. | 34 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 361 | 2 | 342 |00:00:00.01 | 139 | 0 | | | |
    149. |* 35 | INDEX RANGE SCAN | SUPDEPTID_IDX | 361 | 2 | 342 |00:00:00.01 | 63 | 0 | | | |
    150. |* 36 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 19 | 45 | 468 |00:00:00.01 | 63 | 0 | | | |
    151. |* 37 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 1174 | 19 | 8062 |00:00:00.01 | 3219 | 0 | | | |
    152. |* 38 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 8062 | 9 | 5818 |00:00:00.06 | 8019 | 1 | | | |
    153. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    154. Predicate Information (identified by operation id):
    155. ---------------------------------------------------
    156. 1 - filter("RN">0)
    157. 2 - filter(ROWNUM<=10)
    158. 4 - filter(ROWNUM<=10)
    159. 5 - filter(("SU"."BADGENUMBER"=U'36071000000600' OR ("SU"."NAME" LIKE U'%36071000000600%' AND "SU"."NAME" IS NOT NULL AND "SU"."NAME" IS NOT NULL) OR
    160. "SL"."SN"=U'36071000000600'))
    161. 6 - access("SL"."SN"="SC"."SN")
    162. 14 - access("T2"."SUPDEPTID"=PRIOR NULL)
    163. 16 - access("T2"."DEPTID"=U'360710')
    164. 20 - access("connect$_by$_pump$_019"."prior T2.DEPTID "="T2"."SUPDEPTID")
    165. 22 - access("SD"."DEPTID"="DEPTID")
    166. 25 - access("DEPTID"="SD"."DEPTID")
    167. 26 - access("SD"."DEPTID"="DEPTID")
    168. 27 - filter("DEPTID"="SD"."DEPTID")
    169. 29 - access("T2"."SUPDEPTID"=PRIOR NULL)
    170. 31 - access("T2"."DEPTID"=U'360710')
    171. 35 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    172. 36 - access("DEPTID"="SD"."DEPTID")
    173. 37 - access("SU"."BADGENUMBER"="SC"."BADGENUMBER")
    174. 38 - filter(("SC"."CHECKTIME"<=TIMESTAMP' 2017-03-24 00:00:00' AND "SC"."CHECKTIME">=TIMESTAMP' 2017-03-01 00:00:00'))
    175. Column Projection Information (identified by operation id):
    176. -----------------------------------------------------------
    177. 1 - "from$_subquery$_001"."DEPTNUMBER"[NVARCHAR2,80], "from$_subquery$_001"."DEPTNAME"[NVARCHAR2,400], "from$_subquery$_001"."BADGENUMBER"[NVARCHAR2,48],
    178. "from$_subquery$_001"."NAME"[NVARCHAR2,48], "from$_subquery$_001"."CHECKTIME"[VARCHAR2,19], "from$_subquery$_001"."VERIFYCODE"[CHARACTER,1],
    179. "from$_subquery$_001"."DEVICENAME"[NVARCHAR2,124], "from$_subquery$_001"."INSYSTIME"[VARCHAR2,19], "RN"[NUMBER,22]
    180. 2 - "A"."DEPTNUMBER"[NVARCHAR2,80], "A"."DEPTNAME"[NVARCHAR2,400], "A"."BADGENUMBER"[NVARCHAR2,48], "A"."NAME"[NVARCHAR2,48], "A"."CHECKTIME"[VARCHAR2,19],
    181. "A"."VERIFYCODE"[CHARACTER,1], "A"."DEVICENAME"[NVARCHAR2,124], "A"."INSYSTIME"[VARCHAR2,19], ROWNUM[4]
    182. 3 - "A"."DEPTNUMBER"[NVARCHAR2,80], "A"."DEPTNAME"[NVARCHAR2,400], "A"."BADGENUMBER"[NVARCHAR2,48], "A"."NAME"[NVARCHAR2,48], "A"."CHECKTIME"[VARCHAR2,19],
    183. "A"."VERIFYCODE"[CHARACTER,1], "A"."DEVICENAME"[NVARCHAR2,124], "A"."INSYSTIME"[VARCHAR2,19]
    184. 4 - (#keys=2) "SC"."CHECKTIME"[TIMESTAMP,11], INTERNAL_FUNCTION("SU"."NAME")[48], "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400],
    185. "SU"."BADGENUMBER"[NVARCHAR2,48], TO_CHAR(INTERNAL_FUNCTION("SC"."INSYSTIME"),'YYYY-MM-DD HH24:MI:SS')[19], TO_CHAR(INTERNAL_FUNCTION("SC"."CHECKTIME"),'YYYY-MM-DD
    186. HH24:MI:SS')[19], CASE RTRIM("SC"."VERIFYCODE") WHEN '0' THEN 'A' WHEN '1' THEN 'B' WHEN '2' THEN 'D' WHEN '15' THEN 'C' WHEN 'ZW' THEN 'B' WHEN 'RL' THEN 'C' WHEN
    187. 'YD' THEN 'E' WHEN 'EJ' THEN 'F' END [1], "SL"."SN"||U'('||"SL"."ALIAS"||U')'[124]
    188. 5 - "SL"."SN"[NVARCHAR2,40], "SL"."ALIAS"[NVARCHAR2,80], "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400], "SU"."BADGENUMBER"[NVARCHAR2,48],
    189. "SU"."NAME"[NVARCHAR2,48], "SC"."CHECKTIME"[TIMESTAMP,11], "SC"."VERIFYCODE"[CHARACTER,4], "SC"."INSYSTIME"[TIMESTAMP,11]
    190. 6 - (#keys=1) "SL"."SN"[NVARCHAR2,40], "SL"."ALIAS"[NVARCHAR2,80], "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400],
    191. "SU"."BADGENUMBER"[NVARCHAR2,48], "SU"."NAME"[NVARCHAR2,48], "SC"."CHECKTIME"[TIMESTAMP,11], "SC"."VERIFYCODE"[CHARACTER,4], "SC"."INSYSTIME"[TIMESTAMP,11]
    192. 7 - "SL"."SN"[NVARCHAR2,40], "SL"."ALIAS"[NVARCHAR2,80]
    193. 8 - "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400], "SU"."BADGENUMBER"[NVARCHAR2,48], "SU"."NAME"[NVARCHAR2,48], "SC"."CHECKTIME"[TIMESTAMP,11],
    194. "SC"."VERIFYCODE"[CHARACTER,4], "SC"."SN"[NVARCHAR2,40], "SC"."INSYSTIME"[TIMESTAMP,11]
    195. 9 - "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400], "SU"."BADGENUMBER"[NVARCHAR2,48], "SU"."NAME"[NVARCHAR2,48], "SC".ROWID[ROWID,10]
    196. 10 - "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400], "SU"."BADGENUMBER"[NVARCHAR2,48], "SU"."NAME"[NVARCHAR2,48]
    197. 11 - "SD"."DEPTID"[NVARCHAR2,80], "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400]
    198. 12 - "DEPTID"[NVARCHAR2,80]
    199. 13 - "DEPTID"[NVARCHAR2,80]
    200. 14 - "T2"."SUPDEPTID"[NVARCHAR2,80], "T2"."DEPTID"[NVARCHAR2,80], PRIOR NULL[80], LEVEL[4]
    201. 15 - "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80], "T2"."SUPDEPTID"[NVARCHAR2,80]
    202. 16 - "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80]
    203. 17 - "connect$_by$_pump$_019"."prior T2.DEPTID "[NVARCHAR2,80], "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80], "T2"."SUPDEPTID"[NVARCHAR2,80]
    204. 18 - "connect$_by$_pump$_019"."prior T2.DEPTID "[NVARCHAR2,80]
    205. 19 - "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80], "T2"."SUPDEPTID"[NVARCHAR2,80]
    206. 20 - "T2".ROWID[ROWID,10], "T2"."SUPDEPTID"[NVARCHAR2,80]
    207. 21 - "SD"."DEPTID"[NVARCHAR2,80], "SD"."DEPTNUMBER"[NVARCHAR2,80], "SD"."DEPTNAME"[NVARCHAR2,400]
    208. 22 - "SD".ROWID[ROWID,10], "SD"."DEPTID"[NVARCHAR2,80]
    209. 23 - "SU"."BADGENUMBER"[NVARCHAR2,48], "SU"."NAME"[NVARCHAR2,48]
    210. 24 - STRDEF[48], STRDEF[48], STRDEF[80]
    211. 25 - "BADGENUMBER"[NVARCHAR2,48], "NAME"[NVARCHAR2,48], "DEPTID"[NVARCHAR2,80]
    212. 26 - (#keys=1) "SD"."DEPTID"[NVARCHAR2,80], "BADGENUMBER"[NVARCHAR2,48], "NAME"[NVARCHAR2,48]
    213. 27 - "DEPTID"[NVARCHAR2,80]
    214. 28 - (#keys=1) "DEPTID"[NVARCHAR2,80]
    215. 29 - "T2"."SUPDEPTID"[NVARCHAR2,80], "T2"."DEPTID"[NVARCHAR2,80], PRIOR NULL[80], LEVEL[4]
    216. 30 - "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80], "T2"."SUPDEPTID"[NVARCHAR2,80]
    217. 31 - "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80]
    218. 32 - "connect$_by$_pump$_008"."prior T2.DEPTID "[NVARCHAR2,80], "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80], "T2"."SUPDEPTID"[NVARCHAR2,80]
    219. 33 - "connect$_by$_pump$_008"."prior T2.DEPTID "[NVARCHAR2,80]
    220. 34 - "T2".ROWID[ROWID,10], "T2"."DEPTID"[NVARCHAR2,80], "T2"."SUPDEPTID"[NVARCHAR2,80]
    221. 35 - "T2".ROWID[ROWID,10], "T2"."SUPDEPTID"[NVARCHAR2,80]
    222. 36 - "BADGENUMBER"[NVARCHAR2,48], "NAME"[NVARCHAR2,48], "SD"."DEPTID"[NVARCHAR2,80]
    223. 37 - "SC".ROWID[ROWID,10]
    224. 38 - "SC"."CHECKTIME"[TIMESTAMP,11], "SC"."VERIFYCODE"[CHARACTER,4], "SC"."SN"[NVARCHAR2,40], "SC"."INSYSTIME"[TIMESTAMP,11]
    按照之前的方法重新测试,执行计划还是跟awr上面不一样,手工绑sqlprofile也不行,再寻找其他出路。

    2017年3月30日 星期四 9:54:28

    使用oracle的sql 自动优化功能,看看是否可以模拟出跟awr报告上面一样的执行计划,看看其结果如何:
    1. GENERAL INFORMATION SECTION
    2. -------------------------------------------------------------------------------
    3. Tuning Task Name : TASK_88857
    4. Tuning Task Owner : SYS
    5. Workload Type : Single SQL Statement
    6. Scope : COMPREHENSIVE
    7. Time Limit(seconds): 1800
    8. Completion Status : COMPLETED
    9. Started at : 03/30/2017 09:20:25
    10. Completed at : 03/30/2017 09:22:27
    11. -------------------------------------------------------------------------------
    12. Schema Name: SAMS
    13. SQL ID : 99vaabs5ptktb
    14. SQL Text : SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (select
    15. sd.deptnumber,sd.deptname,su.badgenumber,su.name,to_char(sc.check
    16. time,'YYYY-MM-DD HH24:MI:SS') as CHECKTIME,case when
    17. rtrim(sc.verifycode)='0' then '密码' when rtrim(sc.verifycode)='1'
    18. then '指纹' when rtrim(sc.verifycode)='2' then '补签' when
    19. rtrim(sc.verifycode)='15' then '面部' when
    20. rtrim(sc.verifycode)='ZW' then '指纹' when
    21. rtrim(sc.verifycode)='RL' then '面部' when
    22. rtrim(sc.verifycode)='YD' then '移动打卡(GPS)' when
    23. rtrim(sc.verifycode)='EJ' then '国寿E家' end as
    24. verifycode,sl.sn||'('||sl.alias||')' as
    25. devicename,to_char(sc.insystime,'YYYY-MM-DD HH24:MI:SS') as
    26. INSYSTIME from SAMS_CHECKINOUT sc inner join (select
    27. badgenumber, name, deptid from SAMS_USERINFO union all select
    28. badgenumber, name, deptid from SAMS_USERINFO_DIMISSION sd where
    29. 1=1 and sd.deptid in(select Deptid from SAMS_DEPARTMENTS T2
    30. start with T2.Deptid = :1 connect by prior T2.DEPTID
    31. =T2.SUPDEPTID)) su on su.badgenumber = sc.badgenumber inner join
    32. SAMS_DEPARTMENTS sd on sd.deptid = su.deptid left join
    33. SAMS_ICLOCK sl on sl.sn=sc.sn where 1=1 and sc.checktime>=
    34. to_date(:2 ,'yyyy-MM-dd') and sc.checktime<= to_date(:3
    35. ,'yyyy-MM-dd')+1 and sd.deptid in(select Deptid from
    36. SAMS_DEPARTMENTS T2 start with T2.Deptid = :4 connect by prior
    37. T2.DEPTID =T2.SUPDEPTID) and (su.badgenumber=:5 or su.name LIKE
    38. :6 or sl.sn=:7 ) order by sc.checktime,su.name desc ) A WHERE
    39. ROWNUM <= :8 ) WHERE RN > :9
    40. Bind Variables :
    41. 1 - (VARCHAR2(32)):350627
    42. 2 - (VARCHAR2(32)):2017-03-01
    43. 3 - (VARCHAR2(32)):2017-03-30
    44. 4 - (VARCHAR2(32)):350627
    45. 5 - (VARCHAR2(32)):35062700001791
    46. 6 - (VARCHAR2(32)):%35062700001791%
    47. 7 - (VARCHAR2(32)):35062700001791
    48. 8 - (NUMBER):10
    49. 9 - (NUMBER):0
    50. -------------------------------------------------------------------------------
    51. FINDINGS SECTION (1 finding)
    52. -------------------------------------------------------------------------------
    53. 1- Alternative Plan Finding
    54. ---------------------------
    55. Some alternative execution plans for this statement were found by searching
    56. the system's real-time and historical performance data.
    57. The following table lists these plans ranked by their average elapsed time.
    58. See section "ALTERNATIVE PLANS SECTION" for detailed information on each
    59. plan.
    60. id plan hash last seen elapsed (s) origin note
    61. -- ---------- -------------------- ------------ --------------- ----------------
    62. 1 3018912096 2017-03-30/09:20:49 0.203 Cursor Cache original plan
    63. 2 205839464 2017-03-28/12:00:08 1.492 AWR
    64. 3 4243346097 2017-03-30/08:00:34 33323.697 AWR
    65. Information
    66. -----------
    67. - The Original Plan appears to have the best performance, based on the
    68. elapsed time per execution. However, if you know that one alternative
    69. plan is better than the Original Plan, you can create a SQL plan baseline
    70. for it. This will instruct the Oracle optimizer to pick it over any other
    71. choices in the future.
    72. execute dbms_sqltune.create_sql_plan_baseline(task_name => 'TASK_88857',
    73. owner_name => 'SYS', plan_hash_value => xxxxxxxx);
    74. -------------------------------------------------------------------------------
    75. ADDITIONAL INFORMATION SECTION
    76. -------------------------------------------------------------------------------
    77. - The optimizer could not merge the view at line ID 3 of the execution plan.
    78. The optimizer cannot merge a view that contains an "ORDER BY" clause unless
    79. the statement is a "DELETE" or an "UPDATE" and the parent query is the top
    80. most query in the statement.
    81. - The optimizer could not merge the view at line ID 1 of the execution plan.
    82. The optimizer cannot merge a view that contains a "ROWNUM" pseudo column.
    83. -------------------------------------------------------------------------------
    84. EXPLAIN PLANS SECTION
    85. -------------------------------------------------------------------------------
    86. 1- Original
    87. -----------
    88. Plan hash value: 3018912096
    89. ------------------------------------------------------------------------------------------------------------------------------------------------
    90. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
    91. ------------------------------------------------------------------------------------------------------------------------------------------------
    92. | 0 | SELECT STATEMENT | | 3 | 1209 | 1604 (1)| 00:00:20 | | |
    93. |* 1 | VIEW | | 3 | 1209 | 1604 (1)| 00:00:20 | | |
    94. |* 2 | COUNT STOPKEY | | | | | | | |
    95. | 3 | VIEW | | 3 | 1170 | 1604 (1)| 00:00:20 | | |
    96. |* 4 | SORT ORDER BY STOPKEY | | 3 | 804 | 1604 (1)| 00:00:20 | | |
    97. |* 5 | FILTER | | | | | | | |
    98. |* 6 | HASH JOIN OUTER | | 3 | 804 | 1603 (1)| 00:00:20 | | |
    99. | 7 | NESTED LOOPS | | 1088 | 233K| 1520 (1)| 00:00:19 | | |
    100. | 8 | NESTED LOOPS | | 1767 | 233K| 1520 (1)| 00:00:19 | | |
    101. | 9 | NESTED LOOPS | | 93 | 14043 | 104 (1)| 00:00:02 | | |
    102. | 10 | NESTED LOOPS | | 3 | 270 | 10 (10)| 00:00:01 | | |
    103. | 11 | VIEW | VW_NSO_2 | 3 | 66 | 7 (15)| 00:00:01 | | |
    104. | 12 | HASH UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    105. |* 13 | FILTER | | | | | | | |
    106. |* 14 | CONNECT BY WITH FILTERING (UNIQUE) | | | | | | | |
    107. | 15 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    108. |* 16 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    109. | 17 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    110. | 18 | CONNECT BY PUMP | | | | | | | |
    111. | 19 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    112. |* 20 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    113. | 21 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 68 | 1 (0)| 00:00:01 | | |
    114. |* 22 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 0 (0)| 00:00:01 | | |
    115. | 23 | VIEW | | 31 | 1891 | 60 (0)| 00:00:01 | | |
    116. | 24 | UNION-ALL PARTITION | | | | | | | |
    117. |* 25 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 69 | 4209 | 4 (0)| 00:00:01 | | |
    118. |* 26 | HASH JOIN | | 3 | 309 | 10 (10)| 00:00:01 | | |
    119. |* 27 | VIEW | VW_NSO_1 | 3 | 126 | 7 (15)| 00:00:01 | | |
    120. | 28 | SORT UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    121. |* 29 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | | |
    122. | 30 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    123. |* 31 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    124. | 32 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    125. | 33 | CONNECT BY PUMP | | | | | | | |
    126. | 34 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    127. |* 35 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    128. |* 36 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 46 | 2806 | 3 (0)| 00:00:01 | | |
    129. |* 37 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 19 | | 3 (0)| 00:00:01 | | |
    130. |* 38 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 12 | 828 | 23 (0)| 00:00:01 | ROWID | ROWID |
    131. | 39 | TABLE ACCESS FULL | SAMS_ICLOCK | 6337 | 297K| 83 (0)| 00:00:01 | | |
    132. ------------------------------------------------------------------------------------------------------------------------------------------------
    133. Predicate Information (identified by operation id):
    134. ---------------------------------------------------
    135. 1 - filter("RN">:9)
    136. 2 - filter(ROWNUM<=:8)
    137. 4 - filter(ROWNUM<=:8)
    138. 5 - filter("SU"."BADGENUMBER"=SYS_OP_C2C(:5) OR "SU"."NAME" LIKE SYS_OP_C2C(:6) OR "SL"."SN"=SYS_OP_C2C(:7))
    139. 6 - access("SL"."SN"(+)="SC"."SN")
    140. 13 - filter(TO_DATE(:3,'yyyy-MM-dd')+1>=TO_DATE(:2,'yyyy-MM-dd'))
    141. 14 - access("T2"."SUPDEPTID"=PRIOR NULL)
    142. 16 - access("T2"."DEPTID"=SYS_OP_C2C(:4))
    143. 20 - access("connect$_by$_pump$_019"."prior T2.DEPTID "="T2"."SUPDEPTID")
    144. 22 - access("SD"."DEPTID"="DEPTID")
    145. 25 - access("DEPTID"="SD"."DEPTID")
    146. 26 - access("SD"."DEPTID"="DEPTID")
    147. 27 - filter("DEPTID"="SD"."DEPTID")
    148. 29 - access("T2"."SUPDEPTID"=PRIOR NULL)
    149. 31 - access("T2"."DEPTID"=SYS_OP_C2C(:1))
    150. 35 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    151. 36 - access("DEPTID"="SD"."DEPTID")
    152. 37 - access("SU"."BADGENUMBER"="SC"."BADGENUMBER")
    153. 38 - filter("SC"."CHECKTIME">=TO_DATE(:2,'yyyy-MM-dd') AND "SC"."CHECKTIME"<=TO_DATE(:3,'yyyy-MM-dd')+1)
    154. -------------------------------------------------------------------------------
    155. ALTERNATIVE PLANS SECTION
    156. -------------------------------------------------------------------------------
    157. Plan 1
    158. ------
    159. Plan Origin :Cursor Cache
    160. Plan Hash Value :3018912096
    161. Executions :39
    162. Elapsed Time :0.203 sec
    163. CPU Time :0.127 sec
    164. Buffer Gets :27754
    165. Disk Reads :38
    166. Disk Writes :0
    167. Notes:
    168. 1. Statistics shown are averaged over multiple executions.
    169. 2. The plan matches the original plan.
    170. ------------------------------------------------------------------------------------------------------------------------------------------------
    171. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
    172. ------------------------------------------------------------------------------------------------------------------------------------------------
    173. | 0 | SELECT STATEMENT | | 10 | 4030 | 1604 (1)| 00:00:20 | | |
    174. |* 1 | VIEW | | 10 | 4030 | 1604 (1)| 00:00:20 | | |
    175. |* 2 | COUNT STOPKEY | | | | | | | |
    176. | 3 | VIEW | | 55 | 21450 | 1604 (1)| 00:00:20 | | |
    177. |* 4 | SORT ORDER BY STOPKEY | | 55 | 14740 | 1604 (1)| 00:00:20 | | |
    178. |* 5 | FILTER | | | | | | | |
    179. |* 6 | HASH JOIN OUTER | | 55 | 14740 | 1603 (1)| 00:00:20 | | |
    180. | 7 | NESTED LOOPS | | 1088 | 233K| 1520 (1)| 00:00:19 | | |
    181. | 8 | NESTED LOOPS | | 1767 | 233K| 1520 (1)| 00:00:19 | | |
    182. | 9 | NESTED LOOPS | | 93 | 14043 | 104 (1)| 00:00:02 | | |
    183. | 10 | NESTED LOOPS | | 3 | 270 | 10 (10)| 00:00:01 | | |
    184. | 11 | VIEW | VW_NSO_2 | 3 | 66 | 7 (15)| 00:00:01 | | |
    185. | 12 | HASH UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    186. |* 13 | FILTER | | | | | | | |
    187. |* 14 | CONNECT BY WITH FILTERING (UNIQUE) | | | | | | | |
    188. | 15 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    189. |* 16 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    190. | 17 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    191. | 18 | CONNECT BY PUMP | | | | | | | |
    192. | 19 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    193. |* 20 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    194. | 21 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 68 | 1 (0)| 00:00:01 | | |
    195. |* 22 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 0 (0)| 00:00:01 | | |
    196. | 23 | VIEW | | 31 | 1891 | 60 (0)| 00:00:01 | | |
    197. | 24 | UNION-ALL PARTITION | | | | | | | |
    198. |* 25 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 69 | 4209 | 4 (0)| 00:00:01 | | |
    199. |* 26 | HASH JOIN | | 3 | 309 | 10 (10)| 00:00:01 | | |
    200. |* 27 | VIEW | VW_NSO_1 | 3 | 126 | 7 (15)| 00:00:01 | | |
    201. | 28 | SORT UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    202. |* 29 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | | |
    203. | 30 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    204. |* 31 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    205. | 32 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    206. | 33 | CONNECT BY PUMP | | | | | | | |
    207. | 34 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    208. |* 35 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    209. |* 36 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 46 | 2806 | 3 (0)| 00:00:01 | | |
    210. |* 37 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 19 | | 3 (0)| 00:00:01 | | |
    211. |* 38 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 12 | 828 | 23 (0)| 00:00:01 | ROWID | ROWID |
    212. | 39 | TABLE ACCESS FULL | SAMS_ICLOCK | 6337 | 297K| 83 (0)| 00:00:01 | | |
    213. ------------------------------------------------------------------------------------------------------------------------------------------------
    214. Predicate Information (identified by operation id):
    215. ---------------------------------------------------
    216. 1 - filter("RN">:9)
    217. 2 - filter(ROWNUM<=:8)
    218. 4 - filter(ROWNUM<=:8)
    219. 5 - filter("SU"."BADGENUMBER"=SYS_OP_C2C(:5) OR "SU"."NAME" LIKE SYS_OP_C2C(:6) OR "SL"."SN"=SYS_OP_C2C(:7))
    220. 6 - access("SL"."SN"(+)="SC"."SN")
    221. 13 - filter(TO_DATE(:3,'yyyy-MM-dd')+1>=TO_DATE(:2,'yyyy-MM-dd'))
    222. 14 - access("T2"."SUPDEPTID"=PRIOR NULL)
    223. 16 - access("T2"."DEPTID"=SYS_OP_C2C(:4))
    224. 20 - access("connect$_by$_pump$_019"."prior T2.DEPTID "="T2"."SUPDEPTID")
    225. 22 - access("SD"."DEPTID"="DEPTID")
    226. 25 - access("DEPTID"="SD"."DEPTID")
    227. 26 - access("SD"."DEPTID"="DEPTID")
    228. 27 - filter("DEPTID"="SD"."DEPTID")
    229. 29 - access("T2"."SUPDEPTID"=PRIOR NULL)
    230. 31 - access("T2"."DEPTID"=SYS_OP_C2C(:1))
    231. 35 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    232. 36 - access("DEPTID"="SD"."DEPTID")
    233. 37 - access("SU"."BADGENUMBER"="SC"."BADGENUMBER")
    234. 38 - filter("SC"."CHECKTIME">=TO_DATE(:2,'yyyy-MM-dd') AND "SC"."CHECKTIME"<=TO_DATE(:3,'yyyy-MM-dd')+1)
    235. Plan 2
    236. ------
    237. Plan Origin :AWR
    238. Plan Hash Value :205839464
    239. Executions :86
    240. Elapsed Time :1.492 sec
    241. CPU Time :0.100 sec
    242. Buffer Gets :31273
    243. Disk Reads :109
    244. Disk Writes :0
    245. Notes:
    246. 1. Statistics shown are averaged over multiple executions.
    247. -----------------------------------------------------------------------------------------------------------------------------------------------
    248. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop |
    249. -----------------------------------------------------------------------------------------------------------------------------------------------
    250. | 0 | SELECT STATEMENT | | 10 | 4030 | 1998 (1)| 00:00:24 | | |
    251. |* 1 | VIEW | | 10 | 4030 | 1998 (1)| 00:00:24 | | |
    252. |* 2 | COUNT STOPKEY | | | | | | | |
    253. | 3 | VIEW | | 55 | 21450 | 1998 (1)| 00:00:24 | | |
    254. |* 4 | SORT ORDER BY STOPKEY | | 55 | 14740 | 1998 (1)| 00:00:24 | | |
    255. |* 5 | FILTER | | | | | | | |
    256. | 6 | NESTED LOOPS OUTER | | 55 | 14740 | 1997 (1)| 00:00:24 | | |
    257. | 7 | NESTED LOOPS | | 1088 | 233K| 1520 (1)| 00:00:19 | | |
    258. | 8 | NESTED LOOPS | | 93 | 14043 | 104 (1)| 00:00:02 | | |
    259. | 9 | NESTED LOOPS | | 3 | 270 | 10 (10)| 00:00:01 | | |
    260. | 10 | VIEW | VW_NSO_2 | 3 | 66 | 7 (15)| 00:00:01 | | |
    261. | 11 | HASH UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    262. |* 12 | FILTER | | | | | | | |
    263. |* 13 | CONNECT BY WITH FILTERING (UNIQUE) | | | | | | | |
    264. | 14 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    265. |* 15 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    266. | 16 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    267. | 17 | CONNECT BY PUMP | | | | | | | |
    268. | 18 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    269. |* 19 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    270. | 20 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 68 | 1 (0)| 00:00:01 | | |
    271. |* 21 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 0 (0)| 00:00:01 | | |
    272. | 22 | VIEW | | 31 | 1891 | 60 (0)| 00:00:01 | | |
    273. | 23 | UNION-ALL PARTITION | | | | | | | |
    274. |* 24 | INDEX RANGE SCAN | IDX1_SAMS_USERINFO | 69 | 4209 | 4 (0)| 00:00:01 | | |
    275. |* 25 | HASH JOIN | | 3 | 309 | 10 (10)| 00:00:01 | | |
    276. |* 26 | VIEW | VW_NSO_1 | 3 | 126 | 7 (15)| 00:00:01 | | |
    277. | 27 | SORT UNIQUE | | 3 | 192 | 7 (15)| 00:00:01 | | |
    278. |* 28 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | | |
    279. | 29 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | 2 (0)| 00:00:01 | | |
    280. |* 30 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | 1 (0)| 00:00:01 | | |
    281. | 31 | NESTED LOOPS | | 2 | 160 | 4 (0)| 00:00:01 | | |
    282. | 32 | CONNECT BY PUMP | | | | | | | |
    283. | 33 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | 2 (0)| 00:00:01 | | |
    284. |* 34 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | 1 (0)| 00:00:01 | | |
    285. |* 35 | INDEX RANGE SCAN | IDX_SAMS_USERINFO_DIMISSION | 46 | 2806 | 3 (0)| 00:00:01 | | |
    286. |* 36 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 12 | 828 | 23 (0)| 00:00:01 | ROWID | ROWID |
    287. |* 37 | INDEX RANGE SCAN | IDX_SAMS_CHECKINOUT | 19 | | 3 (0)| 00:00:01 | | |
    288. | 38 | TABLE ACCESS BY INDEX ROWID | SAMS_ICLOCK | 1 | 48 | 1 (0)| 00:00:01 | | |
    289. |* 39 | INDEX UNIQUE SCAN | PK_SAMS_ICLOCK | 1 | | 0 (0)| 00:00:01 | | |
    290. -----------------------------------------------------------------------------------------------------------------------------------------------
    291. Predicate Information (identified by operation id):
    292. ---------------------------------------------------
    293. 1 - filter("RN">:9)
    294. 2 - filter(ROWNUM<=:8)
    295. 4 - filter(ROWNUM<=:8)
    296. 5 - filter("SU"."BADGENUMBER"=SYS_OP_C2C(:5) OR "SU"."NAME" LIKE SYS_OP_C2C(:6) OR "SL"."SN"=SYS_OP_C2C(:7))
    297. 12 - filter(TO_DATE(:3,'yyyy-MM-dd')+1>=TO_DATE(:2,'yyyy-MM-dd'))
    298. 13 - access("T2"."SUPDEPTID"=PRIOR NULL)
    299. 15 - access("T2"."DEPTID"=SYS_OP_C2C(:4))
    300. 19 - access("connect$_by$_pump$_019"."prior T2.DEPTID "="T2"."SUPDEPTID")
    301. 21 - access("SD"."DEPTID"="DEPTID")
    302. 24 - access("DEPTID"="SD"."DEPTID")
    303. 25 - access("SD"."DEPTID"="DEPTID")
    304. 26 - filter("DEPTID"="SD"."DEPTID")
    305. 28 - access("T2"."SUPDEPTID"=PRIOR NULL)
    306. 30 - access("T2"."DEPTID"=SYS_OP_C2C(:1))
    307. 34 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    308. 35 - access("DEPTID"="SD"."DEPTID")
    309. 36 - filter("SC"."CHECKTIME">=TO_DATE(:2,'yyyy-MM-dd') AND "SC"."CHECKTIME"<=TO_DATE(:3,'yyyy-MM-dd')+1)
    310. 37 - access("SU"."BADGENUMBER"="SC"."BADGENUMBER")
    311. 39 - access("SL"."SN"(+)="SC"."SN")
    312. Plan 3
    313. ------
    314. Plan Origin :AWR
    315. Plan Hash Value :4243346097
    316. Executions :130
    317. Elapsed Time :33323.697 sec
    318. CPU Time :5608.075 sec
    319. Buffer Gets :168638697
    320. Disk Reads :156922
    321. Disk Writes :0
    322. Notes:
    323. 1. Statistics shown are averaged over multiple executions.
    324. ------------------------------------------------------------------------------------------------------------------------------------------------------
    325. | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
    326. ------------------------------------------------------------------------------------------------------------------------------------------------------
    327. | 0 | SELECT STATEMENT | | 10 | 4030 | | 59M (1)|199:40:41 | | |
    328. |* 1 | VIEW | | 10 | 4030 | | 59M (1)|199:40:41 | | |
    329. |* 2 | COUNT STOPKEY | | | | | | | | |
    330. | 3 | VIEW | | 55 | 21450 | | 59M (1)|199:40:41 | | |
    331. |* 4 | SORT ORDER BY STOPKEY | | 55 | 14740 | | 59M (1)|199:40:41 | | |
    332. |* 5 | HASH JOIN | | 55 | 14740 | 228M| 59M (1)|199:40:41 | | |
    333. | 6 | NESTED LOOPS | | 928K| 217M| | 59M (1)|199:38:25 | | |
    334. | 7 | NESTED LOOPS | | 928K| 217M| | 59M (1)|199:38:25 | | |
    335. | 8 | NESTED LOOPS | | 928K| 157M| | 58M (1)|196:32:36 | | |
    336. | 9 | NESTED LOOPS OUTER | | 18M| 2066M| | 21M (1)| 73:02:45 | | |
    337. | 10 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 18M| 1218M| | 13M (1)| 45:58:50 | ROWID | ROWID |
    338. |* 11 | INDEX RANGE SCAN | IDX1_SAMS_CHECKINOUT | 18M| | | 242K (1)| 00:48:26 | | |
    339. | 12 | TABLE ACCESS BY INDEX ROWID | SAMS_ICLOCK | 1 | 48 | | 1 (0)| 00:00:01 | | |
    340. |* 13 | INDEX UNIQUE SCAN | PK_SAMS_ICLOCK | 1 | | | 0 (0)| 00:00:01 | | |
    341. |* 14 | VIEW | | 1 | 61 | | 2 (0)| 00:00:01 | | |
    342. | 15 | UNION-ALL PARTITION | | | | | | | | |
    343. | 16 | TABLE ACCESS BY INDEX ROWID | SAMS_USERINFO | 1 | 61 | | 3 (0)| 00:00:01 | | |
    344. |* 17 | INDEX UNIQUE SCAN | PK_SAMS_USERINFO | 1 | | | 2 (0)| 00:00:01 | | |
    345. | 18 | NESTED LOOPS | | 1 | 103 | | 10 (10)| 00:00:01 | | |
    346. | 19 | TABLE ACCESS BY INDEX ROWID | SAMS_USERINFO_DIMISSION | 1 | 61 | | 3 (0)| 00:00:01 | | |
    347. |* 20 | INDEX UNIQUE SCAN | PK_SAMS_USERINFO_DIMISSION | 1 | | | 2 (0)| 00:00:01 | | |
    348. |* 21 | VIEW | VW_NSO_1 | 1 | 42 | | 7 (15)| 00:00:01 | | |
    349. | 22 | SORT UNIQUE | | 3 | 192 | | 7 (15)| 00:00:01 | | |
    350. |* 23 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | | | |
    351. | 24 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | | 2 (0)| 00:00:01 | | |
    352. |* 25 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 1 (0)| 00:00:01 | | |
    353. | 26 | NESTED LOOPS | | 2 | 160 | | 4 (0)| 00:00:01 | | |
    354. | 27 | CONNECT BY PUMP | | | | | | | | |
    355. | 28 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | | 2 (0)| 00:00:01 | | |
    356. |* 29 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | | 1 (0)| 00:00:01 | | |
    357. |* 30 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 0 (0)| 00:00:01 | | |
    358. | 31 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 68 | | 1 (0)| 00:00:01 | | |
    359. | 32 | VIEW | VW_NSO_2 | 3 | 66 | | 7 (15)| 00:00:01 | | |
    360. | 33 | HASH UNIQUE | | 3 | 192 | | 7 (15)| 00:00:01 | | |
    361. |* 34 | FILTER | | | | | | | | |
    362. |* 35 | CONNECT BY WITH FILTERING (UNIQUE) | | | | | | | | |
    363. | 36 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | | 2 (0)| 00:00:01 | | |
    364. |* 37 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 1 (0)| 00:00:01 | | |
    365. | 38 | NESTED LOOPS | | 2 | 160 | | 4 (0)| 00:00:01 | | |
    366. | 39 | CONNECT BY PUMP | | | | | | | | |
    367. | 40 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | | 2 (0)| 00:00:01 | | |
    368. |* 41 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | | 1 (0)| 00:00:01 | | |
    369. ------------------------------------------------------------------------------------------------------------------------------------------------------
    370. Predicate Information (identified by operation id):
    371. ---------------------------------------------------
    372. 1 - filter("RN">:9)
    373. 2 - filter(ROWNUM<=:8)
    374. 4 - filter(ROWNUM<=:8)
    375. 5 - access("SD"."DEPTID"="DEPTID")
    376. 11 - access("SC"."CHECKTIME">=TO_DATE(:2,'yyyy-MM-dd') AND "SC"."CHECKTIME"<=TO_DATE(:3,'yyyy-MM-dd')+1)
    377. 13 - access("SL"."SN"(+)="SC"."SN")
    378. 14 - filter("SU"."BADGENUMBER"=SYS_OP_C2C(:5) OR "SU"."NAME" LIKE SYS_OP_C2C(:6) OR "SL"."SN"=SYS_OP_C2C(:7))
    379. 17 - access("BADGENUMBER"="SC"."BADGENUMBER")
    380. 20 - access("BADGENUMBER"="SC"."BADGENUMBER")
    381. 21 - filter("SD"."DEPTID"="DEPTID")
    382. 23 - access("T2"."SUPDEPTID"=PRIOR NULL)
    383. 25 - access("T2"."DEPTID"=SYS_OP_C2C(:1))
    384. 29 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    385. 30 - access("SD"."DEPTID"="SU"."DEPTID")
    386. 34 - filter(TO_DATE(:3,'yyyy-MM-dd')+1>=TO_DATE(:2,'yyyy-MM-dd'))
    387. 35 - access("T2"."SUPDEPTID"=PRIOR NULL)
    388. 37 - access("T2"."DEPTID"=SYS_OP_C2C(:4))
    389. 41 - access("connect$_by$_pump$_019"."prior T2.DEPTID "="T2"."SUPDEPTID")
    390. -------------------------------------------------------------------------------
    391. SQL>
    392. SQL>
    393. SQL>
    394. SQL> !ora ddl sams index IDX1_SAMS_CHECKINOUT
    395. Session altered.
    396. DBMS_METADATA.GET_DDL(UPPER('INDEX'),UPPER('IDX1_SAMS_CHECKINOUT'),UPPER('SAMS')
    397. --------------------------------------------------------------------------------
    398. CREATE INDEX "SAMS"."IDX1_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("CHECK
    399. TIME", "BADGENUMBER", "ID") PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATIS
    400. TICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
    401. PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
    402. BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
    403. TABLESPACE "SAMSDATA"
    通过自动优化功能,我们看到了跟awr报告上一样的执行计划。

    1. ------------------------------------------------------------------------------------------------------------------------------------------------------
    2. | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
    3. ------------------------------------------------------------------------------------------------------------------------------------------------------
    4. | 0 | SELECT STATEMENT | | 10 | 4030 | | 59M (1)|199:40:41 | | |
    5. |* 1 | VIEW | | 10 | 4030 | | 59M (1)|199:40:41 | | |
    6. |* 2 | COUNT STOPKEY | | | | | | | | |
    7. | 3 | VIEW | | 55 | 21450 | | 59M (1)|199:40:41 | | |
    8. |* 4 | SORT ORDER BY STOPKEY | | 55 | 14740 | | 59M (1)|199:40:41 | | |
    9. |* 5 | HASH JOIN | | 55 | 14740 | 228M| 59M (1)|199:40:41 | | |
    10. | 6 | NESTED LOOPS | | 928K| 217M| | 59M (1)|199:38:25 | | |
    11. | 7 | NESTED LOOPS | | 928K| 217M| | 59M (1)|199:38:25 | | |
    12. | 8 | NESTED LOOPS | | 928K| 157M| | 58M (1)|196:32:36 | | |
    13. | 9 | NESTED LOOPS OUTER | | 18M| 2066M| | 21M (1)| 73:02:45 | | |
    14. | 10 | TABLE ACCESS BY GLOBAL INDEX ROWID | SAMS_CHECKINOUT | 18M| 1218M| | 13M (1)| 45:58:50 | ROWID | ROWID |
    15. |* 11 | INDEX RANGE SCAN | IDX1_SAMS_CHECKINOUT | 18M| | | 242K (1)| 00:48:26 | | |
    16. | 12 | TABLE ACCESS BY INDEX ROWID | SAMS_ICLOCK | 1 | 48 | | 1 (0)| 00:00:01 | | |
    17. |* 13 | INDEX UNIQUE SCAN | PK_SAMS_ICLOCK | 1 | | | 0 (0)| 00:00:01 | | |
    18. |* 14 | VIEW | | 1 | 61 | | 2 (0)| 00:00:01 | | |
    19. | 15 | UNION-ALL PARTITION | | | | | | | | |
    20. | 16 | TABLE ACCESS BY INDEX ROWID | SAMS_USERINFO | 1 | 61 | | 3 (0)| 00:00:01 | | |
    21. |* 17 | INDEX UNIQUE SCAN | PK_SAMS_USERINFO | 1 | | | 2 (0)| 00:00:01 | | |
    22. | 18 | NESTED LOOPS | | 1 | 103 | | 10 (10)| 00:00:01 | | |
    23. | 19 | TABLE ACCESS BY INDEX ROWID | SAMS_USERINFO_DIMISSION | 1 | 61 | | 3 (0)| 00:00:01 | | |
    24. |* 20 | INDEX UNIQUE SCAN | PK_SAMS_USERINFO_DIMISSION | 1 | | | 2 (0)| 00:00:01 | | |
    25. |* 21 | VIEW | VW_NSO_1 | 1 | 42 | | 7 (15)| 00:00:01 | | |
    26. | 22 | SORT UNIQUE | | 3 | 192 | | 7 (15)| 00:00:01 | | |
    27. |* 23 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | | | |
    28. | 24 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | | 2 (0)| 00:00:01 | | |
    29. |* 25 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 1 (0)| 00:00:01 | | |
    30. | 26 | NESTED LOOPS | | 2 | 160 | | 4 (0)| 00:00:01 | | |
    31. | 27 | CONNECT BY PUMP | | | | | | | | |
    32. | 28 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | | 2 (0)| 00:00:01 | | |
    33. |* 29 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | | 1 (0)| 00:00:01 | | |
    34. |* 30 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 0 (0)| 00:00:01 | | |
    35. | 31 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 68 | | 1 (0)| 00:00:01 | | |
    36. | 32 | VIEW | VW_NSO_2 | 3 | 66 | | 7 (15)| 00:00:01 | | |
    37. | 33 | HASH UNIQUE | | 3 | 192 | | 7 (15)| 00:00:01 | | |
    38. |* 34 | FILTER | | | | | | | | |
    39. |* 35 | CONNECT BY WITH FILTERING (UNIQUE) | | | | | | | | |
    40. | 36 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | | 2 (0)| 00:00:01 | | |
    41. |* 37 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 1 (0)| 00:00:01 | | |
    42. | 38 | NESTED LOOPS | | 2 | 160 | | 4 (0)| 00:00:01 | | |
    43. | 39 | CONNECT BY PUMP | | | | | | | | |
    44. | 40 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | | 2 (0)| 00:00:01 | | |
    45. |* 41 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | | 1 (0)| 00:00:01 | | |
    46. ------------------------------------------------------------------------------------------------------------------------------------------------------

    从执行计划上看sql执行效率非常的不好,开销是从id为10的一步开始骤降:TABLE ACCESS BY GLOBAL INDEX ROWID, 其操作是对IDX1_SAMS_CHECKINOUT这个索引的范围扫描回表拿数据
    删除这个索引,然后重新创建ID列的索引
    1. drop index "SAMS"."IDX1_SAMS_CHECKINOUT";
    2. CREATE INDEX "SAMS"."IDX1_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" ("ID")

    针对其他性能较差的sql进行优化
    1. Schema Name: SAMS
    2. SQL ID : bfv69ds34p99n
    3. SQL Text : SELECT ID,SN_ID,CMDCONTENT,CMDCOMMITTIME,CMDTRANSTIME,CMDOVERTIME
    4. ,CMDRETURN,USERID FROM SAMS_DEVCMDS WHERE CMDCONTENT IS NOT NULL
    5. AND CMDTRANSTIME IS NULL AND SN_ID=:1 AND ROWNUM<100 ORDER BY
    6. TO_NUMBER(ID)
    7. Bind Variables :
    8. 1 - (VARCHAR2(128)):6538992526356
    9. -------------------------------------------------------------------------------
    10. FINDINGS SECTION (1 finding)
    11. -------------------------------------------------------------------------------
    12. 1- Index Finding (see explain plans section below)
    13. --------------------------------------------------
    14. The execution plan of this statement can be improved by creating one or more
    15. indices.
    16. Recommendation (estimated benefit: 92.69%)
    17. ------------------------------------------
    18. - Consider running the Access Advisor to improve the physical schema design
    19. or creating the recommended index.
    20. create index SAMS.IDX$$_15B1A0001 on SAMS.SAMS_DEVCMDS("SN_ID","CMDTRANSTIME"); -------------------根据建议创建这个索引
    21. Rationale
    22. ---------
    23. Creating the recommended indices significantly improves the execution plan
    24. of this statement. However, it might be preferable to run "Access Advisor"
    25. using a representative SQL workload as opposed to a single statement. This
    26. will allow to get comprehensive index recommendations which takes into
    27. account index maintenance overhead and additional space consumption.
    28. -------------------------------------------------------------------------------
    29. EXPLAIN PLANS SECTION
    30. -------------------------------------------------------------------------------
    31. 1- Original
    32. -----------
    33. Plan hash value: 2086948762
    34. --------------------------------------------------------------------------------------------------
    35. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    36. --------------------------------------------------------------------------------------------------
    37. | 0 | SELECT STATEMENT | | 87 | 172K| 630 (1)| 00:00:08 |
    38. | 1 | SORT ORDER BY | | 87 | 172K| 630 (1)| 00:00:08 |
    39. |* 2 | COUNT STOPKEY | | | | | |
    40. |* 3 | TABLE ACCESS BY INDEX ROWID| SAMS_DEVCMDS | 87 | 172K| 629 (0)| 00:00:08 |
    41. |* 4 | INDEX RANGE SCAN | IDX_SAMS_DEVCMDS | 908 | | 16 (0)| 00:00:01 |
    42. --------------------------------------------------------------------------------------------------
    43. Predicate Information (identified by operation id):
    44. ---------------------------------------------------
    45. 2 - filter(ROWNUM<100)
    46. 3 - filter("CMDTRANSTIME" IS NULL AND "CMDCONTENT" IS NOT NULL)
    47. 4 - access("SN_ID"=SYS_OP_C2C(:1))
    48. 2- Using New Indices
    49. --------------------
    50. Plan hash value: 3352898769
    51. -------------------------------------------------------------------------------------------------
    52. | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    53. -------------------------------------------------------------------------------------------------
    54. | 0 | SELECT STATEMENT | | 87 | 172K| 46 (3)| 00:00:01 |
    55. | 1 | SORT ORDER BY | | 87 | 172K| 46 (3)| 00:00:01 |
    56. |* 2 | COUNT STOPKEY | | | | | |
    57. |* 3 | TABLE ACCESS BY INDEX ROWID| SAMS_DEVCMDS | 87 | 172K| 45 (0)| 00:00:01 |
    58. |* 4 | INDEX RANGE SCAN | IDX$$_15B1A0001 | 95 | | 3 (0)| 00:00:01 |
    59. -------------------------------------------------------------------------------------------------
    60. Predicate Information (identified by operation id):
    61. ---------------------------------------------------
    62. 2 - filter(ROWNUM<100)
    63. 3 - filter("CMDCONTENT" IS NOT NULL)
    64. 4 - access("SN_ID"=SYS_OP_C2C(:1) AND "CMDTRANSTIME" IS NULL)

    创建索引如下,都是根据oracle建议做的。:

    1. create index SAMS.IDX$$_15B1D0001 on SAMS.SAMS_USERINFO_DIMISSION("DEPTID") TABLESPACE "SAMSDATA" ;
    2. create index SAMS.IDX$$_15B1A0001 on SAMS.SAMS_DEVCMDS("SN_ID","CMDTRANSTIME");
    3. create index SAMS.IDX$$_15B3A0001 on SAMS.SAMS_CHECKINOUT_BAK(SYS_OP_C2C("BADGENUMBER")) TABLESPACE "SAMSDATA" parallel 12;
    1. Tune the sql
    2. ~~~~~~~~~~~~
    3. GENERAL INFORMATION SECTION
    4. -------------------------------------------------------------------------------
    5. Tuning Task Name : TASK_88888
    6. Tuning Task Owner : SYS
    7. Workload Type : Single SQL Statement
    8. Scope : COMPREHENSIVE
    9. Time Limit(seconds): 1800
    10. Completion Status : COMPLETED
    11. Started at : 03/30/2017 11:31:00
    12. Completed at : 03/30/2017 11:46:57
    13. -------------------------------------------------------------------------------
    14. Schema Name: SAMS
    15. SQL ID : cpbt6x15q273d
    16. SQL Text : select count(1) from(select aa.badgenumber,case when valids is
    17. not null then '是' else '否' end as valids from(select
    18. distinct(su.badgenumber),(select distinct('是') from
    19. sams_template st where st.badgenumber=su.badgenumber and
    20. st.template is not null and st.deltag = '0') as valids from
    21. SAMS_USERINFO su inner join SAMS_DEPARTMENTS sd on
    22. su.deptid=sd.deptid where 1=1 and su.SYSTAG='1' and sd.deptid
    23. in ( select T2.Deptid from SAMS_DEPARTMENTS T2 start with
    24. T2.Deptid = '000000' connect by prior T2.DEPTID = T2.SUPDEPTID)
    25. ) aa where 1=1 )
    26. -------------------------------------------------------------------------------
    27. FINDINGS SECTION (2 findings)
    28. -------------------------------------------------------------------------------
    29. 1- SQL Profile Finding (see explain plans section below)
    30. --------------------------------------------------------
    31. 2 potentially better execution plans were found for this statement. Choose
    32. one of the following SQL profiles to implement.
    33. Recommendation (estimated benefit: 98.35%)
    34. ------------------------------------------
    35. - Consider accepting the recommended SQL profile.
    36. execute dbms_sqltune.accept_sql_profile(task_name => 'TASK_88888',
    37. task_owner => 'SYS', replace => TRUE);
    38. Recommendation (estimated benefit: 99.98%)
    39. ------------------------------------------
    40. - Consider accepting the recommended SQL profile to use parallel execution
    41. for this statement.
    42. execute dbms_sqltune.accept_sql_profile(task_name => 'TASK_88888',
    43. task_owner => 'SYS', replace => TRUE, profile_type =>
    44. DBMS_SQLTUNE.PX_PROFILE);
    45. Executing this query parallel with DOP 96 will improve its response time
    46. 99.37% over the SQL profile plan. However, there is some cost in enabling
    47. parallel execution. It will increase the statement's resource consumption by
    48. an estimated 39.06% which may result in a reduction of system throughput.
    49. Also, because these resources are consumed over a much smaller duration, the
    50. response time of concurrent statements might be negatively impacted if
    51. sufficient hardware capacity is not available.
    52. The following data shows some sampled statistics for this SQL from the past
    53. week and projected weekly values when parallel execution is enabled.
    54. Past week sampled statistics for this SQL
    55. -----------------------------------------
    56. Number of executions 1241
    57. Percent of total activity .75
    58. Percent of samples with #Active Sessions > 2*CPU 42.16
    59. Weekly DB time (in sec) 462921.68
    60. Projected statistics with Parallel Execution
    61. --------------------------------------------
    62. Weekly DB time (in sec) 643740.64
    63. 2- Alternative Plan Finding
    64. ---------------------------
    65. Some alternative execution plans for this statement were found by searching
    66. the system's real-time and historical performance data.
    67. The following table lists these plans ranked by their average elapsed time.
    68. See section "ALTERNATIVE PLANS SECTION" for detailed information on each
    69. plan.
    70. id plan hash last seen elapsed (s) origin note
    71. -- ---------- -------------------- ------------ --------------- ----------------
    72. 1 1614405204 2017-03-30/09:32:49 7132.651 Cursor Cache original plan
    73. 2 2181297630 2017-03-29/19:00:16 19363.989 AWR
    74. Information
    75. -----------
    76. - The Original Plan appears to have the best performance, based on the
    77. elapsed time per execution. However, if you know that one alternative
    78. plan is better than the Original Plan, you can create a SQL plan baseline
    79. for it. This will instruct the Oracle optimizer to pick it over any other
    80. choices in the future.
    81. execute dbms_sqltune.create_sql_plan_baseline(task_name => 'TASK_88888',
    82. owner_name => 'SYS', plan_hash_value => xxxxxxxx);
    83. -------------------------------------------------------------------------------
    84. EXPLAIN PLANS SECTION
    85. -------------------------------------------------------------------------------
    86. 1- Original With Adjusted Cost
    87. ------------------------------
    88. Plan hash value: 1614405204
    89. --------------------------------------------------------------------------------------------------------------------------
    90. | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
    91. --------------------------------------------------------------------------------------------------------------------------
    92. | 0 | SELECT STATEMENT | | 1 | 2 | | 1683K (1)| 05:36:42 |
    93. | 1 | SORT AGGREGATE | | 1 | 2 | | | |
    94. | 2 | VIEW | | 18606 | 37212 | | 1683K (1)| 05:36:42 |
    95. | 3 | SORT UNIQUE NOSORT | | 3 | 7833 | | 7 (15)| 00:00:01 |
    96. |* 4 | TABLE ACCESS BY INDEX ROWID | SAMS_TEMPLATE | 3 | 7833 | | 6 (0)| 00:00:01 |
    97. |* 5 | INDEX RANGE SCAN | IDX$$_CBE40001 | 3 | | | 3 (0)| 00:00:01 |
    98. | 6 | HASH UNIQUE | | 18606 | 1835K| 170M| 1683K (1)| 05:36:42 |
    99. | 7 | NESTED LOOPS | | 1571K| 151M| | 1670K (1)| 05:34:05 |
    100. | 8 | NESTED LOOPS | | 3513K| 151M| | 1670K (1)| 05:34:05 |
    101. | 9 | NESTED LOOPS | | 50926 | 2188K| | 7 (15)| 00:00:01 |
    102. | 10 | VIEW | VW_NSO_1 | 3 | 66 | | 7 (15)| 00:00:01 |
    103. | 11 | HASH UNIQUE | | 3 | 252 | | 7 (15)| 00:00:01 |
    104. |* 12 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | |
    105. | 13 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | | 2 (0)| 00:00:01 |
    106. |* 14 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 1 (0)| 00:00:01 |
    107. | 15 | NESTED LOOPS | | 2 | 160 | | 4 (0)| 00:00:01 |
    108. | 16 | CONNECT BY PUMP | | | | | | |
    109. | 17 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | | 2 (0)| 00:00:01 |
    110. |* 18 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | | 1 (0)| 00:00:01 |
    111. |* 19 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 16975 | 364K| | 0 (0)| 00:00:01 |
    112. |* 20 | INDEX RANGE SCAN | IDX_SAMS_USERINFO | 69 | | | 3 (0)| 00:00:01 |
    113. |* 21 | TABLE ACCESS BY INDEX ROWID | SAMS_USERINFO | 31 | 1767 | | 69 (0)| 00:00:01 |
    114. --------------------------------------------------------------------------------------------------------------------------
    115. Predicate Information (identified by operation id):
    116. ---------------------------------------------------
    117. 4 - filter("ST"."TEMPLATE" IS NOT NULL)
    118. 5 - access("ST"."DELTAG"='0' AND "ST"."BADGENUMBER"=:B1)
    119. 12 - access("T2"."SUPDEPTID"=PRIOR NULL)
    120. 14 - access("T2"."DEPTID"=U'000000')
    121. 18 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    122. 19 - access("SD"."DEPTID"="DEPTID")
    123. 20 - access("SU"."DEPTID"="SD"."DEPTID")
    124. 21 - filter("SU"."SYSTAG"=U'1')
    125. 2- Using SQL Profile
    126. --------------------
    127. Plan hash value: 1215825753
    128. -------------------------------------------------------------------------------------------------------------------------
    129. | Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
    130. -------------------------------------------------------------------------------------------------------------------------
    131. | 0 | SELECT STATEMENT | | 1 | 2 | | 27702 (1)| 00:05:33 |
    132. | 1 | SORT AGGREGATE | | 1 | 2 | | | |
    133. | 2 | VIEW | | 18606 | 37212 | | 27702 (1)| 00:05:33 |
    134. | 3 | SORT UNIQUE NOSORT | | 3 | 7833 | | 7 (15)| 00:00:01 |
    135. |* 4 | TABLE ACCESS BY INDEX ROWID | SAMS_TEMPLATE | 3 | 7833 | | 6 (0)| 00:00:01 |
    136. |* 5 | INDEX RANGE SCAN | IDX$$_CBE40001 | 3 | | | 3 (0)| 00:00:01 |
    137. | 6 | HASH UNIQUE | | 18606 | 1835K| 170M| 27702 (1)| 00:05:33 |
    138. |* 7 | HASH JOIN | | 1571K| 151M| | 14640 (1)| 00:02:56 |
    139. |* 8 | TABLE ACCESS FULL | SAMS_USERINFO | 18606 | 1035K| | 14627 (1)| 00:02:56 |
    140. | 9 | NESTED LOOPS | | 50926 | 2188K| | 7 (15)| 00:00:01 |
    141. | 10 | VIEW | VW_NSO_1 | 3 | 66 | | 7 (15)| 00:00:01 |
    142. | 11 | HASH UNIQUE | | 3 | 252 | | 7 (15)| 00:00:01 |
    143. |* 12 | CONNECT BY WITH FILTERING (UNIQUE)| | | | | | |
    144. | 13 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 1 | 38 | | 2 (0)| 00:00:01 |
    145. |* 14 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 1 | | | 1 (0)| 00:00:01 |
    146. | 15 | NESTED LOOPS | | 2 | 160 | | 4 (0)| 00:00:01 |
    147. | 16 | CONNECT BY PUMP | | | | | | |
    148. | 17 | TABLE ACCESS BY INDEX ROWID | SAMS_DEPARTMENTS | 2 | 76 | | 2 (0)| 00:00:01 |
    149. |* 18 | INDEX RANGE SCAN | SUPDEPTID_IDX | 2 | | | 1 (0)| 00:00:01 |
    150. |* 19 | INDEX UNIQUE SCAN | PK_SAMS_DEPARTMENTS | 16975 | 364K| | 0 (0)| 00:00:01 |
    151. -------------------------------------------------------------------------------------------------------------------------
    152. Predicate Information (identified by operation id):
    153. ---------------------------------------------------
    154. 4 - filter("ST"."TEMPLATE" IS NOT NULL)
    155. 5 - access("ST"."DELTAG"='0' AND "ST"."BADGENUMBER"=:B1)
    156. 7 - access("SU"."DEPTID"="SD"."DEPTID")
    157. 8 - filter("SU"."SYSTAG"=U'1')
    158. 12 - access("T2"."SUPDEPTID"=PRIOR NULL)
    159. 14 - access("T2"."DEPTID"=U'000000')
    160. 18 - access("connect$_by$_pump$_008"."prior T2.DEPTID "="T2"."SUPDEPTID")
    161. 19 - access("SD"."DEPTID"="DEPTID")
    对以上sql进行了执行计划绑定修改
    1. execute dbms_sqltune.accept_sql_profile(task_name => 'TASK_88888',task_owner => 'SYS', replace => TRUE);

    1. SQL> select NAME from dba_sql_profiles;
    2. NAME
    3. ------------------------------
    4. coe_8yshc4jbu0qc1_1759800418
    5. SYS_SQLPROF_015b1d66e6010001
    6. SYS_SQLPROF_015b1af651db0000
    7. coe_bw0b67268pva8_561269195
    8. coe_fq1q2q6h7kqf0_561269195
    9. 5 rows selected.
    10. SQL> select SQL_TEXT from dba_sql_profiles where name ='SYS_SQLPROF_015b1d66e6010001';
    11. SQL_TEXT
    12. --------------------------------------------------
    13. select count(1) from(select aa.badgenumber,case wh
    14. en valids is not null then '是' else '否' end a
    15. s valids from(select distinct(su.badgenumber),(sel
    16. ect distinct('是') from sams_template st where st
    17. .badgenumber=su.badgenumber and st.template is not
    18. null and st.deltag = '0') as valids from SAMS_USE
    19. RINFO su inner join SAMS_DEPARTMENTS sd on su.dept
    20. id=sd.deptid where 1=1 and su.SYSTAG='1' and sd.
    21. deptid in ( select T2.Deptid from SAMS_DEPARTMENTS
    22. T2 start with T2.Deptid = '000000' connect by pri
    23. or T2.DEPTID = T2.SUPDEPTID) ) aa where 1=1 )
    24. 1 row selected.
    25. SQL> select SQL_TEXT from dba_sql_profiles where name ='SYS_SQLPROF_015b1af651db0000';
    26. SQL_TEXT
    27. --------------------------------------------------
    28. SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (select
    29. sd.deptnumber,sd.deptname,su.badgenumber,su.name,
    30. to_char(sc.checktime,'YYYY-MM-DD HH24:MI:SS') as C
    31. HECKTIME,case when rtrim(sc.verifycode)='0' then '
    32. 密码' when rtrim(sc.verifycode)='1' then '指纹' wh
    33. en rtrim(sc.verifycode)='2' then '补签' when rtrim
    34. (sc.verifycode)='15' then '面部' when rtrim(sc.ver
    35. ifycode)='ZW' then '指纹' when rtrim(sc.verifycode
    36. )='RL' then '面部' when rtrim(sc.verifycode)='YD'
    37. then '移动打卡(GPS)' when rtrim(sc.verifycode)='EJ
    38. ' then '国寿E家' end as verifycode,sl.sn||'('||sl
    39. .alias||')' as devicename, to_char(sc.insystime,'Y
    40. YYY-MM-DD HH24:MI:SS') as INSYSTIME from SAMS_CHEC
    41. KINOUT sc inner join (select badgenumber,name,dept
    42. id from SAMS_USERINFO union all select badgenumber
    43. ,name,deptid from SAMS_USERINFO_DIMISSION sd where
    44. 1=1 and sd.DEPTID=:1 ) su on su.badgenumber = s
    45. c.badgenumber inner join SAMS_DEPARTMENTS sd on sd
    46. .deptid = su.deptid left join SAMS_ICLOCK sl on sl
    47. .sn=sc.sn where 1=1 and sc.checktime>= to_date(:2
    48. ,'yyyy-MM-dd') and sc.checktime<= to_date(:3 ,'yy
    49. yy-MM-dd')+1 and sd.DEPTID=:4 order by sc.check
    50. time,su.name desc ) A WHERE ROWNUM <= :5 ) WHERE
    51. RN > :6
    一共做了2个profile如上,针对2个sql,先不动了。


    六个变量的sql是前台没有做关联查询的,执行情况跟9个变量的差不多


     

    手工测试也是无法模拟awr报告上面的执行计划,在sql自动优化里面可以看到,这个sql的执行计划已经做了profile,对应的name是SYS_SQLPROF_015b1af651db0000


    2017年3月31日 星期五 10:45:22

    今天测试一下上面两个索引的效果
    create index sams.ind1_sams_iclock on sams.sams_iclock(sn,alias) TABLESPACE "SAMSDATA" parallel 12;
    alter index sams.ind1_sams_iclock  noparallel;
     CREATE INDEX "SAMS"."IDX5_SAMS_CHECKINOUT" ON "SAMS"."SAMS_CHECKINOUT" (BADGENUMBER,CHECKTIME,SN,VERIFYCODE,INSYSTIME) TABLESPACE "SAMSDATA" parallel 12;

    效果不明显,而且影响了已经存在的sql执行,故先删除,其实感觉走了IDX5_SAMS_CHECKINOUT性能能稍微好一点,但是从测试结果上看,可能对原来的执行有影响,所以先不动了




















    附件列表

    • 相关阅读:
      安卓热修复
      Activity四种启动模式
      11、网页制作Dreamweaver(补充:JS零碎知识点&&正则表达式)
      6、C#基础整理(for 语句经典习题--for循环嵌套、穷举)
      5、C#基础整理(for 语句经典习题--与 if 的嵌套)
      4、C#基础整理(if语句经典习题)
      3、C#基础整理(语句概述)
      2、C#基础整理(运算符、数据类型与转换、var关键字)
      1、C#基础整理(进制转换的方法)
      10、网页制作Dreamweaver(扩展:各浏览器对 onunload 事件的支持与触发条件实现有差异)
    • 原文地址:https://www.cnblogs.com/wangrongxin/p/6653965.html
    Copyright © 2020-2023  润新知