Oracle数据库数据恢复、性能优化

找回密码
注册
搜索
热搜: 活动 交友 discuz
发新帖

131

积分

1

好友

13

主题
1#
发表于 2011-11-28 22:36:27 | 查看: 5868| 回复: 4
看了群主一片文章“CBO为什么不走索引?”
我按照群主的方式走下来还是获得不到群主的结果,我的步骤如下:
SQL>  create table maclean as select * from dba_objects;

Table created.

SQL>  update maclean set status='INVALID' where owner='TEST';

3 rows updated.

SQL> COMMIT;

Commit complete.

SQL> create index ind_maclean on maclean(status);

Index created.

SQL> exec dbms_stats.gather_table_stats('SYS','MACLEAN');

PL/SQL procedure successfully completed.

SQL> select obj# from obj$ t where t.name='MACLEAN';

      OBJ#
----------
     74890

SQL> select count(*) from col_usage$ t where t.obj#=74890;

  COUNT(*)
----------
         1

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'

no rows selected
SQL> declare
  2      begin
  3      for i in 1..500 loop
execute immediate ' alter system flush shared_pool';
DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO;
    execute immediate 'select count(*)  from maclean where status=''INVALID'' ' ;
    end loop;
  8      end;
  9  /

PL/SQL procedure successfully completed.

SQL> select count(*) from col_usage$ t where t.obj#=74890;

  COUNT(*)
----------
         2

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                5570                         100     *************************************************
SQL>
为什么还是没有收集到柱状图的信息那?
2#
发表于 2011-11-30 16:51:02
CHAR_VALUE BUCKET_COUNT        PCT  PCT_OF_MAX
--------------------------------------------------------------------------------
VALIC3                5570                         100     *************************************************


这就是柱状图信息啊 , 只是并不准确

回复 只看该作者 道具 举报

3#
发表于 2011-11-30 17:08:25
要进行什么操作才能收集到正确的直方图信息那?

回复 只看该作者 道具 举报

4#
发表于 2011-12-1 22:09:26
因为dbms_stats 默认自动选择采样的比例,而status=''INVALID'' ' 的数据行可能正好没有被采样到。

