Connecting Tech Pros Worldwide Help | Site Map

Query to get total rows in a table in faster way.

Newbie
 
Join Date: Nov 2008
Posts: 4
#1: Dec 5 '08
How can i get the total rows of a table:

If i use "select count(*) from <tablename>" it will take lot of time to get the total row count.

for example in MSSQL we have "SELECT ROWS FROM sysindexes WHERE id = OBJECT_ID(<Table name>) AND indid < 2"

I am looking some system index tables which hold the total rows as a statistic values of the table so that i can query that table to get the total rows faster.

My search observations:
I refered this URL: PostgreSQL: Documentation: Manuals: PostgreSQL 8.3: The Statistics Collector

we have "pg_stat_all_tables" it says that this table holds the number of rows information. But when i actually opened that table in the PGAdmin and looked into the columns of that table i did not find any of the columns to hold the Total rows.

I also found that if we select a table in PGAdmin we get the properties in the right side pane, in that we have "rows (counted)" value. But i dont knwo how to retrieve that properties values.

Can any one help me in getting the total rows of the table.

Thanks
Moderator
 
Join Date: Dec 2006
Location: Europe
Posts: 292
#2: Dec 14 '08

re: Query to get total rows in a table in faster way.


you can look into a table pg_class. There is a colum name reltuples wich stores total number of rows for each table in a database. But to be sure the number is correct you should first vacuum the table wich total number of rows you want to know. Column reltuples doesn't change dynamically, if i am right.
Reply