数据库优化二

INSERT INTO ATTEMPT_RCO (
    ID
    ,USER_ID
    ,ICR_ID
    ,RCO_ID
    ,RT_TIME
    ,RT_STATUS
    ,RT_SCORE
    ,CLASSROOM_ID
    ,START_DATE
    ,END_DATE
    ,ATTEMPT_NUM
    )
VALUES (
    ATTEMPT_RCO_SEQ.NEXTVAL
    ,:B10
    ,:B9
    ,:B8
    ,:B7
    ,:B6
    ,:B5
    ,:B4
    ,TO_DATE(:B3, 'yyyy-mm-dd hh24:mi:ss')
    ,TO_DATE(:B2, 'yyyy-mm-dd hh24:mi:ss')
    ,:B1
    );

ATTEMPT_RCO 这个表建议使用ID创建hash分区,可以先创建16个分区看看效果如何。

SELECT *
FROM (
    SELECT rownum rn
        ,tmp3.*
    FROM (
        SELECT tmp2.icr_id
            ,tmp2.tbc_id
            ,tmp2.attr_1
            ,tmp2.type
            ,to_char((
                    SELECT max(tcp2.last_study_date)
                    FROM training_class_performance tcp2
                    WHERE tmp2.icr_id = tcp2.icr_id
                        AND tcp2.user_id = : 1
                    ), 'yyyy-MM-dd') last_update_date
            ,(
                SELECT count(f.id)
                FROM favorites f
                WHERE f.icr_id = tmp2.icr_id
                    AND f.create_by = : 2
                ) is_faved
            ,tmp2.course_name
            ,tmp2.duration
            ,tmp2.substr_course_name
            ,to_char(tmp2.start_date, 'yyyy-MM-dd') start_date
            ,to_char(tmp2.end_date, 'yyyy-MM-dd') end_date
            ,tmp2.course_point
            ,(
                SELECT tcp2.c_time
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = : 3
                ) c_time
            ,(
                SELECT tcp2.enroll_type
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = : 4
                ) enroll_type
            ,(
                SELECT decode(tcp2.STATUS, 'P', 'Y', 'C', 'Y', 'F', 'F', 'I', 'N', 'N')
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = : 5
                ) is_pass
            ,(
                SELECT tcp2.learning_progress
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = : 6
                ) learn_progress
            ,trunc((
                    round((
                            SELECT tcp2.c_time
                            FROM training_class_performance tcp2
                            WHERE tmp2.icr_id = tcp2.icr_id
                                AND tcp2.user_id = : 7
                            ) / 60, 2) / tmp2.effectivelength
                    ) * 100, 1) effectivelength
            ,(
                CASE 
                    WHEN (tmp2.start_date IS NULL)
                        OR (
                            tmp2.start_date <= sysdate
                            AND tmp2.end_date IS NULL
                            )
                        OR (
                            tmp2.start_date <= sysdate
                            AND tmp2.end_date >= sysdate
                            )
                        THEN 'Y'
                    ELSE 'N'
                    END
                ) en_start
            ,(
                SELECT floor(sum((
                                SELECT tcp2.c_time
                                FROM training_class_performance tcp2
                                WHERE tmp2.icr_id = tcp2.icr_id
                                    AND tcp2.user_id = : 8
                                )) / 3600)
                FROM dual
                ) hour
            ,(
                SELECT floor(mod(sum((
                                    SELECT tcp2.c_time
                                    FROM training_class_performance tcp2
                                    WHERE tmp2.icr_id = tcp2.icr_id
                                        AND tcp2.user_id = : 9
                                    )), 3600) / 60)
                FROM dual
                ) minute
            ,(
                SELECT mod(sum((
                                SELECT tcp2.c_time
                                FROM training_class_performance tcp2
                                WHERE tmp2.icr_id = tcp2.icr_id
                                    AND tcp2.user_id = : 10
                                )), 60)
                FROM dual
                ) second
            ,tmp2.effectivelength total_study_time
            ,(
                SELECT floor(sum((
                                SELECT tcp2.c_time
                                FROM training_class_performance tcp2
                                WHERE tmp2.icr_id = tcp2.icr_id
                                    AND tcp2.user_id = : 11
                                )) / 60)
                FROM dual
                ) total_minute
            ,(
                SELECT count(1)
                FROM cst_performance cst
                    ,training_class_performance tcp
                WHERE tcp.classroom_id = cst.classroom_id
                    AND tmp2.icr_id = tcp.icr_id
                    AND cst.user_id = : 12
                ) isDropClass
            ,(
                SELECT nvl(tcp2.learning_progress, 0)
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = : 13
                ) learning_progress
            ,(
                SELECT nvl(tcp2.learning_progress_t, 0)
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = : 14
                ) learning_progress_t
        FROM (
            SELECT icr.id icr_id
                ,icr.tbc_id
                ,decode(icr.attr_1, '', 'class01.jpg', icr.attr_1) attr_1
                ,icr.NAME course_name
                ,cb.duration
                ,substr(icr.NAME, 1, 15) substr_course_name
                ,icr.start_date
                ,icr.end_date
                ,icr.course_point
                ,icr.total_study_time
                ,decode(icr.learningthroughtime, NULL, 30, 0, 1, icr.learningthroughtime) effectivelength
                ,t.reg_date
                ,icr.type
            FROM (
                SELECT icr_id
                    ,max(reg_date) reg_date
                FROM (
                    SELECT icr.id icr_id
                        ,reg_date
                    FROM learn_enroll le
                        ,implement_class_relation icr
                    WHERE le.tbc_id = icr.tbc_id
                        AND icr.is_deleted = 'N'
                        AND (
                            icr.end_date IS NULL
                            OR (
                                icr.end_date IS NOT NULL
                                AND icr.end_date > sysdate
                                )
                            )
                        AND le.reg_id IN (
                            : 15
                            ,: 16
                            ,: 17
                            ,: 18
                            ,: 19
                            ,: 20
                            ,: 21
                            )
                        AND le.icr_id = 0
                    UNION ALL
                    SELECT icr_id
                        ,reg_date
                    FROM learn_enroll le
                    WHERE le.icr_id <> 0
                        AND le.reg_id IN (
                            : 22
                            ,: 23
                            ,: 24
                            ,: 25
                            ,: 26
                            ,: 27
                            ,: 28
                            )
                    )
                GROUP BY icr_id
                ) t
                ,implement_class_relation icr
                ,training_bag_class tbc
                ,course_bank cb
            WHERE tbc.id = icr.tbc_id
                AND t.icr_id = icr.id
                AND icr.is_deleted = 'N'
                AND tbc.object_type IN (
                    'O'
                    ,'W'
                    )
                AND tbc.is_deleted = 'N'
                AND (
                    icr.end_date IS NULL
                    OR (
                        icr.end_date IS NOT NULL
                        AND icr.end_date > sysdate
                        )
                    )
                AND (
                    tbc.end_date IS NULL
                    OR (
                        tbc.end_date IS NOT NULL
                        AND tbc.end_date > sysdate
                        )
                    )
                AND tbc.is_published = 'Y'
                AND icr.course_id = cb.id
                AND cb.is_deleted = 'N'
            ORDER BY reg_date DESC
            ) tmp2
        ) tmp3
    WHERE 1 = 1
    )