如:
  1. SQL> update maclean set status='INVALID' where owner='MACLEAN';

  2. 9 rows updated.

  3. SQL> commit;

  4. Commit complete.

  5. SQL> create index ind_maclean on maclean(status);

  6. Index created.

  7. SQL>  declare
  8.   2      begin
  9.   3      for i in 1..500 loop
  10.   4     execute immediate ' alter system flush shared_pool';
  11.   5     DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO;
  12.   6      execute immediate 'select count(*)  from maclean where status=''INVALID'' ' ;
  13.   7      end loop;
  14.   8      end;
  15.   9      /

  16. PL/SQL procedure successfully completed.


  17. SQL>  declare
  18.   2      begin
  19.   3      for i in 1..500 loop
  20.   4     execute immediate ' alter system flush shared_pool';
  21.   5     DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO;
  22.   6      execute immediate 'select count(*)  from maclean where status=''INVALID'' ' ;
  23.   7      end loop;
  24.   8      end;
  25.   9      /

  26. PL/SQL procedure successfully completed.

  27. SQL> exec dbms_stats.gather_table_stats('SYS','MACLEAN');

  28. PL/SQL procedure successfully completed.

  29. SQL> WITH hist_data AS (
  30.   2  SELECT endpoint_value,endpoint_actual_value,
  31.   3         NVL(LAG (endpoint_value) OVER (ORDER BY endpoint_value),' ') prev_value,
  32.   4         endpoint_number,
  33.   5         endpoint_number,
  34.   6         endpoint_number
  35.   7         - NVL (LAG (endpoint_number) OVER (ORDER BY endpoint_value), 0)
  36.   8             bucket_count
  37.   9  FROM dba_tab_histograms
  38. 10  JOIN dba_tab_col_statistics USING (owner, table_name,column_name)
  39. 11  WHERE     owner = '&owner'
  40. 12        AND table_name = '&table'
  41. 13        AND column_name = '&column'
  42. 14        AND histogram='FREQUENCY')
  43. 15  SELECT hexstr(endpoint_value) char_value,
  44. 16         bucket_count,
  45. 17         ROUND(bucket_count*100/SUM(bucket_count) OVER(),2) PCT,
  46. 18         RPAD(' ',ROUND(bucket_count*50/MAX(bucket_count) OVER()),'*') pct_of_max
  47. 19    FROM hist_data
  48. 20  ORDER BY endpoint_value;
  49. Enter value for owner: SYS
  50. old  11: WHERE     owner = '&owner'
  51. new  11: WHERE     owner = 'SYS'
  52. Enter value for table: MACLEAN
  53. old  12:       AND table_name = '&table'
  54. new  12:       AND table_name = 'MACLEAN'
  55. Enter value for column: STATUS
  56. old  13:       AND column_name = '&column'
  57. new  13:       AND column_name = 'STATUS'

  58. CHAR_VALUE
  59. --------------------------------------------------------------------------------
  60. BUCKET_COUNT        PCT
  61. ------------ ----------
  62. PCT_OF_MAX
  63. --------------------------------------------------------------------------------
  64. VALIC3
  65.         5546        100
  66. *************************************************

  67. 以上使用AUTO_SAMPLE自动采样大小 得到我们认为不准确的直方图


  68. 我们手动指定100%的采样比例

  69. SQL> exec dbms_stats.gather_table_stats(ownname => 'SYS',tabname => 'MACLEAN',estimate_percent => 100);

  70. PL/SQL procedure successfully completed.


  71. SQL> set linesize 80;
  72. SQL> WITH hist_data AS (
  73.   2  SELECT endpoint_value,endpoint_actual_value,
  74.   3         NVL(LAG (endpoint_value) OVER (ORDER BY endpoint_value),' ') prev_value,
  75.   4         endpoint_number,
  76.   5         endpoint_number,
  77.   6         endpoint_number
  78.   7         - NVL (LAG (endpoint_number) OVER (ORDER BY endpoint_value), 0)
  79.   8             bucket_count
  80.   9  FROM dba_tab_histograms
  81. 10  JOIN dba_tab_col_statistics USING (owner, table_name,column_name)
  82. 11  WHERE     owner = '&owner'
  83. 12        AND table_name = '&table'
  84. 13        AND column_name = '&column'
  85. 14        AND histogram='FREQUENCY')
  86. 15  SELECT hexstr(endpoint_value) char_value,
  87. 16         bucket_count,
  88. 17         ROUND(bucket_count*100/SUM(bucket_count) OVER(),2) PCT,
  89. 18         RPAD(' ',ROUND(bucket_count*50/MAX(bucket_count) OVER()),'*') pct_of_max
  90. 19    FROM hist_data
  91. 20  ORDER BY endpoint_value;
  92. Enter value for owner: SYS
  93. old  11: WHERE     owner = '&owner'
  94. new  11: WHERE     owner = 'SYS'
  95. Enter value for table: MACLEAN
  96. old  12:       AND table_name = '&table'
  97. new  12:       AND table_name = 'MACLEAN'
  98. Enter value for column: STATUS
  99. old  13:       AND column_name = '&column'
  100. new  13:       AND column_name = 'STATUS'

  101. CHAR_VALUE
  102. --------------------------------------------------------------------------------
  103. BUCKET_COUNT        PCT
  104. ------------ ----------
  105. PCT_OF_MAX
  106. --------------------------------------------------------------------------------
  107. INVALI
  108.           10        .02


  109. VALIC3
  110.        55539      99.98
  111. *************************************************
复制代码


得到期望的直方图

回复 只看该作者 道具 举报

5#
发表于 2011-12-1 22:10:04
注意今后如果贴代码的话,建议用代码模式"
  1. " "
复制代码
"

回复 只看该作者 道具 举报

您需要登录后才可以回帖 登录 | 注册

QQ|手机版|Archiver|Oracle数据库数据恢复、性能优化

GMT+8, 2024-11-15 02:06 , Processed in 0.109508 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部
TEL/電話+86 13764045638
Email service@parnassusdata.com
QQ 47079569