2017-06-02 12 views

回答

3

這可以用一些XML魔法來完成:

select table_schema, table_name, 
     (xpath('/row/count/text()', query_to_xml('select count(*) from '||format('%I.%I', table_schema, table_name), true, true, '')))[1]::text::int as row_count 
from information_schema.tables 
where table_schema = 'public' 
3

https://www.postgresql.org/docs/current/static/monitoring-stats.html

n_live_tup估計數量的活排

t=# select relname,n_live_tup 
from pg_stat_all_tables 
where schemaname = 'public' 
order by n_live_tup desc 
limit 3; 
     relname  | n_live_tup 
------------+---------------------+------------ 
    x_pricing   | 96493977 
    x_forum    | 57696510 
    x_uploading   | 55477043 
(3 rows) 

當然這些數據將達到某個近似水平。要計算確切的數字,您將需要動態的plpgsql(這btw會給你更接近的數字,但仍達到一些近似水平)。這兩種近似值取決於您更改數據和運行真空的頻率...

這種方法的好處當然是消耗更少的資源(負載和時間)。 COUNT(*)的好處是在服務器負載的價格和時間更精確的結果等待

相關問題