WHERE rn >= : 29
    AND rn <= : 30;
-------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                           |                            |       |       |   536 (100)|          |
|   1 |  SORT AGGREGATE                            |                            |     1 |    22 |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     1 |    22 |     3   (0)| 00:00:01 |
|   3 |    INDEX UNIQUE SCAN                       | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|   4 |  SORT AGGREGATE                            |                            |     1 |    14 |            |          |
|   5 |   TABLE ACCESS BY INDEX ROWID              | FAVORITES                  |     1 |    14 |     3   (0)| 00:00:01 |
|   6 |    INDEX RANGE SCAN                        | IDEX_ICR_ID                |     2 |       |     1   (0)| 00:00:01 |
|   7 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|   8 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|   9 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    16 |     3   (0)| 00:00:01 |
|  10 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  11 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    16 |     3   (0)| 00:00:01 |
|  12 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  13 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    17 |     3   (0)| 00:00:01 |
|  14 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  15 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  16 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  17 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  18 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  19 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  20 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  21 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  22 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  23 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  24 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  25 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  26 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  27 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  28 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  29 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  30 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  31 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  32 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  33 |  SORT AGGREGATE                            |                            |     1 |    28 |            |          |
|  34 |   HASH JOIN                                |                            |    31 |   868 |     8  (13)| 00:00:01 |
|  35 |    INDEX RANGE SCAN                        | CP_PK                      |    81 |  1134 |     3   (0)| 00:00:01 |
|  36 |    TABLE ACCESS BY INDEX ROWID             | TRAINING_CLASS_PERFORMANCE |   158 |  2212 |     4   (0)| 00:00:01 |
|  37 |     INDEX RANGE SCAN                       | TCP_ICR_ID_IDX             |   158 |       |     3   (0)| 00:00:01 |
|  38 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    17 |     3   (0)| 00:00:01 |
|  39 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  40 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    16 |     3   (0)| 00:00:01 |
|  41 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  42 |  FILTER                                    |                            |       |       |            |          |
|  43 |   VIEW                                     |                            |     1 |  3857 |   536   (1)| 00:00:07 |
|  44 |    COUNT                                   |                            |       |       |            |          |
|  45 |     VIEW                                   |                            |     1 |  3691 |   536   (1)| 00:00:07 |
|  46 |      SORT ORDER BY                         |                            |     1 |   131 |   536   (1)| 00:00:07 |
|  47 |       NESTED LOOPS                         |                            |     1 |   131 |   535   (1)| 00:00:07 |
|  48 |        NESTED LOOPS                        |                            |     1 |   110 |   533   (1)| 00:00:07 |
|  49 |         NESTED LOOPS                       |                            |     1 |    99 |   532   (1)| 00:00:07 |
|  50 |          VIEW                              |                            |    25 |   550 |   481   (1)| 00:00:06 |
|  51 |           HASH GROUP BY                    |                            |    25 |   550 |   481   (1)| 00:00:06 |
|  52 |            VIEW                            |                            |    25 |   550 |   480   (1)| 00:00:06 |
|  53 |             UNION-ALL                      |                            |       |       |            |          |
|  54 |              TABLE ACCESS BY INDEX ROWID   | IMPLEMENT_CLASS_RELATION   |     1 |    24 |    14   (0)| 00:00:01 |
|  55 |               NESTED LOOPS                 |                            |    24 |  1152 |   444   (1)| 00:00:06 |
|  56 |                INLIST ITERATOR             |                            |       |       |            |          |
|  57 |                 TABLE ACCESS BY INDEX ROWID| LEARN_ENROLL               |    29 |   696 |    36   (0)| 00:00:01 |
|  58 |                  INDEX RANGE SCAN          | ENROLL_REG_ID              |    30 |       |     9   (0)| 00:00:01 |
|  59 |                INDEX RANGE SCAN            | ICR_PK                     |    15 |       |     2   (0)| 00:00:01 |
|  60 |              INLIST ITERATOR               |                            |       |       |            |          |
|  61 |               TABLE ACCESS BY INDEX ROWID  | LEARN_ENROLL               |     1 |    18 |    36   (0)| 00:00:01 |
|  62 |                INDEX RANGE SCAN            | ENROLL_REG_ID              |    30 |       |     9   (0)| 00:00:01 |
|  63 |          TABLE ACCESS BY INDEX ROWID       | IMPLEMENT_CLASS_RELATION   |     1 |    77 |     3   (0)| 00:00:01 |
|  64 |           INDEX RANGE SCAN                 | ICR_ID                     |     1 |       |     2   (0)| 00:00:01 |
|  65 |         TABLE ACCESS BY INDEX ROWID        | COURSE_BANK                |     1 |    11 |     1   (0)| 00:00:01 |
|  66 |          INDEX UNIQUE SCAN                 | SYS_C0028643               |     1 |       |     0   (0)|          |
|  67 |        TABLE ACCESS BY INDEX ROWID         | TRAINING_BAG_CLASS         |     1 |    21 |     2   (0)| 00:00:01 |
|  68 |         INDEX UNIQUE SCAN                  | SYS_C0032569               |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------------------------   

