创建表t
create table t(id,number);
create index t_id on t(id);
begin
for i in 1..10000
loop
insert into t values(i,'xiaobai');
end loop;
commit;
end;
/
select * from t;
desc dba_histograms
endpoint_number:桶的编号
endpoint_value:桶的值
endpoint_actural_value:
select endpoint_number,endpoint_value from dba_histograms where owner='SCOTT' and table_name='T' and column_name='ID';
第一次查查不到。
exec dbms_stats.gather_table_stats('SOCTT','T',method_opt=>'FOR COLUMNS SIZE 10 id');
SIZE 10是创建10个桶的意思。上面这个过程可以创建直方图。
再查一次直方图
select endpoint_number,endpoint_value from dba_histograms where owner='SCOTT' and table_name='T' and column_name='ID';
则能查出。
update t set id=66 where id>23 and id<8700;
exec dbms_stats.gather_table_stats('SOCTT','T',method_opt=>'FOR COLUMNS SIZE 10 id');
将统计信息再生成一遍。
select endpoint_number,endpoint_value from dba_histograms where owner='SCOTT' and table_name='T' and column_name='ID';
发现统计信息不同了。
set autotrace on
此时select * from t where id=66;就应该是全表扫描。
select * from t where id =9999;就应该是用索引。
由于有了直方图,查询优化器就产生了不同的执行计划。
Auto Statistics Collecting
exec dbms_stats.gather_schema_stats -
(OWNNAME => 'OE', - OPTION => 'GATHER AUTO');
create table t(id,number);
create index t_id on t(id);
begin
for i in 1..10000
loop
insert into t values(i,'xiaobai');
end loop;
commit;
end;
/
select * from t;
desc dba_histograms
endpoint_number:桶的编号
endpoint_value:桶的值
endpoint_actural_value:
select endpoint_number,endpoint_value from dba_histograms where owner='SCOTT' and table_name='T' and column_name='ID';
第一次查查不到。
exec dbms_stats.gather_table_stats('SOCTT','T',method_opt=>'FOR COLUMNS SIZE 10 id');
SIZE 10是创建10个桶的意思。上面这个过程可以创建直方图。
再查一次直方图
select endpoint_number,endpoint_value from dba_histograms where owner='SCOTT' and table_name='T' and column_name='ID';
则能查出。
update t set id=66 where id>23 and id<8700;
exec dbms_stats.gather_table_stats('SOCTT','T',method_opt=>'FOR COLUMNS SIZE 10 id');
将统计信息再生成一遍。
select endpoint_number,endpoint_value from dba_histograms where owner='SCOTT' and table_name='T' and column_name='ID';
发现统计信息不同了。
set autotrace on
此时select * from t where id=66;就应该是全表扫描。
select * from t where id =9999;就应该是用索引。
由于有了直方图,查询优化器就产生了不同的执行计划。
Auto Statistics Collecting
exec dbms_stats.gather_schema_stats -
(OWNNAME => 'OE', - OPTION => 'GATHER AUTO');