分区表上的低效SQL计划

时间:2013-11-14 12:22:31

标签: oracle oracle11g oracle10g

我们面临的问题是一个连接在几个表之间的查询。 即使表格中有数百条记录,计划也要进行合并加入,只考虑表格中的一条记录,请查看以下计划。 当使用合并排序计划时,查询将失败并出现临时空间问题。

oracle仅在作业加载到新创建的分区时选择合并计划。但其余的旧分区正在选择Hash Join,我们可以在几秒钟内获得结果。

获取信息。所有连接的表都具有相同的音量。

你能解释一下为什么会这样吗?

Merg join( query hung)
-----------------------------------------------------------------------------------------------------------------------------                                                                                                                                                                                
| Id  | Operation                   | Name                          | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |                                                                                                                                                                                
-----------------------------------------------------------------------------------------------------------------------------                                                                                                                                                                                
|   0 | SELECT STATEMENT            |                               |     1 |   712 |    36   (3)| 00:00:01 |       |       |                                                                                                                                                                                
|*  1 |  HASH JOIN                  |                               |     1 |   712 |    36   (3)| 00:00:01 |       |       |                                                                                                                                                                                
|   2 |   MERGE JOIN CARTESIAN      |                               |     1 |   679 |    28   (0)| 00:00:01 |       |       |                                                                                                                                                                                
|   3 |    MERGE JOIN CARTESIAN     |                               |     1 |   615 |    21   (0)| 00:00:01 |       |       |                                                                                                                                                                                
|   4 |     MERGE JOIN CARTESIAN    |                               |     1 |   388 |    14   (0)| 00:00:01 |       |       |                                                                                                                                                                                
|   5 |      PARTITION RANGE SINGLE |                               |     1 |   105 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|*  6 |       TABLE ACCESS FULL     | PCA_DCM_CLNT_BSPKE_REFS_M_LND |     1 |   105 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|   7 |      BUFFER SORT            |                               |     1 |   283 |     7   (0)| 00:00:01 |       |       |                                                                                                                                                                                
|   8 |       PARTITION RANGE SINGLE|                               |     1 |   283 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|*  9 |        TABLE ACCESS FULL    | PCA_DCM_INDBTDNS_BLK_M_LND    |     1 |   283 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|  10 |     BUFFER SORT             |                               |     1 |   227 |    14   (0)| 00:00:01 |       |       |                                                                                                                                                                                
|  11 |      PARTITION RANGE SINGLE |                               |     1 |   227 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|* 12 |       TABLE ACCESS FULL     | PCA_DCM_DELPHI_BLK_M_LND      |     1 |   227 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|  13 |    BUFFER SORT              |                               |     1 |    64 |    21   (0)| 00:00:01 |       |       |                                                                                                                                                                                
|  14 |     PARTITION RANGE SINGLE  |                               |     1 |    64 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|* 15 |      TABLE ACCESS FULL      | PCA_DCM_APACS_BLK_M_LND       |     1 |    64 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|  16 |   PARTITION RANGE SINGLE    |                               |     1 |    33 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
|* 17 |    TABLE ACCESS FULL        | PCA_DCM_SCORE_BLK_M_LND       |     1 |    33 |     7   (0)| 00:00:01 |     4 |     4 |                                                                                                                                                                                
-----------------------------------------------------------------------------------------------------------------------------             

哈希联接(我们得到结果的时间很短)

----------------------------------------------------------------------------------------------------------------------------                                                                                                                                                                                 
| Id  | Operation                  | Name                          | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |                                                                                                                                                                                 
----------------------------------------------------------------------------------------------------------------------------                                                                                                                                                                                 
|   0 | SELECT STATEMENT           |                               |   491 |   341K|    74   (3)| 00:00:01 |       |       |                                                                                                                                                                                 
|*  1 |  HASH JOIN                 |                               |   491 |   341K|    74   (3)| 00:00:01 |       |       |                                                                                                                                                                                 
|   2 |   PARTITION RANGE SINGLE   |                               |   493 |   109K|    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|*  3 |    TABLE ACCESS FULL       | PCA_DCM_DELPHI_BLK_M_LND      |   493 |   109K|    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|*  4 |   HASH JOIN                |                               |   491 |   232K|    60   (4)| 00:00:01 |       |       |                                                                                                                                                                                 
|*  5 |    HASH JOIN               |                               |   492 | 99384 |    45   (3)| 00:00:01 |       |       |                                                                                                                                                                                 
|*  6 |     HASH JOIN              |                               |   492 | 47724 |    31   (4)| 00:00:01 |       |       |                                                                                                                                                                                 
|   7 |      PARTITION RANGE SINGLE|                               |   493 | 16269 |    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|*  8 |       TABLE ACCESS FULL    | PCA_DCM_SCORE_BLK_M_LND       |   493 | 16269 |    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|   9 |      PARTITION RANGE SINGLE|                               |   493 | 31552 |    16   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|* 10 |       TABLE ACCESS FULL    | PCA_DCM_APACS_BLK_M_LND       |   493 | 31552 |    16   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|  11 |     PARTITION RANGE SINGLE |                               |   493 | 51765 |    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|* 12 |      TABLE ACCESS FULL     | PCA_DCM_CLNT_BSPKE_REFS_M_LND |   493 | 51765 |    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|  13 |    PARTITION RANGE SINGLE  |                               |   493 |   136K|    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
|* 14 |     TABLE ACCESS FULL      | PCA_DCM_INDBTDNS_BLK_M_LND    |   493 |   136K|    14   (0)| 00:00:01 |     3 |     3 |                                                                                                                                                                                 
----------------------------------------------------------------------------------------------------------------------------                                                                                                                                                                                 

