By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,558 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,558 IT Pros & Developers. It's quick & easy.

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

P: 4
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
Dec 5 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 700
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.
Dec 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.