sql长时间运行查询需要优化

时间:2014-08-12 14:13:14

标签: sql performance optimization query-optimization sql-execution-plan

Oracle查询。

以下查询需要一些时间才能执行:

    SELECT GCCOM_ACCOUNT_CONTRACT.ID_ACCOUNT_CONTRACT
    FROM GCCOM_ACCOUNT_CONTRACT, GCCOM_ACCOUNT_GROUP
    WHERE
        GCCOM_ACCOUNT_CONTRACT.ID_ACCOUNT_GROUP = GCCOM_ACCOUNT_GROUP.ID_ACCOUNT_GROUP (+) AND
        EXISTS (SELECT 1 FROM GCCOM_CONTRACTED_SERVICE
                WHERE ID_ACCOUNT_CONTRACT = GCCOM_ACCOUNT_CONTRACT.ID_ACCOUNT_CONTRACT AND
                      STATUS = 'ESTSC00002' AND
                      DROP_DATE IS NULL ) AND
        EXISTS (SELECT 1 FROM GCCOM_SEND_SERVICE
                WHERE (ID_ACCOUNT_CONTRACT = GCCOM_ACCOUNT_CONTRACT.ID_ACCOUNT_CONTRACT OR
                       ID_ACCOUNT_GROUP = GCCOM_ACCOUNT_GROUP.ID_ACCOUNT_GROUP)  
                       ) AND
        (( GCCOM_ACCOUNT_CONTRACT.ACCOUNT_CODE between 200000001  AND 900468243))
    ORDER BY
        GCCOM_ACCOUNT_CONTRACT.ID_COMPANY,
        GCCOM_ACCOUNT_GROUP.ID_ACCOUNT_GROUP,
        GCCOM_ACCOUNT_CONTRACT.ACCOUNT_CODE

解释计划如下:

    PLAN_TABLE_OUTPUT
    ----------------------------------------------------------------------------------------------------------------------------------------------
    Plan hash value: 391653930
    ------------------------------------------------------------------------------------------------------------------------------------------
    | Id  | Operation                             | Name                     | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     | Pstart| Pstop |
    ------------------------------------------------------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                      |                          |     1 |    45 |       |   570K  (1)| 01:54:06 |       |       |
    |   1 |  SORT ORDER BY                        |                          |     1 |    45 |    12M|   570K  (1)| 01:54:06 |       |       |
    |*  2 |   FILTER                              |                          |       |       |       |         |     |       |       |
    |   3 |    NESTED LOOPS OUTER                 |                          |   255K|    10M|       | 17381   (1)| 00:03:29 |       |       |
    |*  4 |     HASH JOIN RIGHT SEMI              |                          |   255K|  9979K|  8648K| 17380   (1)| 00:03:29 |       |       |
    |   5 |      PARTITION HASH ALL               |                          |   260K|  5592K|       |  7175   (1)| 00:01:27 |     1 |    16 |
    |*  6 |       TABLE ACCESS FULL               | GCCOM_CONTRACTED_SERVICE |   260K|  5592K|       |  7175   (1)| 00:01:27 |     1 |    16 |
    |*  7 |      TABLE ACCESS FULL                | GCCOM_ACCOUNT_CONTRACT   |   810K|    13M|       |  8627   (1)| 00:01:44 |       |       |
    |*  8 |     INDEX UNIQUE SCAN                 | PK_GCCOM_ACCOUNT_GROUP   |     1 |     5 |       |     1   (0)| 00:00:01 |       |       |
    |   9 |    CONCATENATION                      |                          |       |       |       |         |     |       |       |
    |  10 |     TABLE ACCESS BY GLOBAL INDEX ROWID| GCCOM_SEND_SERVICE       |     1 |     7 |       |     1   (0)| 00:00:01 | ROWID | ROWID |
    |* 11 |      INDEX RANGE SCAN                 | IDX_GCCOMSENDSERVICE_27  |     1 |       |       |     1   (0)| 00:00:01 |       |       |
    |* 12 |     TABLE ACCESS BY GLOBAL INDEX ROWID| GCCOM_SEND_SERVICE       |     2 |    14 |       |     1   (0)| 00:00:01 | ROWID | ROWID |
    |* 13 |      INDEX RANGE SCAN                 | IDX_GCCOMSENDSERVICE_04  |     1 |       |       |     1   (0)| 00:00:01 |       |       |
    ------------------------------------------------------------------------------------------------------------------------------------------

    Predicate Information (identified by operation id):

    PLAN_TABLE_OUTPUT
    ----------------------------------------------------------------------------------------------------------------------------------------------
    ---------------------------------------------------

       2 - filter( EXISTS (SELECT 0 FROM "GCCOM_SEND_SERVICE" "GCCOM_SEND_SERVICE"<not feasible>)
       4 - access("ID_ACCOUNT_CONTRACT"="GCCOM_ACCOUNT_CONTRACT"."ID_ACCOUNT_CONTRACT")
       6 - filter("DROP_DATE" IS NULL AND "STATUS"='ESTSC00002')
       7 - filter("GCCOM_ACCOUNT_CONTRACT"."ACCOUNT_CODE">=200000001 AND "GCCOM_ACCOUNT_CONTRACT"."ACCOUNT_CODE"<=900468243)
       8 - access("GCCOM_ACCOUNT_CONTRACT"."ID_ACCOUNT_GROUP"="GCCOM_ACCOUNT_GROUP"."ID_ACCOUNT_GROUP"(+))
      11 - access("ID_ACCOUNT_CONTRACT"=:B1)
      12 - filter(LNNVL("ID_ACCOUNT_CONTRACT"=:B1))
      13 - access("ID_ACCOUNT_GROUP"=:B1)

    32 filas seleccionadas.

表格的平均基数如下:

select count(*) from GCCOM_ACCOUNT_CONTRACT >> rows: 810412

select avg(distinct ID_ACCOUNT_GROUP) from GCCOM_ACCOUNT_CONTRACT >> cardinality: 87173

高度索引。

尝试了许多事情,但没用。

有什么想法吗?

0 个答案:

没有答案