请查询

SELECT
substr(BLK.ACC_NUM,1,14) AS ACCOUNT_NUMBER,
CASE WHEN SUBSTR(BLK.ACC_NUM,20,1) = '1'   THEN 'F1'  
        WHEN SUBSTR(BLK.ACC_NUM,20,1) = ' '   THEN 'F1'  
        WHEN SUBSTR(BLK.ACC_NUM,20,1) = '0'   THEN 'F1'  
         WHEN SUBSTR(BLK.ACC_NUM,20,1) = '2'   THEN 'F2'
   END FLTR,
DELPHI.ND_SPA_CII_SPA
FROM
BUR_LND.PCA_DCM_SCORE_BLK_M_LND BLK
INNER JOIN BUR_LND.PCA_DCM_CLNT_BSPKE_REFS_M_LND REFFS
ON BLK.ACC_NUM= REFFS.ACC_NUM
INNER JOIN BUR_LND.PCA_DCM_INDBTDNS_BLK_M_LND IND
ON BLK.ACC_NUM= IND.ACC_NUM
INNER JOIN BUR_LND.PCA_DCM_DELPHI_BLK_M_LND DELPHI
ON BLK.ACC_NUM= DELPHI.ACC_NUM
INNER JOIN BUR_LND.PCA_DCM_APACS_BLK_M_LND APACS
ON BLK.ACC_NUM= APACS.ACC_NUM
WHERE 
BLK.EFF_DT=TO_DATE('2013-10-30','YYYY-MM-DD')
AND REFFS.EFF_DT=TO_DATE('2013-10-30','YYYY-MM-DD')
AND IND.EFF_DT=TO_DATE('2013-10-30','YYYY-MM-DD')
AND DELPHI.EFF_DT=TO_DATE('2013-10-30','YYYY-MM-DD')
AND APACS.EFF_DT=TO_DATE('2013-10-30','YYYY-MM-DD')

提前感谢您的帮助。

由于 arkesh

1 个答案:

答案 0 :(得分:1)

计划很糟糕,因为新分区缺少统计信息。应在分区更改后更新统计信息,最好使用增量统计信息。如果那不可能,那么像/*+ dynamic_sampling(4) */这样的提示可以提供帮助。

统计数据可能准确,不准确或缺失。由于动态采样,缺少统计数据通常不是一个大问题。使用默认动态采样级别2,如果语句包含没有统计信息的表,Oracle将自动收集统计信息。

不幸的是,对于这种情况,Oracle仅考虑每个缺少统计信息,而不是每个分区。 (这将是一个很好的功能请求,但这对你现在没有帮助。)使用SQL语句中的文字,Oracle似乎确切地知道要查看哪个分区。由于没有该分区的统计信息,它假设有没有行,导致糟糕的计划。

示例

创建一个包含1000行但没有收集统计​​信息的样本分区表。

create table partition_test
(
    a number,
    b number
)
partition by range (a)
(
    partition p1 values less than (2)
);
insert into partition_test select 1, level from dual connect by level <= 1000;

如果没有统计信息,Oracle将自动使用动态采样并获得良好的行数。你无法在这个简单的计划中看到它,但通常这会带来更好的访问方法和更好的连接操作。

explain plan for select * from partition_test where a = 1 and b <= 1000;
select * from table(dbms_xplan.display);

Plan hash value: 4097357352
     ---------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name           | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
---------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                |  1000 | 26000 |    16   (0)| 00:00:01 |       |       |
|   1 |  PARTITION RANGE SINGLE|                |  1000 | 26000 |    16   (0)| 00:00:01 |     1 |     1 |
|*  2 |   TABLE ACCESS FULL    | PARTITION_TEST |  1000 | 26000 |    16   (0)| 00:00:01 |     1 |     1 |
---------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("A"=1 AND "B"<=1000)

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)

收集统计信息,然后使用数据创建新分区。虽然该分区有1000行且没有统计信息,但Oracle并不知道这一点,只是假设它是空的。

begin
    dbms_stats.gather_table_stats(user, 'partition_test');
end;
/
alter table partition_test add partition p2 values less than (3);
insert into partition_test select 2, level from dual connect by level <= 1000;
explain plan for select * from partition_test where a = 2 and b <= 1000;
select * from table(dbms_xplan.display);


Plan hash value: 4097357352

---------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name           | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
---------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                |     1 |     7 |     9   (0)| 00:00:01 |       |       |
|   1 |  PARTITION RANGE SINGLE|                |     1 |     7 |     9   (0)| 00:00:01 |     2 |     2 |
|*  2 |   TABLE ACCESS FULL    | PARTITION_TEST |     1 |     7 |     9   (0)| 00:00:01 |     2 |     2 |
---------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("A"=2 AND "B"<=1000)

明确请求动态采样将修复基数估算,这可能会解决您的执行计划问题。

explain plan for select /*+ dynamic_sampling(4) */ * from partition_test where a = 2 and b <= 1000;
select * from table(dbms_xplan.display);

Plan hash value: 4097357352

---------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name           | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
---------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                |  1000 |  7000 |     9   (0)| 00:00:01 |       |       |
|   1 |  PARTITION RANGE SINGLE|                |  1000 |  7000 |     9   (0)| 00:00:01 |     2 |     2 |
|*  2 |   TABLE ACCESS FULL    | PARTITION_TEST |  1000 |  7000 |     9   (0)| 00:00:01 |     2 |     2 |
---------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("A"=2 AND "B"<=1000)

Note
-----
   - dynamic statistics used: dynamic sampling (level=4)