What is the most efficient way to get a row count of all tables in my database?
I'm using a Postgres database.
Example Result
table_name row_count
------------ -------------
some_table 1,234
foobar 5,678
another_table 32
...
What is the most efficient way to get a row count of all tables in my database?
I'm using a Postgres database.
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.
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?" : 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 the link