分析如何影响CBO决策一例

本文通过具体实例展示了如何利用SQL查询、表和索引分析、动态采样技术来优化查询效率,包括创建表、索引,插入数据,使用dynamic_sampling hint进行查询并分析执行计划,以及通过dbms_stats.gather_table_stats进行表和索引的分析,最终提高查询准确性和性能。

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

SQL> conn evan/evan Connected. SQL> select * from v$version where rownum<2; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production SQL> create table t as select object_id,object_name from dba_objects where 1=2; Table created. SQL> create index ind_t on t(object_id); Index created. SQL> insert into t select object_id,object_name from dba_objects; 72486 rows created. SQL> commit; Commit complete. SQL> select num_rows,avg_row_len,blocks,last_analyzed from user_tables where table_name='T'; NUM_ROWS AVG_ROW_LEN BLOCKS LAST_ANALYZE ---------- ----------- ---------- ------------ SQL> select blevel,leaf_blocks,distinct_keys,last_analyzed from user_indexes where table_name='T'; BLEVEL LEAF_BLOCKS DISTINCT_KEYS LAST_ANALYZE ---------- ----------- ------------- ------------ 0 0 0 15-APR-11以上可知,t表和索引没有被分析。

使用dynamic_sampling hint来禁止动态采样,可以看出cbo采用了index range scan,生成了错误的执行计划,cbo估算满足条件的记录为4(Rows)。

SQL> set autotrace traceonly SQL> select /*+ dynamic_sampling(t 0) */ * from t where object_id>30; 72457 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 4013845416 ------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 4 | 316 | 0 (0)| 00:00:01 | | 1 | TABLE ACCESS BY INDEX ROWID| T | 4 | 316 | 0 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | IND_T | 1 | | 0 (0)| 00:00:01 | ------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - access("OBJECT_ID">30) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 10254 consistent gets 0 physical reads 0 redo size 3383807 bytes sent via SQL*Net to client 53650 bytes received via SQL*Net from client 4832 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 72457 rows processed使用dbms_stats.gather_table_stats对表和索引做分析.

可以看出cbo估算出的记录数为72459(Rows),和实际记录数很相近,从而采用table access full.

SQL> set autotrace off SQL> exec dbms_stats.gather_table_stats(user,'T'); PL/SQL procedure successfully completed. SQL> select num_rows,avg_row_len,blocks,last_analyzed from user_tables where table_name='T'; NUM_ROWS AVG_ROW_LEN BLOCKS LAST_ANALYZE ---------- ----------- ---------- ------------ 72486 29 370 15-APR-11 SQL> select blevel,leaf_blocks,distinct_keys,last_analyzed from user_indexes where table_name='T'; BLEVEL LEAF_BLOCKS DISTINCT_KEYS LAST_ANALYZE ---------- ----------- ------------- ------------ 1 261 72486 15-APR-11 SQL> set autotrace traceonly SQL> select /*+ dynamic_sampling(t 0) */ * from t where object_id>30; 72457 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 72459 | 2052K| 103 (1)| 00:00:02 | |* 1 | TABLE ACCESS FULL| T | 72459 | 2052K| 103 (1)| 00:00:02 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_ID">30) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 5179 consistent gets 0 physical reads 0 redo size 3094035 bytes sent via SQL*Net to client 53650 bytes received via SQL*Net from client 4832 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 72457 rows processed SQL> select * from t where object_id>30; 72457 rows selected. Execution Plan ---------------------------------------------------------- Plan hash value: 1601196873 -------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 72459 | 2052K| 103 (1)| 00:00:02 | |* 1 | TABLE ACCESS FULL| T | 72459 | 2052K| 103 (1)| 00:00:02 | -------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 1 - filter("OBJECT_ID">30) Statistics ---------------------------------------------------------- 1 recursive calls 0 db block gets 5179 consistent gets 0 physical reads 0 redo size 3094035 bytes sent via SQL*Net to client 53650 bytes received via SQL*Net from client 4832 SQL*Net roundtrips to/from client 0 sorts (memory) 0 sorts (disk) 72457 rows processed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值