分区修剪(Partition Pruning)

来源:互联网 发布:日系淘宝店铺推荐 编辑:程序博客网 时间:2024/05/01 20:18

Partition Pruning

Partition pruning is an essential performance feature for data warehouses. In partition pruning, the optimizer analyzes FROM and WHERE clauses in SQL statements to eliminate unneeded partitions when building the partition access list. This functionality enables Oracle Database to perform operations only on those partitions that are relevant to the SQL statement.

分区修剪是数据仓库的一个重要特性。当访问分区表的时候,优化器分析FROM和WHERE子句来消除不必要的分区。

This section contains the following topics:

  • Benefits of Partition Pruning

  • Information That Can Be Used for Partition Pruning

  • How to Identify Whether Partition Pruning Has Been Used

  • Static Partition Pruning

  • Dynamic Partition Pruning

Benefits of Partition Pruning

Partition pruning dramatically reduces the amount of data retrieved from disk and shortens processing time, thus improving query performance and optimizing resource utilization. If you partition the index and table on different columns (with a global partitioned index), then partition pruning also eliminates index partitions even when the partitions of the underlying table cannot be eliminated.

分区修剪提高了查询性能和优化了资源的使用。如果你的分区表和分区索引在不同的列,分区修剪也能消除分区索引即使分区表不能被消除。

Depending upon the actual SQL statement, Oracle Database may use static or dynamic pruning. Static pruning occurs at compile-time, with the information about the partitions accessed beforehand. Dynamic pruning occurs at run-time, meaning that the exact partitions to be accessed by a statement are not known beforehand. A sample scenario for static pruning is a SQL statement containing a WHERE condition with a constant literal on the partition key column. An example of dynamic pruning is the use of operators or functions in the WHERE condition.

根据实际的SQL语句,Oracle使用静态修剪或者动态修剪。静态修剪发生在编译的时候且分区已经知晓,动态修剪发生在运行的时候意味着实际分区在被语句访问前并不知道。一个简单的静态修剪的例子就是包含常数的where子句,一个动态修建的例子就是where子句中使用函数和操作符。

Partition pruning affects the statistics of the objects where pruning occurs and also affects the execution plan of a statement.

分区修剪影响着统计信息和执行计划。

Information That Can Be Used for Partition Pruning

Oracle Database prunes partitions when you use range, LIKE, equality, and IN-list predicates on the range or list partitioning columns, and when you use equality and IN-list predicates on the hash partitioning columns.

Oracle当你在区间分区或列表分区列上使用between,like,‘=‘,in这些谓词的时候使用分区修剪,当你在hash分区列上使用'=',in时使用分区修剪。

On composite partitioned objects, Oracle Database can prune at both levels using the relevant predicates. Examine the table sales_range_hash, which is partitioned by range on the column s_saledate and subpartitioned by hash on the column s_productid in Example 3-1.

Example 3-1 Creating a table with partition pruning

CREATE TABLE sales_range_hash(  s_productid  NUMBER,  s_saledate   DATE,  s_custid     NUMBER,  s_totalprice NUMBER)PARTITION BY RANGE (s_saledate)  --使用了range partitioningSUBPARTITION BY HASH (s_productid) SUBPARTITIONS 8 (PARTITION sal99q1 VALUES LESS THAN   (TO_DATE('01-APR-1999', 'DD-MON-YYYY')),  PARTITION sal99q2 VALUES LESS THAN   (TO_DATE('01-JUL-1999', 'DD-MON-YYYY')),  PARTITION sal99q3 VALUES LESS THAN   (TO_DATE('01-OCT-1999', 'DD-MON-YYYY')),  PARTITION sal99q4 VALUES LESS THAN   (TO_DATE('01-JAN-2000', 'DD-MON-YYYY')));SELECT * FROM sales_range_hashWHERE s_saledate BETWEEN (TO_DATE('01-JUL-1999', 'DD-MON-YYYY'))  AND (TO_DATE('01-OCT-1999', 'DD-MON-YYYY')) AND s_productid = 1200;

Oracle uses the predicate on the partitioning columns to perform partition pruning as follows:

  • When using range partitioning, Oracle accesses only partitions sal99q2 and sal99q3, representing the partitions for the third and fourth quarters of 1999. 当使用了区间分区,Oracle只访问sal99q2和sal99q3两个分区。

  • When using hash subpartitioning, Oracle accesses only the one subpartition in each partition that stores the rows with s_productid=1200. The mapping between the subpartition and the predicate is calculated based on Oracle's internal hash distribution function.当使用了hash分区,Oracle只访问存储了行s_productid=200的分区,子分区和谓词的映射是基于Oracle内部hash分布函数计算的。

