[20120626]11G的Extended Stats问题.txt

[20120626]11G的Extended Stats问题.txt

select * from v$version ;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE    11.2.0.1.0      Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production

1.建立测试:

create table t1
as
with generator as (
    select    --+ materialize
        rownum id 
    from dual 
    connect by 
        level 
)
select
    mod(rownum,100)        col1,
    mod(rownum,10)        col2
from
    generator    v1,
    generator    v2
where
    rownum 
;
begin
    dbms_stats.gather_table_stats(
        ownname         => user,
        tabname         =>'T1',
        method_opt     => 'for all columns size 1'
    );
end;
/

2.开始测试:

SQL> select * from t1 where col1 = 50 and col2 = 5 ;
no rows selected
SQL> @dpc
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  2w1wpwhgwnpuy, child number 0
-------------------------------------
select * from t1 where col1 = 50 and col2 = 5
Plan hash value: 3617692013
--------------------------------------------------------
| Id  | Operation         | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
|   0 | SELECT STATEMENT  |      |        |    25 (100)|
|*  1 |  TABLE ACCESS FULL| T1   |     50 |    25   (0)|
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("COL1"=50 AND "COL2"=5))
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

24 rows selected.

--可以发现估计是50rows,而实际是0行.

50000/100/50=50行.

很明显col1与col2存在相关性.

3.建立Extended Stats分析:

exec DBMS_STATS.GATHER_TABLE_STATS (OwnName=>null,TabName => 'T1',Estimate_Percent=> NULL,Method_Opt => 'FOR COLUMNS (col1,col2) SIZE 254 ',Cascade=> TRUE);
SQL> select * from t1 where col1 = 50 and col2 = 5 ;
no rows selected
SQL> @dpc
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  2w1wpwhgwnpuy, child number 0
-------------------------------------
select * from t1 where col1 = 50 and col2 = 5
Plan hash value: 3617692013
--------------------------------------------------------
| Id  | Operation         | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
|   0 | SELECT STATEMENT  |      |        |    25 (100)|
|*  1 |  TABLE ACCESS FULL| T1   |    250 |    25   (0)|
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("COL1"=50 AND "COL2"=5))
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

24 rows selected.

--更加糟糕!估计250行.

select count(*),col1,col2 from t1 group by col1 ,col2
返回100行.oracle对于不在范围的行,返回的是

50000/100*.5 =250行.

SQL> select * from t1 where col1 = 29 and col2 = 9 ;
SQL> @dpc
PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------
SQL_ID  7zxz2cswcz432, child number 0
-------------------------------------
select * from t1 where col1 = 29 and col2 = 9
Plan hash value: 3617692013
--------------------------------------------------------
| Id  | Operation         | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
|   0 | SELECT STATEMENT  |      |        |    25 (100)|
|*  1 |  TABLE ACCESS FULL| T1   |    500 |    25   (0)|
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("COL1"=29 AND "COL2"=9))
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

24 rows selected.

--这样才正确.

SQL> select * from t1 where col1 = 29 and col2 = 9.0001 ;
no rows selected
SQL> @dpc
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------
SQL_ID  34rumajymcx1g, child number 0
-------------------------------------
select * from t1 where col1 = 29 and col2 = 9.0001
Plan hash value: 3617692013
--------------------------------------------------------
| Id  | Operation         | Name | E-Rows | Cost (%CPU)|
--------------------------------------------------------
|   0 | SELECT STATEMENT  |      |        |    25 (100)|
|*  1 |  TABLE ACCESS FULL| T1   |     50 |    25   (0)|
--------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(("COL1"=29 AND "COL2"=9.0001))
Note
-----
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or
       * parameter 'statistics_level' is set to 'ALL', at session or system level

24 rows selected.

--看来oracle优化器存在许多改进的地方.

时间: 2024-09-21 16:46:40

[20120626]11G的Extended Stats问题.txt的相关文章

[20120803]11G SPM的学习1.txt