该查询使用索引TCP_UN进行INDEX UNIQUE SCAN,建议表TRAINING_CLASS_PERFORMANCE按照ICR_ID,USER_ID创建hash分区。可先创建16个分区。

UPDATE TRAINING_CLASS_PERFORMANCE TCP
SET TCP.LAST_UPDATE_RCO = :B9
    ,TCP.C_TIME = NVL(TCP.C_TIME, 0) + :B8
    ,TCP.C_TIMES = NVL(TCP.C_TIMES, 0) + :B7
    ,TCP.LAST_UPDATEED_BY = :B2
    ,TCP.LEARNING_PROGRESS = ROUND(:B6, 1)
    ,TCP.LEARNING_PROGRESS_T = ROUND(:B5, 1)
    ,TCP.LAST_UPDATE_DATE = SYSDATE
    ,TCP.LAST_STUDY_DATE = SYSDATE
    ,TCP.CLASSROOM_ID = NVL(TCP.CLASSROOM_ID, :B4)
    ,TCP.IS_LEARN = 1
    ,TCP.ENROLL_TYPE = DECODE(TCP.IS_DELETED, 'Y', 'R', TCP.ENROLL_TYPE)
    ,TCP.STATUS = DECODE(TCP.STATUS, 'I', 'F', TCP.STATUS)
    ,TCP.IS_DELETED = 'N'
