1. 程式人生 > >oracle 檢視列統計資訊中 low_value

oracle 檢視列統計資訊中 low_value

-- 準備環境  
create table t1(id int,name varchar2(10));  
begin  
  for i in 1001..100000 loop  
    insert into  t1 values(i,'china'||to_char(i));  
  end loop;  
  commit;  
end; 

-- 收集資訊
begin
  dbms_stats.gather_table_stats(ownname => 'LOGE',tabname => 'SMALL',cascade => true);
end;

-- 建立函式
create or replace function f_display_raw(rawval raw, type varchar2)
  return varchar2 is
  cn  number;
  cv  varchar2(32);
  cd  date;
  cnv nvarchar2(32);
  cr  rowid;
  cc  char(32);
  cbf binary_float;
  cbd binary_double;
begin
  if (type = 'VARCHAR2') then
    dbms_stats.convert_raw_value(rawval, cv);
    return to_char(cv);
  elsif (type = 'DATE') then
    dbms_stats.convert_raw_value(rawval, cd);
    return to_char(cd);
  elsif (type = 'NUMBER') then
    dbms_stats.convert_raw_value(rawval, cn);
    return to_char(cn);
  elsif (type = 'BINARY_FLOAT') then
    dbms_stats.convert_raw_value(rawval, cbf);
    return to_char(cbf);
  elsif (type = 'BINARY_DOUBLE') then
    dbms_stats.convert_raw_value(rawval, cbd);
    return to_char(cbd);
  elsif (type = 'NVARCHAR2') then
    dbms_stats.convert_raw_value(rawval, cnv);
    return to_char(cnv);
  elsif (type = 'ROWID') then
    dbms_stats.convert_raw_value(rawval, cr);
    return to_char(cr);
  elsif (type = 'CHAR') then
    dbms_stats.convert_raw_value(rawval, cc);
    return to_char(cc);
  else
    return 'UNKNOWN DATATYPE';
  end if;
end;
/

-- 檢視
select a.column_name,
       display_raw(a.low_value, b.data_type) as low_val,
       display_raw(a.high_value, b.data_type) as high_val,
       b.data_type
  from user_tab_col_statistics a, user_tab_cols b
 where a.table_name = 'T1'
   and a.table_name = b.table_name
   and a.column_name = b.column_name;