如何获取 postgresql 9.5 中特定模式中存在的所有 table 的 table 行数?
how to get the table row count of all the tables present in particular schema in postgresql 9.5?
如何获取 postgresql 9.5 中特定模式中存在的所有 table 的 table 行数?我希望结果为 table_name | row_count。如何使用查询来完成?
https://www.postgresql.org/docs/current/static/monitoring-stats.html
n_live_tup Estimated number of live rows
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(顺便说一下,它会给您更接近的数字,但仍达到某个近似水平)。两种近似值都取决于您更改数据的频率和 运行 vacuum...
这种方法的好处当然是消耗更少的资源(负载和时间)。 count(*) 的好处是以服务器负载和等待时间为代价的更精确的结果
这可以通过一些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'
如何获取 postgresql 9.5 中特定模式中存在的所有 table 的 table 行数?我希望结果为 table_name | row_count。如何使用查询来完成?
https://www.postgresql.org/docs/current/static/monitoring-stats.html
n_live_tup Estimated number of live rows
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(顺便说一下,它会给您更接近的数字,但仍达到某个近似水平)。两种近似值都取决于您更改数据的频率和 运行 vacuum...
这种方法的好处当然是消耗更少的资源(负载和时间)。 count(*) 的好处是以服务器负载和等待时间为代价的更精确的结果
这可以通过一些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'