WHERE TCP.ICR_ID = :B3
    AND TCP.USER_ID = :B2
    AND TCP.TBC_ID = :B1;

同样,该语句使用TCP_UN索引查询,上面的优化对此sql同样有效


查看awr发现有几个对象的gc等待比较高的

数据库优化二_第1张图片
gc等待

主要是以下三个对象
IDX_ILA_USERNAME
IDX_ILA_FIRST_NAME
ILA_USER

查看以下这几个对象之间的关系

select table_name,INDEX_NAME from dba_indexes where index_name IN ('IDX_ILA_USERNAME','IDX_ILA_FIRST_NAME') and owner='ILEARN_TRA';

TABLE_NAME                     INDEX_NAME
------------------------------ ------------------------------------------------------------------------------------------
ILA_USER                       IDX_ILA_FIRST_NAME
ILA_USER                       IDX_ILA_USERNAME


UPDATE ila_user u
SET last_request_date = trunc(sysdate)
    ,LAST_LOGIN = sysdate
WHERE u.id = : 1;


select * from table(dbms_xplan.display_awr('68br5jm77bq0m'));

---------------------------------------------------------------------------------
| Id  | Operation         | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------
|   0 | UPDATE STATEMENT  |             |       |       |     4 (100)|          |
|   1 |  UPDATE           | ILA_USER    |       |       |            |          |
|   2 |   INDEX RANGE SCAN| TB_SITE_IDX |     1 |    11 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------

查看索引和表的创建sql

CREATE INDEX "ILEARN_TRA"."IDX_ILA_FIRST_NAME" ON "ILEARN_TRA"."ILA_USER" ("FIRST_NAME")
CREATE INDEX "ILEARN_TRA"."IDX_ILA_USERNAME" ON "ILEARN_TRA"."ILA_USER" ("USERNAME")

表ILA_USER存在主键,主键列是CONSTRAINT "ILA_USER_PK" PRIMARY KEY ("ID")

以上建议全部修改为hash分区