[20120803]11G SPM的学习1.txt     开始学习SQL Plan Management(SPM) ,11G开始提供SPM,在10g下我经常使用sql profile看一些bad sql语句,sql profile我觉得已经做的很好,有时候能够提供很好的建议.我开始学习SPM的时候感觉不习惯(也许是因为在toad下使用sql profile很简单)为什么oracle还有搞出SPM来,慢慢看资料,才明白其中一些细节.     我看过别人在从8i升级到9i的时候,出现性能波动,里面

[20170620]11G 12c expand sql text.txt

[20170620]11G 12c expand sql text.txt --//原来写的脚本只能分别在11g,12c单独使用.上午花一点点时间.把两者整合起来. --//讨论链接:http://www.itpub.net/thread-2088981-1-1.html --//再次感谢solomon_007的指点: set long 20000 set serveroutput on declare     L_sqltext clob := null;     l_version varc

[20161208]11g直方图与char数据类型.txt

[20161208]11g直方图与char数据类型.txt --以前看tom大师的书提到过不要使用char数据类型,哪怕是char(1)也不要使用,最近看了几篇blob里面都提到了11g升级后会出现char数 --据类型直方图统计发生了变化,我重复别人的例子来说明.再次强调不要生产环境使用char类型. --参考链接:http://blog.dbi-services.com/histograms-on-character-strings-between-11-2-0-3-and-11-2-0-4

[20171120]11G关闭直接路径读.txt

[20171120]11G关闭直接路径读.txt --//今天做filesystemio_options参数测试时,遇到一个关于直接路径读的问题. --//如果看以前的博客介绍,设置"_serial_direct_read"=never或者events '10949 trace name context forever';就可以关闭直接路径读. --//我的测试在11GR2下set events '10949 trace name context forever';不行. --//通过

[20170518]11G审计日志清除3.txt

[20170518]11G审计日志清除3.txt --//以前写的链接:http://blog.itpub.net/267265/viewspace-2133145/ --//我当时写存在许多问题,仅仅清除sys.aud$内容,参数audit_file_dest目录的文件不清除,而这里参数大量的aud文件,重新测试看看. 1.环境: SYS@book> @ &r/ver1 PORT_STRING                    VERSION        BANNER -------

[20150504]11G口令不对的问题.txt

[20150504]11G口令不对的问题.txt --前一阵子,遇到用户口令不对,导致登录缓慢的问题,主要11G加入密码错误验证延迟导致的.(哎现在看文档才知道) http://blog.itpub.net/267265/viewspace-1479718/ Preventing passwords from being broken. If a user tries to log in to Oracle Database multiple times using an incorrect p

[20160516]11G HugePage的使用问题.txt

[20160516]11G HugePage的使用问题.txt --我们的dg内存不足,配置的/etc/sysctl.conf如下: vm.nr_hugepages = 3200 --没有启动数据库前如下: # grep -i huge /proc/meminfo AnonHugePages:     14336 kB HugePages_Total:    3200 HugePages_Free:     3200 HugePages_Rsvd:        0 HugePages_Surp

[20150706]11G谓词推入问题.txt

[20150706]11G谓词推入问题.txt --生产系统遇到一个sql语句的问题. --生产系统的sql语句比较复杂,做一个简化的例子来说明问题.来说明自己优化遇到的困惑. 1.建立测试环境: SCOTT@test> @ver1 PORT_STRING                    VERSION        BANNER ------------------------------ -------------- ----------------------------------

[20160816]11G dataguard坏块修复.txt

[20160816]11G dataguard坏块修复.txt --11GR2 不仅仅支持在备库在只读的情况下,日志应用(ACTIVE Data Guard),还提供主备库的坏块修复.自己以前也做过相关测试, --我记得上次测试的仅仅是主库数据块损坏,没有测试备库的数据块损坏.补充一些测试: 1.环境: SYS@test> @ ver1 PORT_STRING                    VERSION        BANNER ---------------------------