A reference-partitioned table can take advantage of partition pruning through the join with the referenced table. Virtual column-based partitioned tables benefit from partition pruning for statements that use the virtual column-defining expression in the SQL statement.引用分区在连接了父子表时会采用分区修剪。虚拟列分区会在使用了虚拟列表达式时采用分区修剪。

How to Identify Whether Partition Pruning Has Been Used

Whether Oracle uses partition pruning is reflected in the execution plan of a statement, either in the plan table for the EXPLAIN PLAN statement or in the shared SQL area.

Oracle是否使用了分区修剪提现在了执行计划和查看explan plan。

The partition pruning information is reflected in the plan columns PSTART (PARTITION_START) and PSTOP (PARTITION_STOP). For serial statements, the pruning information is also reflected in the OPERATION and OPTIONS columns.

分区修剪信息反应在执行计划的PSTART和PSTOP上。对于串行统计信息,表现在OPERATION和OPTION列上。

Static Partition Pruning

For many cases, Oracle determines the partitions to be accessed at compile time. Static partition pruning occurs if you use static predicates, except for the following cases: 静态修剪发生在你使用静态谓词的时候,除了以下几种情况:

  • Partition pruning occurs using the result of a subquery. 子查询的结果

  • The optimizer rewrites the query with a star transformation and pruning occurs after the star transformation. 星星查询

  • The most efficient execution plan is a nested loop. --nested loop

These three cases result in the use of dynamic pruning. 这三种情况会使用动态查询

If at parse time Oracle can identify which contiguous set of partitions is accessed, then the PSTART and PSTOP columns in the execution plan show the begin and the end values of the partitions being accessed. Any other cases of partition pruning, including dynamic pruning, show the KEY value in PSTART and PSTOP, optionally with an additional attribute.

如果Oracle在解析时能够辨别连续的分区能被访问,那么执行计划中会通过PSTART和PSTOP来显示开始和结束的分区值。除此之外包括动态修剪的其他分区修剪,PSTART和PSTOP值会显示为KEY。

The following is an example:

SQL> explain plan for select * from sales where time_id = to_date('01-jan-2001', 'dd-mon-yyyy');Explained.SQL> select * from table(dbms_xplan.display);PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------Plan hash value: 3971874201----------------------------------------------------------------------------------------------| Id | Operation              | Name  | Rows | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |----------------------------------------------------------------------------------------------|  0 | SELECT STATEMENT       |       | 673  | 19517 | 27      (8)| 00:00:01 |       |       ||  1 |  PARTITION RANGE SINGLE|       | 673  | 19517 | 27      (8)| 00:00:01 | 17    | 17    ||* 2 |   TABLE ACCESS FULL    | SALES | 673  | 19517 | 27      (8)| 00:00:01 | 17    | 17    |----------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------    2 - filter("TIME_ID"=TO_DATE('2001-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss'))

This plan shows that Oracle accesses partition number 17, as shown in the PSTART and PSTOP columns. The OPERATION column shows PARTITION RANGE SINGLE, indicating that only a single partition is being accessed. If OPERATION shows PARTITION RANGE ALL, then all partitions are being accessed and effectively no pruning takes place. PSTART then shows the very first partition of the table and PSTOP shows the very last partition.

如果执行计划中出现了PARTITION RANGE ALL则表示没有发生分区修剪。

An execution plan with a full table scan on an interval-partitioned table shows 1 for PSTART, and 1048575 for PSTOP, regardless of how many interval partitions were created.

一个通过全表扫描的间隔分区获得的执行计划PSTART和PSTOP会分别显示为1和1048575,不论创建了多少分区。

Dynamic Partition Pruning

Dynamic pruning occurs if pruning is possible and static pruning is not possible. The following examples show multiple dynamic pruning cases:

  • Dynamic Pruning with Bind Variables

  • Dynamic Pruning with Subqueries

  • Dynamic Pruning with Star Transformation

  • Dynamic Pruning with Nested Loop Joins

Dynamic Pruning with Bind Variables

Statements that use bind variables against partition columns result in dynamic pruning. For example:

