简体   繁体   English

获取 Postgres 数据库中每个表的行数

[英]Get a row count of every table in Postgres database

What is the most efficient way to get a row count of all tables in my database?获取数据库中所有表的行数的最有效方法是什么?

I'm using a Postgres database.我正在使用 Postgres 数据库。

Example Result示例结果

table_name     row_count
------------   -------------
some_table     1,234
foobar         5,678
another_table  32
... 

if you want a perticular table's rowcount then it will work如果你想要一个特定表的行数,那么它会起作用

SELECT reltuples FROM pg_class WHERE oid = 'my_schema.my_table'::regclass;

reltuples is a column from pg_class table, it holds data about "number of rows >in the table. This is only an estimate used by the planner. reltuples 是 pg_class 表中的一列,它保存有关“表中的行数”的数据。这只是规划器使用的估计值。

and if your want a list of all tables with its rowcount then it will do the job如果您想要所有表及其行数的列表,那么它将完成这项工作

SELECT
  pgClass.relname   AS tableName,
  pgClass.reltuples AS rowCount
FROM
  pg_class pgClass
INNER JOIN
  pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE
  pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
  pgClass.relkind='r'

"Why is "SELECT count(*) FROM bigtable;" slow?" “为什么“SELECT count(*) FROM bigtable;”很慢? : count(*) 计数(*)

For total row count of entire database use this对于整个数据库的总行数使用这个

SELECT
  SUM(pgClass.reltuples) AS totalRowCount
FROM
  pg_class pgClass
LEFT JOIN
  pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE
  pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
  pgClass.relkind='r'

And the row counts for the specific tables in the same database go for this并且同一数据库中特定表的行数用于此

SELECT
  pgClass.relname   AS tableName,
  pgClass.reltuples AS rowCount
FROM
  pg_class pgClass
LEFT JOIN
  pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE
  pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
  pgClass.relkind='r'

For reference here is thelink参考这里是链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM