關於Oracle 數據庫使用dba_tables或者all_tables或者user_tables統計數據時,與直接查詢表統計時數據不一致的記錄


1. 今天寫代碼發現這個問題,這里記錄一下, 不一致的原因是因為  dba_tables 、all_tables、user_tables 不是實時的反應表的數據的,所以需要在查詢統計之前對表進行手動分析,步驟如下

第一步:

  

create or replace function count_rows(table_name in varchar2,owner in varchar2 default null)
return number authid current_user IS
num_rows number;
stmt varchar2(2000);
begin
if owner is null then
stmt := 'select count(*) from "' || table_name || '"';
else
stmt := 'select count(*) from "' || owner || '"."' || table_name || '"';
end if;
execute immediate stmt
into num_rows;
return num_rows;
end;

 

第二步:

   analyze table 表名 compute statistics; // 如果有多個表,那么就要把此語音復制為多個,修改一下表名執行一下。 

 

這就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM