- 最后登录
- 2023-8-16
- 在线时间
- 1686 小时
- 威望
- 2135
- 金钱
- 50532
- 注册时间
- 2011-10-12
- 阅读权限
- 200
- 帖子
- 5207
- 精华
- 39
- 积分
- 2135
- UID
- 2
|
4#
发表于 2011-12-1 22:09:26
因为dbms_stats 默认自动选择采样的比例,而status=''INVALID'' ' 的数据行可能正好没有被采样到。
如:
- SQL> update maclean set status='INVALID' where owner='MACLEAN';
- 9 rows updated.
- SQL> commit;
- Commit complete.
- SQL> create index ind_maclean on maclean(status);
- Index created.
- SQL> declare
- 2 begin
- 3 for i in 1..500 loop
- 4 execute immediate ' alter system flush shared_pool';
- 5 DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO;
- 6 execute immediate 'select count(*) from maclean where status=''INVALID'' ' ;
- 7 end loop;
- 8 end;
- 9 /
- PL/SQL procedure successfully completed.
- SQL> declare
- 2 begin
- 3 for i in 1..500 loop
- 4 execute immediate ' alter system flush shared_pool';
- 5 DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO;
- 6 execute immediate 'select count(*) from maclean where status=''INVALID'' ' ;
- 7 end loop;
- 8 end;
- 9 /
- PL/SQL procedure successfully completed.
- SQL> exec dbms_stats.gather_table_stats('SYS','MACLEAN');
- PL/SQL procedure successfully completed.
- SQL> WITH hist_data AS (
- 2 SELECT endpoint_value,endpoint_actual_value,
- 3 NVL(LAG (endpoint_value) OVER (ORDER BY endpoint_value),' ') prev_value,
- 4 endpoint_number,
- 5 endpoint_number,
- 6 endpoint_number
- 7 - NVL (LAG (endpoint_number) OVER (ORDER BY endpoint_value), 0)
- 8 bucket_count
- 9 FROM dba_tab_histograms
- 10 JOIN dba_tab_col_statistics USING (owner, table_name,column_name)
- 11 WHERE owner = '&owner'
- 12 AND table_name = '&table'
- 13 AND column_name = '&column'
- 14 AND histogram='FREQUENCY')
- 15 SELECT hexstr(endpoint_value) char_value,
- 16 bucket_count,
- 17 ROUND(bucket_count*100/SUM(bucket_count) OVER(),2) PCT,
- 18 RPAD(' ',ROUND(bucket_count*50/MAX(bucket_count) OVER()),'*') pct_of_max
- 19 FROM hist_data
- 20 ORDER BY endpoint_value;
- Enter value for owner: SYS
- old 11: WHERE owner = '&owner'
- new 11: WHERE owner = 'SYS'
- Enter value for table: MACLEAN
- old 12: AND table_name = '&table'
- new 12: AND table_name = 'MACLEAN'
- Enter value for column: STATUS
- old 13: AND column_name = '&column'
- new 13: AND column_name = 'STATUS'
- CHAR_VALUE
- --------------------------------------------------------------------------------
- BUCKET_COUNT PCT
- ------------ ----------
- PCT_OF_MAX
- --------------------------------------------------------------------------------
- VALIC3
- 5546 100
- *************************************************
- 以上使用AUTO_SAMPLE自动采样大小 得到我们认为不准确的直方图
- 我们手动指定100%的采样比例
- SQL> exec dbms_stats.gather_table_stats(ownname => 'SYS',tabname => 'MACLEAN',estimate_percent => 100);
- PL/SQL procedure successfully completed.
- SQL> set linesize 80;
- SQL> WITH hist_data AS (
- 2 SELECT endpoint_value,endpoint_actual_value,
- 3 NVL(LAG (endpoint_value) OVER (ORDER BY endpoint_value),' ') prev_value,
- 4 endpoint_number,
- 5 endpoint_number,
- 6 endpoint_number
- 7 - NVL (LAG (endpoint_number) OVER (ORDER BY endpoint_value), 0)
- 8 bucket_count
- 9 FROM dba_tab_histograms
- 10 JOIN dba_tab_col_statistics USING (owner, table_name,column_name)
- 11 WHERE owner = '&owner'
- 12 AND table_name = '&table'
- 13 AND column_name = '&column'
- 14 AND histogram='FREQUENCY')
- 15 SELECT hexstr(endpoint_value) char_value,
- 16 bucket_count,
- 17 ROUND(bucket_count*100/SUM(bucket_count) OVER(),2) PCT,
- 18 RPAD(' ',ROUND(bucket_count*50/MAX(bucket_count) OVER()),'*') pct_of_max
- 19 FROM hist_data
- 20 ORDER BY endpoint_value;
- Enter value for owner: SYS
- old 11: WHERE owner = '&owner'
- new 11: WHERE owner = 'SYS'
- Enter value for table: MACLEAN
- old 12: AND table_name = '&table'
- new 12: AND table_name = 'MACLEAN'
- Enter value for column: STATUS
- old 13: AND column_name = '&column'
- new 13: AND column_name = 'STATUS'
- CHAR_VALUE
- --------------------------------------------------------------------------------
- BUCKET_COUNT PCT
- ------------ ----------
- PCT_OF_MAX
- --------------------------------------------------------------------------------
- INVALI
- 10 .02
- VALIC3
- 55539 99.98
- *************************************************
复制代码
得到期望的直方图 |
|