基础知识之11g新特性用extended statistics 解决列相关性

本文探讨了SQL查询性能优化的关键步骤,包括使用动态采样、表统计信息更新及多谓词查询优化,通过实例展示了如何提升查询效率并解决统计信息准确性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQL> set wrap off
--创建测试表,reap列有比较高的重复率
SQL> create table t as select object_id,floor(object_id/10000) reap from dba_obj
ects;

表已创建。

SQL> set autotrace traceonly explain
--2级动态采样
SQL> select * from t where object_id<10000;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8494 |   215K|    41   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |  8494 |   215K|    41   (3)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"<10000)

Note
-----
   - dynamic sampling used for this statement (level=2)

SQL> show parameter dynamic
SQL> show parameter dyna
--未动态采样
SQL> select /*+dynamic_sampling(t 0)*/* from t where object_id<10000;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   576 | 14976 |    40   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |   576 | 14976 |    40   (0)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"<10000)
--表级分析
SQL> exec dbms_stats.gather_table_stats('YWBZ','T');

PL/SQL 过程已成功完成。

SQL> select * from t where object_id<10000;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  8771 | 70168 |    41   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |  8771 | 70168 |    41   (3)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"<10000)
--多谓词导致执行计划信息不准确
SQL> select * from t where object_id<10000 and reap=1;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |   975 |  7800 |    41   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |   975 |  7800 |    41   (3)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("REAP"=1 AND "OBJECT_ID"<10000)

SQL> set autotrace off
--实际记录数0
SQL> select count(*) from t where object_id<10000 and reap=1;

  COUNT(*)
----------
         0
-- extended statistics
SQL> exec dbms_stats.gather_table_stats('YWBZ','T',METHOD_OPT=>'FOR ALL COLUMNS
SIZE SKEWONLY FOR COLUMNS(OBJECT_ID,REAP) SIZE SKEWONLY');

PL/SQL 过程已成功完成。
--验证列相关性问题解决,统计信息较准确
SQL> select * from t where object_id<10000 and reap=1;

执行计划
----------------------------------------------------------
Plan hash value: 1601196873

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |  195 |  960 |    21   (3)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| T    |  195 |  960 |    21   (3)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("OBJECT_ID"<10000 AND "REAP"=1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值