3:使用dbms_stats包对表进行收集统计信息,在本例中可以看出进行分析过的表,将不会继续使用动态采样的方式收集信息

- SQL> exec dbms_stats.gather_table_stats('HR','T',CASCADE=>TRUE);
- PL/SQL procedure successfully completed.
- SQL> select * from t where id=99;
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 4153437776
- --------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
- --------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 14 | 378 | 2 (0)| 00:00:01 |
- | 1 | TABLE ACCESS BY INDEX ROWID| T | 14 | 378 | 2 (0)| 00:00:01 |
- |* 2 | INDEX RANGE SCAN | I_T_ID | 14 | | 1 (0)| 00:00:01 |
- --------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 2 - access("ID"=99)
- Statistics
- ----------------------------------------------------------
- 0 recursive calls
- 0 db block gets
- 3 consistent gets
- 0 physical reads
- 0 redo size
- 491 bytes sent via SQL*Net to client
- 420 bytes received via SQL*Net from client
- 2 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 1 rows processed
4:对表分析后的表进行update操作,在未重新进行分析前,将产生错误的执行计划,重新分析后正常;因而在生产环境中,经常有DML操作的表应当根据实际情况进行分析,否则将产生类似的问题
- SQL> set autot off
- SQL> update t set id=99 where id=1;
- 77261 rows updated.
- SQL> update t set id=1 where rownum=1;
- 1 row updated.
- SQL> commit;
- Commit complete.
- SQL> select id,count(*) from t group by id;
- ID COUNT(*)
- ---------- ----------
- 1 1
- 99 77261
- SQL> set autot traceonly
- SQL> select * from t where id=99;
- 77261 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 4153437776
- --------------------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
- --------------------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 14 | 378 | 2 (0)| 00:00:01 |
- | 1 | TABLE ACCESS BY INDEX ROWID| T | 14 | 378 | 2 (0)| 00:00:01 |
- |* 2 | INDEX RANGE SCAN | I_T_ID | 14 | | 1 (0)| 00:00:01 |
- --------------------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 2 - access("ID"=99)
- Statistics
- ----------------------------------------------------------
- 0 recursive calls
- 0 db block gets
- 11017 consistent gets
- 0 physical reads
- 13840 redo size
- 3170997 bytes sent via SQL*Net to client
- 57070 bytes received via SQL*Net from client
- 5152 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 77261 rows processed
- SQL> select /*+ full(t) */ * from t where id=99;
- 77261 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 14 | 378 | 101 (1)| 00:00:02 |
- |* 1 | TABLE ACCESS FULL| T | 14 | 378 | 101 (1)| 00:00:02 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=99)
- Statistics
- ----------------------------------------------------------
- 1 recursive calls
- 0 db block gets
- 5477 consistent gets
- 0 physical reads
- 0 redo size
- 2357600 bytes sent via SQL*Net to client
- 57070 bytes received via SQL*Net from client
- 5152 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 77261 rows processed
- SQL> exec dbms_stats.gather_table_stats('HEROSTART_CN','T',CASCADE=>TRUE);
- PL/SQL procedure successfully completed.
- SQL> select * from t where id=99;
- 77261 rows selected.
- Execution Plan
- ----------------------------------------------------------
- Plan hash value: 1601196873
- --------------------------------------------------------------------------
- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
- --------------------------------------------------------------------------
- | 0 | SELECT STATEMENT | | 77241 | 2036K| 101 (1)| 00:00:02 |
- |* 1 | TABLE ACCESS FULL| T | 77241 | 2036K| 101 (1)| 00:00:02 |
- --------------------------------------------------------------------------
- Predicate Information (identified by operation id):
- ---------------------------------------------------
- 1 - filter("ID"=99)
- Statistics
- ----------------------------------------------------------
- 0 recursive calls
- 0 db block gets
- 10845 consistent gets
- 0 physical reads
- 0 redo size
- 3170997 bytes sent via SQL*Net to client
- 57070 bytes received via SQL*Net from client
- 5152 SQL*Net roundtrips to/from client
- 0 sorts (memory)
- 0 sorts (disk)
- 77261 rows processed