SQL> explain plan for select * from sales s where time_id in ( :a, :b, :c, :d);Explained.SQL> select * from table(dbms_xplan.display);PLAN_TABLE_OUTPUT---------------------------------------------------------------------------------------------------Plan hash value: 513834092---------------------------------------------------------------------------------------------------| Id | Operation                         |    Name |Rows|Bytes|Cost (%CPU)|  Time  | Pstart| Pstop|---------------------------------------------------------------------------------------------------|  0 | SELECT STATEMENT                  |         |2517|72993|    292 (0)|00:00:04|       |      ||  1 |  INLIST ITERATOR                  |         |    |     |           |        |       |      ||  2 |   PARTITION RANGE ITERATOR        |         |2517|72993|    292 (0)|00:00:04|KEY(I) |KEY(I)||  3 |    TABLE ACCESS BY LOCAL INDEX ROWID| SALES |2517|72993|    292 (0)|00:00:04|KEY(I) |KEY(I)||  4 |     BITMAP CONVERSION TO ROWIDS   |         |    |     |           |        |       |      ||* 5 |      BITMAP INDEX SINGLE VALUE    |SALES_TIME_BIX| |   |           |        |KEY(I) |KEY(I)|---------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------5 - access("TIME_ID"=:A OR "TIME_ID"=:B OR "TIME_ID"=:C OR "TIME_ID"=:D)

For parallel execution plans, only the partition start and stop columns contain the partition pruning information; the operation column contains information for the parallel operation, as shown in the following example:

SQL> explain plan for select * from sales where time_id in (:a, :b, :c, :d);Explained.SQL> select * from table(dbms_xplan.display);PLAN_TABLE_OUTPUT-------------------------------------------------------------------------------------------------Plan hash value: 4058105390-------------------------------------------------------------------------------------------------| Id| Operation          | Name  |Rows|Bytes|Cost(%CP|  Time  |Pstart| Pstop|  TQ |INOUT| PQ Dis|-------------------------------------------------------------------------------------------------|  0| SELECT STATEMENT   |       |2517|72993|  75(36)|00:00:01|      |      |     |     |       ||  1|  PX COORDINATOR    |       |    |     |        |        |      |      |     |     |       ||  2|  PX SEND QC(RANDOM)|:TQ10000|2517|72993| 75(36)|00:00:01|      |      |Q1,00| P->S|QC(RAND||  3|   PX BLOCK ITERATOR|       |2517|72993|  75(36)|00:00:01|KEY(I)|KEY(I)|Q1,00| PCWC|       ||* 4|   TABLE ACCESS FULL| SALES |2517|72993|  75(36)|00:00:01|KEY(I)|KEY(I)|Q1,00| PCWP|       |-------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   4 - filter("TIME_ID"=:A OR "TIME_ID"=:B OR "TIME_ID"=:C OR "TIME_ID"=:D)

Dynamic Pruning with Subqueries

Statements that explicitly use subqueries against partition columns result in dynamic pruning. For example:

SQL> explain plan for select sum(amount_sold) from sales where time_id in     (select time_id from times where fiscal_year = 2000);Explained.SQL> select * from table(dbms_xplan.display);PLAN_TABLE_OUTPUTPLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------Plan hash value: 3827742054----------------------------------------------------------------------------------------------------| Id  | Operation                  | Name  | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |----------------------------------------------------------------------------------------------------|   0 | SELECT STATEMENT           |       |     1 |    25 |   523   (5)| 00:00:07 |       |       ||   1 |  SORT AGGREGATE            |       |     1 |    25 |            |          |       |       ||*  2 |   HASH JOIN                |       |   191K|  4676K|   523   (5)| 00:00:07 |       |       ||*  3 |    TABLE ACCESS FULL       | TIMES |   304 |  3648 |    18   (0)| 00:00:01 |       |       ||   4 |    PARTITION RANGE SUBQUERY|       |   918K|    11M|   498   (4)| 00:00:06 |KEY(SQ)|KEY(SQ)||   5 |     TABLE ACCESS FULL      | SALES |   918K|    11M|   498   (4)| 00:00:06 |KEY(SQ)|KEY(SQ)|----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   2 - access("TIME_ID"="TIME_ID")   3 - filter("FISCAL_YEAR"=2000)

Dynamic Pruning with Star Transformation

Statements that get transformed by the database using the star transformation result in dynamic pruning. For example:

SQL> explain plan for select p.prod_name, t.time_id, sum(s.amount_sold)     from sales s, times t, products p     where s.time_id = t.time_id and s.prod_id = p.prod_id and t.fiscal_year = 2000     and t.fiscal_week_number = 3 and p.prod_category = 'Hardware'     group by t.time_id, p.prod_name;Explained.SQL> select * from table(dbms_xplan.display);PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------Plan hash value: 4020965003------------------------------------------------------------------------------------------------------| Id  | Operation                             | Name                 | Rows  | Bytes | Pstart| Pstop |------------------------------------------------------------------------------------------------------|   0 | SELECT STATEMENT                      |                      |     1 |    79 |       |       ||   1 |  HASH GROUP BY                        |                      |     1 |    79 |       |       ||*  2 |   HASH JOIN                           |                      |     1 |    79 |       |       ||*  3 |    HASH JOIN                          |                      |     2 |    64 |       |       ||*  4 |     TABLE ACCESS FULL                 | TIMES                |     6 |    90 |       |       ||   5 |     PARTITION RANGE SUBQUERY          |                      |   587 |  9979 |KEY(SQ)|KEY(SQ)||   6 |      TABLE ACCESS BY LOCAL INDEX ROWID| SALES                |   587 |  9979 |KEY(SQ)|KEY(SQ)||   7 |       BITMAP CONVERSION TO ROWIDS     |                      |       |       |       |       ||   8 |        BITMAP AND                     |                      |       |       |       |       ||   9 |         BITMAP MERGE                  |                      |       |       |       |       ||  10 |          BITMAP KEY ITERATION         |                      |       |       |       |       ||  11 |           BUFFER SORT                 |                      |       |       |       |       ||* 12 |            TABLE ACCESS FULL          | TIMES                |     6 |    90 |       |       ||* 13 |           BITMAP INDEX RANGE SCAN     | SALES_TIME_BIX       |       |       |KEY(SQ)|KEY(SQ)||  14 |         BITMAP MERGE                  |                      |       |       |       |       ||  15 |          BITMAP KEY ITERATION         |                      |       |       |       |       ||  16 |           BUFFER SORT                 |                      |       |       |       |       ||  17 |            TABLE ACCESS BY INDEX ROWID| PRODUCTS             |    14 |   658 |       |       ||* 18 |             INDEX RANGE SCAN          | PRODUCTS_PROD_CAT_IX |    14 |       |       |       ||* 19 |           BITMAP INDEX RANGE SCAN     | SALES_PROD_BIX       |       |       |KEY(SQ)|KEY(SQ)||  20 |    TABLE ACCESS BY INDEX ROWID        | PRODUCTS             |    14 |   658 |       |       ||* 21 |     INDEX RANGE SCAN                  | PRODUCTS_PROD_CAT_IX |    14 |       |       |       |------------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   2 - access("S"."PROD_ID"="P"."PROD_ID")   3 - access("S"."TIME_ID"="T"."TIME_ID")   4 - filter("T"."FISCAL_WEEK_NUMBER"=3 AND "T"."FISCAL_YEAR"=2000)  12 - filter("T"."FISCAL_WEEK_NUMBER"=3 AND "T"."FISCAL_YEAR"=2000)  13 - access("S"."TIME_ID"="T"."TIME_ID")  18 - access("P"."PROD_CATEGORY"='Hardware')  19 - access("S"."PROD_ID"="P"."PROD_ID")  21 - access("P"."PROD_CATEGORY"='Hardware')Note-----   - star transformation used for this statement

Dynamic Pruning with Nested Loop Joins

Statements that are most efficiently executed using a nested loop join use dynamic pruning. For example:

SQL> explain plan for select t.time_id, sum(s.amount_sold)     from sales s, times t     where s.time_id = t.time_id and t.fiscal_year = 2000 and t.fiscal_week_number = 3     group by t.time_id;Explained.SQL> select * from table(dbms_xplan.display);PLAN_TABLE_OUTPUT----------------------------------------------------------------------------------------------------Plan hash value: 50737729----------------------------------------------------------------------------------------------------| Id  | Operation                  | Name  | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |----------------------------------------------------------------------------------------------------|   0 | SELECT STATEMENT           |       |     6 |   168 |   126   (4)| 00:00:02 |       |       ||   1 |  HASH GROUP BY             |       |     6 |   168 |   126   (4)| 00:00:02 |       |       ||   2 |   NESTED LOOPS             |       |  3683 |   100K|   125   (4)| 00:00:02 |       |       ||*  3 |    TABLE ACCESS FULL       | TIMES |     6 |    90 |    18   (0)| 00:00:01 |       |       ||   4 |    PARTITION RANGE ITERATOR|       |   629 |  8177 |    18   (6)| 00:00:01 |   KEY |   KEY ||*  5 |     TABLE ACCESS FULL      | SALES |   629 |  8177 |    18   (6)| 00:00:01 |   KEY |   KEY |----------------------------------------------------------------------------------------------------Predicate Information (identified by operation id):---------------------------------------------------   3 - filter("T"."FISCAL_WEEK_NUMBER"=3 AND "T"."FISCAL_YEAR"=2000)   5 - filter("S"."TIME_ID"="T"."TIME_ID")
0 0
原创粉丝点击