select * from table(dbms_xplan.display_awr('14yx2vjbnycqh'));
-------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                  | Name                       | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                           |                            |       |       |   536 (100)|          |
|   1 |  SORT AGGREGATE                            |                            |     1 |    22 |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     1 |    22 |     3   (0)| 00:00:01 |
|   3 |    INDEX UNIQUE SCAN                       | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|   4 |  SORT AGGREGATE                            |                            |     1 |    14 |            |          |
|   5 |   TABLE ACCESS BY INDEX ROWID              | FAVORITES                  |     1 |    14 |     3   (0)| 00:00:01 |
|   6 |    INDEX RANGE SCAN                        | IDEX_ICR_ID                |     2 |       |     1   (0)| 00:00:01 |
|   7 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|   8 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|   9 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    16 |     3   (0)| 00:00:01 |
|  10 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  11 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    16 |     3   (0)| 00:00:01 |
|  12 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  13 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    17 |     3   (0)| 00:00:01 |
|  14 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  15 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  16 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  17 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  18 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  19 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  20 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  21 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  22 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  23 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  24 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  25 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  26 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  27 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  28 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  29 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    18 |     3   (0)| 00:00:01 |
|  30 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  31 |  SORT AGGREGATE                            |                            |     1 |       |            |          |
|  32 |   FAST DUAL                                |                            |     1 |       |     2   (0)| 00:00:01 |
|  33 |  SORT AGGREGATE                            |                            |     1 |    28 |            |          |
|  34 |   HASH JOIN                                |                            |    31 |   868 |     8  (13)| 00:00:01 |
|  35 |    INDEX RANGE SCAN                        | CP_PK                      |    81 |  1134 |     3   (0)| 00:00:01 |
|  36 |    TABLE ACCESS BY INDEX ROWID             | TRAINING_CLASS_PERFORMANCE |   158 |  2212 |     4   (0)| 00:00:01 |
|  37 |     INDEX RANGE SCAN                       | TCP_ICR_ID_IDX             |   158 |       |     3   (0)| 00:00:01 |
|  38 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    17 |     3   (0)| 00:00:01 |
|  39 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  40 |  TABLE ACCESS BY INDEX ROWID               | TRAINING_CLASS_PERFORMANCE |     1 |    16 |     3   (0)| 00:00:01 |
|  41 |   INDEX UNIQUE SCAN                        | TCP_UN                     |     1 |       |     2   (0)| 00:00:01 |
|  42 |  FILTER                                    |                            |       |       |            |          |
|  43 |   VIEW                                     |                            |     1 |  3857 |   536   (1)| 00:00:07 |
|  44 |    COUNT                                   |                            |       |       |            |          |
|  45 |     VIEW                                   |                            |     1 |  3691 |   536   (1)| 00:00:07 |
|  46 |      SORT ORDER BY                         |                            |     1 |   131 |   536   (1)| 00:00:07 |
|  47 |       NESTED LOOPS                         |                            |     1 |   131 |   535   (1)| 00:00:07 |
|  48 |        NESTED LOOPS                        |                            |     1 |   110 |   533   (1)| 00:00:07 |
|  49 |         NESTED LOOPS                       |                            |     1 |    99 |   532   (1)| 00:00:07 |
|  50 |          VIEW                              |                            |    25 |   550 |   481   (1)| 00:00:06 |
|  51 |           HASH GROUP BY                    |                            |    25 |   550 |   481   (1)| 00:00:06 |
|  52 |            VIEW                            |                            |    25 |   550 |   480   (1)| 00:00:06 |
|  53 |             UNION-ALL                      |                            |       |       |            |          |
|  54 |              TABLE ACCESS BY INDEX ROWID   | IMPLEMENT_CLASS_RELATION   |     1 |    24 |    14   (0)| 00:00:01 |
|  55 |               NESTED LOOPS                 |                            |    24 |  1152 |   444   (1)| 00:00:06 |
|  56 |                INLIST ITERATOR             |                            |       |       |            |          |
|  57 |                 TABLE ACCESS BY INDEX ROWID| LEARN_ENROLL               |    29 |   696 |    36   (0)| 00:00:01 |
|  58 |                  INDEX RANGE SCAN          | ENROLL_REG_ID              |    30 |       |     9   (0)| 00:00:01 |
|  59 |                INDEX RANGE SCAN            | ICR_PK                     |    15 |       |     2   (0)| 00:00:01 |
|  60 |              INLIST ITERATOR               |                            |       |       |            |          |
|  61 |               TABLE ACCESS BY INDEX ROWID  | LEARN_ENROLL               |     1 |    18 |    36   (0)| 00:00:01 |
|  62 |                INDEX RANGE SCAN            | ENROLL_REG_ID              |    30 |       |     9   (0)| 00:00:01 |
|  63 |          TABLE ACCESS BY INDEX ROWID       | IMPLEMENT_CLASS_RELATION   |     1 |    77 |     3   (0)| 00:00:01 |
|  64 |           INDEX RANGE SCAN                 | ICR_ID                     |     1 |       |     2   (0)| 00:00:01 |
|  65 |         TABLE ACCESS BY INDEX ROWID        | COURSE_BANK                |     1 |    11 |     1   (0)| 00:00:01 |
|  66 |          INDEX UNIQUE SCAN                 | SYS_C0028643               |     1 |       |     0   (0)|          |
|  67 |        TABLE ACCESS BY INDEX ROWID         | TRAINING_BAG_CLASS         |     1 |    21 |     2   (0)| 00:00:01 |
|  68 |         INDEX UNIQUE SCAN                  | SYS_C0032569               |     1 |       |     1   (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------------------------------

看看sql的统计信息

数据库优化二_第2张图片
SQL统计信息

该SQL的执行统计信息明显是不正常的。逻辑读过高,一方面存在数据重复扫描的情况,另外,有可能限制的预估执行计划不是真实的执行情况。

检查表TRAINING_CLASS_PERFORMANCE的统计信息时间

SQL> select last_analyzed from dba_tables where table_name ='TRAINING_CLASS_PERFORMANCE' and owner ='ILEARN_TRA';

LAST_ANALYZED
------------------
18-NOV-15

统计信息比较陈旧了,查看是否被锁定

SQL> select STATTYPE_LOCKED,TABLE_NAME,owner from dba_tab_statistics where table_name ='TRAINING_CLASS_PERFORMANCE' and owner ='ILEARN_TRA';
STATTYPE_LOCKED TABLE_NAME                     OWNER
--------------- ------------------------------ ---------------
ALL             TRAINING_CLASS_PERFORMANCE     ILEARN_TRA

这个表的统计信息已经被锁定,后续准备定位人为锁定原因。现在检查该SQL的真实执行计划

select datatype_string,name,value_string from v$sql_bind_capture where sql_id='14yx2vjbnycqh';
DATATYPE_STRING                NAME  VALUE_STRING
------------------------------ ----- --------------------------------------------------
NUMBER                         :1    277406254
NUMBER                         :2    277406254
NUMBER                         :3    277406254
NUMBER                         :4    277406254
NUMBER                         :5    277406254
NUMBER                         :6    277406254
NUMBER                         :7    277406254
NUMBER                         :8    277406254
NUMBER                         :9    277406254
NUMBER                         :10   277406254
NUMBER                         :11   277406254
NUMBER                         :12   277406254
NUMBER                         :13   277406254
NUMBER                         :14   277406254
VARCHAR2(32)                   :15   277406254
VARCHAR2(32)                   :16   181
VARCHAR2(32)                   :17   181
VARCHAR2(32)                   :18   11836001
VARCHAR2(32)                   :19   11836952
VARCHAR2(32)                   :20   11836982
VARCHAR2(32)                   :21   11836989
VARCHAR2(32)                   :22   277406254
VARCHAR2(32)                   :23   181
VARCHAR2(32)                   :24   181
VARCHAR2(32)                   :25   11836001
VARCHAR2(32)                   :26   11836952
VARCHAR2(32)                   :27   11836982
VARCHAR2(32)                   :28   11836989
NUMBER                         :29   1
NUMBER                         :30   10

先把这些绑定变量的值带入到sql里面

SELECT /*+ gather_plan_statistics */ *
FROM (
    SELECT rownum rn
        ,tmp3.*
    FROM (
        SELECT tmp2.icr_id
            ,tmp2.tbc_id
            ,tmp2.attr_1
            ,tmp2.type
            ,to_char((
                    SELECT max(tcp2.last_study_date)
                    FROM training_class_performance tcp2
                    WHERE tmp2.icr_id = tcp2.icr_id
                        AND tcp2.user_id = 277406254
                    ), 'yyyy-MM-dd') last_update_date
            ,(
                SELECT count(f.id)
                FROM favorites f
                WHERE f.icr_id = tmp2.icr_id
                    AND f.create_by = 277406254
                ) is_faved
            ,tmp2.course_name
            ,tmp2.duration
            ,tmp2.substr_course_name
            ,to_char(tmp2.start_date, 'yyyy-MM-dd') start_date
            ,to_char(tmp2.end_date, 'yyyy-MM-dd') end_date
            ,tmp2.course_point
            ,(
                SELECT tcp2.c_time
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = 277406254
                ) c_time
            ,(
                SELECT tcp2.enroll_type
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = 277406254
                ) enroll_type
            ,(
                SELECT decode(tcp2.STATUS, 'P', 'Y', 'C', 'Y', 'F', 'F', 'I', 'N', 'N')
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = 277406254
                ) is_pass
            ,(
                SELECT tcp2.learning_progress
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = 277406254
                ) learn_progress
            ,trunc((
                    round((
                            SELECT tcp2.c_time
                            FROM training_class_performance tcp2
                            WHERE tmp2.icr_id = tcp2.icr_id
                                AND tcp2.user_id = 277406254
                            ) / 60, 2) / tmp2.effectivelength
                    ) * 100, 1) effectivelength
            ,(
                CASE 
                    WHEN (tmp2.start_date IS NULL)
                        OR (
                            tmp2.start_date <= sysdate
                            AND tmp2.end_date IS NULL
                            )
                        OR (
                            tmp2.start_date <= sysdate
                            AND tmp2.end_date >= sysdate
                            )
                        THEN 'Y'
                    ELSE 'N'
                    END
                ) en_start
            ,(
                SELECT floor(sum((
                                SELECT tcp2.c_time
                                FROM training_class_performance tcp2
                                WHERE tmp2.icr_id = tcp2.icr_id
                                    AND tcp2.user_id = 277406254
                                )) / 3600)
                FROM dual
                ) hour
            ,(
                SELECT floor(mod(sum((
                                    SELECT tcp2.c_time
                                    FROM training_class_performance tcp2
                                    WHERE tmp2.icr_id = tcp2.icr_id
                                        AND tcp2.user_id = 277406254
                                    )), 3600) / 60)
                FROM dual
                ) minute
            ,(
                SELECT mod(sum((
                                SELECT tcp2.c_time
                                FROM training_class_performance tcp2
                                WHERE tmp2.icr_id = tcp2.icr_id
                                    AND tcp2.user_id = 277406254
                                )), 60)
                FROM dual
                ) second
            ,tmp2.effectivelength total_study_time
            ,(
                SELECT floor(sum((
                                SELECT tcp2.c_time
                                FROM training_class_performance tcp2
                                WHERE tmp2.icr_id = tcp2.icr_id
                                    AND tcp2.user_id = 277406254
                                )) / 60)
                FROM dual
                ) total_minute
            ,(
                SELECT count(1)
                FROM cst_performance cst
                    ,training_class_performance tcp
                WHERE tcp.classroom_id = cst.classroom_id
                    AND tcp.icr_id = tmp2.icr_id
                    AND cst.user_id = 277406254
                ) isDropClass
            ,(
                SELECT nvl(tcp2.learning_progress, 0)
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = 277406254
                ) learning_progress
            ,(
                SELECT nvl(tcp2.learning_progress_t, 0)
                FROM training_class_performance tcp2
                WHERE tmp2.icr_id = tcp2.icr_id
                    AND tcp2.user_id = 277406254
                ) learning_progress_t
        FROM (
            SELECT icr.id icr_id
                ,icr.tbc_id
                ,decode(icr.attr_1, '', 'class01.jpg', icr.attr_1) attr_1
                ,icr.NAME course_name
                ,cb.duration
                ,substr(icr.NAME, 1, 15) substr_course_name
                ,icr.start_date
                ,icr.end_date
                ,icr.course_point
                ,icr.total_study_time
                ,decode(icr.learningthroughtime, NULL, 30, 0, 1, icr.learningthroughtime) effectivelength
                ,t.reg_date
                ,icr.type
            FROM (
                SELECT icr_id,max(reg_date) reg_date
                FROM (
                    SELECT icr.id icr_id
                        ,reg_date
                    FROM learn_enroll le
                        ,implement_class_relation icr
                    WHERE le.tbc_id = icr.tbc_id
                        AND icr.is_deleted = 'N'
                        AND (
                            icr.end_date IS NULL
                            OR (
                                icr.end_date IS NOT NULL
                                AND icr.end_date > sysdate
                                )
                            )
                        AND le.reg_id IN (
                            '277406254'
                            ,'181'
                            ,'181'
                            ,'11836001'
                            ,'11836952'
                            ,'11836982'
                            ,'11836989'
                            )
                        AND le.icr_id = 0
                    UNION ALL
                    SELECT icr_id
                        ,reg_date
                    FROM learn_enroll le
                    WHERE le.icr_id <> 0
                        AND le.reg_id IN (
                            '277406254'
                            ,'181'
                            ,'181'
                            ,'11836001'
                            ,'11836952'
                            ,'11836982'
                            ,'11836989'
                            )
                    )
                GROUP BY icr_id
                ) t
                ,implement_class_relation icr
                ,training_bag_class tbc
                ,course_bank cb
            WHERE tbc.id = icr.tbc_id
                AND t.icr_id = icr.id
                AND icr.is_deleted = 'N'
                AND tbc.object_type IN (
                    'O'
                    ,'W'
                    )
                AND tbc.is_deleted = 'N'
                AND (
                    icr.end_date IS NULL
                    OR (
                        icr.end_date IS NOT NULL
                        AND icr.end_date > sysdate
                        )
                    )
                AND (
                    tbc.end_date IS NULL
                    OR (
                        tbc.end_date IS NOT NULL
                        AND tbc.end_date > sysdate
                        )
                    )
                AND tbc.is_published = 'Y'
                AND icr.course_id = cb.id
                AND cb.is_deleted = 'N'
            ORDER BY reg_date DESC
            ) tmp2
        ) tmp3
    WHERE 1 = 1
    )
WHERE rn >= 1
    AND rn <= 10;

select * from table(dbms_xplan.display_cursor(null,null,'iostats'));

----------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                                 | Name                       | Starts | E-Rows | A-Rows |   A-Time   | Buffers |
----------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                          |                            |      1 |        |     10 |00:00:00.03 |   92617 |
|   1 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.01 |     254 |
|   2 |   TABLE ACCESS BY INDEX ROWID             | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|*  3 |    INDEX UNIQUE SCAN                      | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|   4 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.01 |      65 |
|*  5 |   TABLE ACCESS BY INDEX ROWID             | FAVORITES                  |     63 |      1 |      0 |00:00:00.01 |      65 |
|*  6 |    INDEX RANGE SCAN                       | IDEX_ICR_ID                |     63 |      2 |      0 |00:00:00.01 |      65 |
|   7 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|*  8 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|   9 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 10 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  11 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 12 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  13 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 14 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  15 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 16 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  17 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 18 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  19 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.01 |     254 |
|  20 |   FAST DUAL                               |                            |     63 |      1 |     63 |00:00:00.01 |       0 |
|  21 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 22 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  23 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.01 |     254 |
|  24 |   FAST DUAL                               |                            |     63 |      1 |     63 |00:00:00.01 |       0 |
|  25 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 26 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  27 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.01 |     254 |
|  28 |   FAST DUAL                               |                            |     63 |      1 |     63 |00:00:00.01 |       0 |
|  29 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 30 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  31 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.01 |     254 |
|  32 |   FAST DUAL                               |                            |     63 |      1 |     63 |00:00:00.01 |       0 |
|  33 |  SORT AGGREGATE                           |                            |     63 |      1 |     63 |00:00:00.86 |   88632 |
|* 34 |   HASH JOIN                               |                            |     63 |     38 |    130K|00:00:00.82 |   88632 |
|* 35 |    INDEX RANGE SCAN                       | CP_PK                      |     63 |     86 |   5733 |00:00:00.01 |     189 |
|  36 |    TABLE ACCESS BY INDEX ROWID            | TRAINING_CLASS_PERFORMANCE |     63 |    158 |    103K|00:00:00.63 |   88443 |
|* 37 |     INDEX RANGE SCAN                      | TCP_ICR_ID_IDX             |     63 |    158 |    103K|00:00:00.01 |    1524 |
|  38 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 39 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|  40 |  TABLE ACCESS BY INDEX ROWID              | TRAINING_CLASS_PERFORMANCE |     63 |      1 |     63 |00:00:00.01 |     254 |
|* 41 |   INDEX UNIQUE SCAN                       | TCP_UN                     |     63 |      1 |     63 |00:00:00.01 |     191 |
|* 42 |  VIEW                                     |                            |      1 |      1 |     10 |00:00:00.03 |   92617 |
|  43 |   COUNT                                   |                            |      1 |        |     63 |00:00:00.01 |     872 |
|  44 |    VIEW                                   |                            |      1 |      1 |     63 |00:00:00.01 |     872 |
|  45 |     SORT ORDER BY                         |                            |      1 |      1 |     63 |00:00:00.01 |     872 |
|  46 |      NESTED LOOPS                         |                            |      1 |      1 |     63 |00:00:00.01 |     872 |
|  47 |       NESTED LOOPS                        |                            |      1 |      1 |     64 |00:00:00.01 |     678 |
|  48 |        NESTED LOOPS                       |                            |      1 |      1 |     66 |00:00:00.01 |     546 |
|  49 |         VIEW                              |                            |      1 |     19 |     66 |00:00:00.01 |     345 |
|  50 |          HASH GROUP BY                    |                            |      1 |     19 |     66 |00:00:00.01 |     345 |
|  51 |           VIEW                            |                            |      1 |     19 |     66 |00:00:00.01 |     345 |
|  52 |            UNION-ALL                      |                            |      1 |        |     66 |00:00:00.01 |     345 |
|* 53 |             TABLE ACCESS BY INDEX ROWID   | IMPLEMENT_CLASS_RELATION   |      1 |      1 |      3 |00:00:00.01 |     276 |
|  54 |              NESTED LOOPS                 |                            |      1 |     18 |    172 |00:00:00.01 |     195 |
|  55 |               INLIST ITERATOR             |                            |      1 |        |     62 |00:00:00.01 |      69 |
|* 56 |                TABLE ACCESS BY INDEX ROWID| LEARN_ENROLL               |      6 |     24 |     62 |00:00:00.01 |      69 |
|* 57 |                 INDEX RANGE SCAN          | ENROLL_REG_ID              |      6 |     25 |    125 |00:00:00.01 |      18 |
|* 58 |               INDEX RANGE SCAN            | ICR_PK                     |     62 |     15 |    109 |00:00:00.01 |     126 |
|  59 |             INLIST ITERATOR               |                            |      1 |        |     63 |00:00:00.01 |      69 |
|* 60 |              TABLE ACCESS BY INDEX ROWID  | LEARN_ENROLL               |      6 |      1 |     63 |00:00:00.01 |      69 |
|* 61 |               INDEX RANGE SCAN            | ENROLL_REG_ID              |      6 |     25 |    125 |00:00:00.01 |      18 |
|* 62 |         TABLE ACCESS BY INDEX ROWID       | IMPLEMENT_CLASS_RELATION   |     66 |      1 |     66 |00:00:00.01 |     201 |
|* 63 |          INDEX RANGE SCAN                 | ICR_ID                     |     66 |      1 |     66 |00:00:00.01 |     135 |
|* 64 |        TABLE ACCESS BY INDEX ROWID        | COURSE_BANK                |     66 |      1 |     64 |00:00:00.01 |     132 |
|* 65 |         INDEX UNIQUE SCAN                 | SYS_C0028643               |     66 |      1 |     64 |00:00:00.01 |      68 |
|* 66 |       TABLE ACCESS BY INDEX ROWID         | TRAINING_BAG_CLASS         |     64 |      1 |     63 |00:00:00.01 |     194 |
|* 67 |        INDEX UNIQUE SCAN                  | SYS_C0032569               |     64 |      1 |     64 |00:00:00.01 |     130 |
----------------------------------------------------------------------------------------------------------------------------------

你可能感兴趣的:(数据库优化二)