2017-06-02 49 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(*)的好处是在服务器负载的价格和时间更精确的结果等待

相关问题