这篇文章给大家分享的是有关oracle12CR2查询转换之表扩展的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。表扩展工作原理表分区使用表扩展成为可能。如果在一个分区表上
这篇文章给大家分享的是有关oracle12CR2查询转换之表扩展的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
表扩展工作原理
表分区使用表扩展成为可能。如果在一个分区表上创建一个本地索引,那么优化器可能会标记索引对于特定的分区不可使用。实际有些分区没有创建索引。在表扩展中,优化器将查询转换为一个uNIOn all语句,让一些子查询访问创建索引的分区,一些子查询访问没有创建索引的分区。优化器可以为每个分区选择最有效的访问路径,而不管它是否存在于查询所要访问的所有分区中。
优化器不总是会选择表扩展
.表扩展是基于成本
当数据库访问扩展表的每个分区只会跨越union all的所有分支一次,数据库所连接的任何表都是在分支中被访问。
.语义问题可能导致表扩展无效
例如,一个表出现在一个外连接的右边对于表扩展来说是无效的。
可以使用expand_table hint来控制表扩展。这个hint会覆盖基于成本的决策,但不会覆盖语义检查。
表扩展使用场景
优化器基于查询中出现的谓词条件对每个表必须被访问的分区保持跟踪。分区裁剪能让优化器使用表扩展来生成更有效的执行计划。
下面的例子假设满足以下条件:
.想要对sh.sales表执行星型查询,表sh.sales是基于time_id列进行范围分区的一个分区表。
.想要禁用特定分区上的索引来查看表扩展的优点。
操作步骤如下:
1.以sh用户登录数据库
[oracle@jytest1 ~]$ sqlplus sh
Predicate InfORMation (identified by operation id):
---------------------------------------------------
4 - access("PROD_ID"=38)
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
2 - "PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
3 - "SALES".ROWID[ROWID,10], "PROD_ID"[NUMBER,22]
4 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 7920], "PROD_ID"[NUMBER,22]
Note
-----
- automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold
58 rows selected.
在执行计划中的Pstart与Pstop列,显示了优化器判断只需要访问表的13到28分区。在优化器已经判断了被访问的分区之后,它将考虑所有这些分区上可以使用的索引。在上面的执行计划中,优化器选择使用sales_prod_bix位图索引
4.禁用sales表中sales_1995分区上的索引;
SQL> alter index sales_prod_bix modify partition sales_1995 unusable;
Index altered.
5.再次执行之前的查询语句,然后显示执行计划,可以看到执行计划变成了由两个子查询组成的union all语句,第一个子查询还是对13-28分区使用索引,第二个子查询步骤对应的Pstart与Pstop为invalid,id=11的过滤条件为”PROD_ID”=38,id=9的过滤条件为”SALES”.”TIME_ID”=TO_DATE(‘ 2000-01-01 00:00:00', ‘syyyy-mm-dd hh34:mi:ss')))这个过滤条件是为否的,所以过滤后的记录为0,从对应的A-Rows列也可以看到记录为0
SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced allstats last runstats_last peeked_binds'));
SQL_ID 214qgysqqz0k8, child number 0
-------------------------------------
select * from sales where time_id >= to_date('2000-01-01 00:00:00',
'syyyy-mm-dd hh34:mi:ss') and prod_id = 38
Plan hash value: 238952339
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | Pstart| Pstop | A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 224 (100)| | | | 4224 |00:00:00.05 | 334 |
| 1 | VIEW | VW_TE_2 | 1 | 5079 | 431K| 224 (0)| 00:00:01 | | | 4224 |00:00:00.05 | 334 |
| 2 | UNION-ALL | | 1 | | | | | | | 4224 |00:00:00.05 | 334 |
| 3 | PARTITION RANGE ITERATOR | | 1 | 5078 | 143K| 224 (0)| 00:00:01 | 13 | 28 | 4224 |00:00:00.03 | 334 |
| 4 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED | SALES | 16 | 5078 | 143K| 224 (0)| 00:00:01 | 13 | 28 | 4224 |00:00:00.02 | 334 |
| 5 | BITMAP CONVERSION TO ROWIDS | | 8 | | | | | | | 4224 |00:00:00.01 | 24 |
|* 6 | BITMAP INDEX SINGLE VALUE | SALES_PROD_BIX | 8 | | | | | 13 | 28 | 8 |00:00:00.01 | 24 |
|* 7 | FILTER | | 1 | | | | | | | 0 |00:00:00.01 | 0 |
| 8 | PARTITION RANGE EMPTY | | 0 | 1 | 29 | 1 (0)| 00:00:01 |INVALID|INVALID| 0 |00:00:00.01 | 0 |
|* 9 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED| SALES | 0 | 1 | 29 | 1 (0)| 00:00:01 |INVALID|INVALID| 0 |00:00:00.01 | 0 |
| 10 | BITMAP CONVERSION TO ROWIDS | | 0 | | | | | | | 0 |00:00:00.01 | 0 |
|* 11 | BITMAP INDEX SINGLE VALUE | SALES_PROD_BIX | 0 | | | | |INVALID|INVALID| 0 |00:00:00.01 | 0 |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SET$D0A14387 / VW_TE_2@SEL$0A5B0FFE
2 - SET$D0A14387
3 - SET$D0A14387_1
4 - SET$D0A14387_1 / SALES@SEL$1
7 - SET$D0A14387_2
9 - SET$D0A14387_2 / SALES@SEL$1
Outline Data
-------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - access("PROD_ID"=38)
7 - filter(NULL IS NOT NULL)
9 - filter(("SALES"."TIME_ID"=TO_DATE(' 2000-01-01 00:00:00', 'syyyy-mm-dd
hh34:mi:ss')))
11 - access("PROD_ID"=38)
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "ITEM_1"[NUMBER,22], "ITEM_2"[NUMBER,22], "ITEM_3"[DATE,7], "ITEM_4"[NUMBER,22], "ITEM_5"[NUMBER,22], "ITEM_6"[NUMBER,22], "ITEM_7"[NUMBER,22]
2 - STRDEF[22], STRDEF[22], STRDEF[7], STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[22]
3 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
4 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
5 - "SALES".ROWID[ROWID,10], "SALES"."PROD_ID"[NUMBER,22]
6 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 7920], "SALES"."PROD_ID"[NUMBER,22]
7 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
8 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
9 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
10 - "SALES".ROWID[ROWID,10], "SALES"."PROD_ID"[NUMBER,22]
11 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 7920], "SALES"."PROD_ID"[NUMBER,22]
Note
-----
- automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold
93 rows selected.
6.禁用分区28上的索引(sales_q4_2003),它是查询需要访问的一个分区:
SQL> alter index sales_prod_bix modify partition sales_q4_2003 unusable;
Index altered.
SQL> alter index sales_time_bix modify partition sales_q4_2003 unusable;
Index altered.
通过禁用查询需要访问分区上的索引,查询将不能再使用这些索引。
7.再次执行查询语句,其执行计划如下,执行计划变成了由三个子查询组成的union all语句,相比之前查询多的第三个子查询对表sales的第28个分区执行全表扫描,这里没有索引可用,因为已经禁用28分区上的索引了。
SQL> select * from table(dbms_xplan.display_cursor(null,null,'advanced allstats last runstats_last peeked_binds'));
SQL_ID 214qgysqqz0k8, child number 0
-------------------------------------
select * from sales where time_id >= to_date('2000-01-01 00:00:00',
'syyyy-mm-dd hh34:mi:ss') and prod_id = 38
Plan hash value: 3857158179
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time | Pstart| Pstop | A-Rows | A-Time | Buffers | Reads |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | | 225 (100)| | | | 4224 |00:00:00.20 | 334 | 44 |
| 1 | VIEW | VW_TE_2 | 1 | 5080 | 431K| 225 (0)| 00:00:01 | | | 4224 |00:00:00.20 | 334 | 44 |
| 2 | UNION-ALL | | 1 | | | | | | | 4224 |00:00:00.19 | 334 | 44 |
| 3 | PARTITION RANGE ITERATOR | | 1 | 5078 | 143K| 223 (0)| 00:00:01 | 13 | 27 | 4224 |00:00:00.17 | 334 | 44 |
| 4 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED | SALES | 15 | 5078 | 143K| 223 (0)| 00:00:01 | 13 | 27 | 4224 |00:00:00.16 | 334 | 44 |
| 5 | BITMAP CONVERSION TO ROWIDS | | 8 | | | | | | | 4224 |00:00:00.03 | 24 | 16 |
|* 6 | BITMAP INDEX SINGLE VALUE | SALES_PROD_BIX | 8 | | | | | 13 | 27 | 8 |00:00:00.03 | 24 | 16 |
|* 7 | FILTER | | 1 | | | | | | | 0 |00:00:00.01 | 0 | 0 |
| 8 | PARTITION RANGE EMPTY | | 0 | 1 | 29 | 1 (0)| 00:00:01 |INVALID|INVALID| 0 |00:00:00.01 | 0 | 0 |
|* 9 | TABLE ACCESS BY LOCAL INDEX ROWID BATCHED| SALES | 0 | 1 | 29 | 1 (0)| 00:00:01 |INVALID|INVALID| 0 |00:00:00.01 | 0 | 0 |
| 10 | BITMAP CONVERSION TO ROWIDS | | 0 | | | | | | | 0 |00:00:00.01 | 0 | 0 |
|* 11 | BITMAP INDEX SINGLE VALUE | SALES_PROD_BIX | 0 | | | | |INVALID|INVALID| 0 |00:00:00.01 | 0 | 0 |
| 12 | PARTITION RANGE SINGLE | | 1 | 1 | 87 | 2 (0)| 00:00:01 | 28 | 28 | 0 |00:00:00.01 | 0 | 0 |
|* 13 | TABLE ACCESS FULL | SALES | 1 | 1 | 87 | 2 (0)| 00:00:01 | 28 | 28 | 0 |00:00:00.01 | 0 | 0 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
1 - SET$D0A14387 / VW_TE_2@SEL$0A5B0FFE
2 - SET$D0A14387
3 - SET$D0A14387_1
4 - SET$D0A14387_1 / SALES@SEL$1
7 - SET$D0A14387_2
9 - SET$D0A14387_2 / SALES@SEL$1
12 - SET$D0A14387_3
13 - SET$D0A14387_3 / SALES@SEL$1
Outline Data
-------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - access("PROD_ID"=38)
7 - filter(NULL IS NOT NULL)
9 - filter(("SALES"."TIME_ID"=TO_DATE(' 2000-01-01 00:00:00', 'syyyy-mm-dd hh34:mi:ss')))
11 - access("PROD_ID"=38)
13 - filter("PROD_ID"=38)
Column Projection Information (identified by operation id):
-----------------------------------------------------------
1 - "ITEM_1"[NUMBER,22], "ITEM_2"[NUMBER,22], "ITEM_3"[DATE,7], "ITEM_4"[NUMBER,22], "ITEM_5"[NUMBER,22], "ITEM_6"[NUMBER,22], "ITEM_7"[NUMBER,22]
2 - STRDEF[22], STRDEF[22], STRDEF[7], STRDEF[22], STRDEF[22], STRDEF[22], STRDEF[22]
3 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
4 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
5 - "SALES".ROWID[ROWID,10], "SALES"."PROD_ID"[NUMBER,22]
6 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 7920], "SALES"."PROD_ID"[NUMBER,22]
7 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
8 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
9 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
10 - "SALES".ROWID[ROWID,10], "SALES"."PROD_ID"[NUMBER,22]
11 - STRDEF[BM VAR, 10], STRDEF[BM VAR, 10], STRDEF[BM VAR, 7920], "SALES"."PROD_ID"[NUMBER,22]
12 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
13 - "SALES"."PROD_ID"[NUMBER,22], "SALES"."CUST_ID"[NUMBER,22], "SALES"."TIME_ID"[DATE,7], "SALES"."CHANNEL_ID"[NUMBER,22], "SALES"."PROMO_ID"[NUMBER,22],
"SALES"."QUANTITY_SOLD"[NUMBER,22], "SALES"."AMOUNT_SOLD"[NUMBER,22]
Note
-----
- automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold
103 rows selected.
感谢各位的阅读!关于“Oracle12CR2查询转换之表扩展的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
--结束END--
本文标题: Oracle12CR2查询转换之表扩展的示例分析
本文链接: https://lsjlt.com/news/57527.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0