Home 2015 October PostgreSQL: Fast way to find the row count of a Table

PostgreSQL: Fast way to find the row count of a Table

(追記) (追記ここまで)

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

If a table has a 5000 or 500000 or 5000000000 records and the requirement is to find the total row count of the table, most of the Database Developer always executes COUNT(*) for getting the row count.

I found that many of our team members also execute COUNT(*), but just imagine that table has 5000000000 rows and COUNT(*) is taking a long time for getting the number of records.

The counting of the rows in such a big table always creates the performance issue and its also required I/O operation.

If you need exact row count for a given time, COUNT(*) is mandatory.
But you can speed up this dramatically if the count does not have to be exact.
You can use a metadata table or statistical table to find the row count, which is quite same as real row count.

Because the nature of MVCC, sometimes you can find the difference between actual record count and statistical table’s record count.

What is Multi Version Concurrency Control (MVCC)

[フレーム]

You can easily find roughly row count using statistical tables within 1 second.

I tested and compared the two results in my local machine with the row count of 5000000000.

My COUNT (*) returns a result after 8 to 10 minutes and also taken 10% to 25% CPU and Memory.
After this, I found row count from the statistical table, and it didn’t take even one second.

But I found the little difference between both the count. The count of a statistical table is higher than the actual count. (12585-row count is greater) because of MVCC.

You should configure auto-vacuum and analyze on the table.

I executed vacuum and analyze on the table, and now my count is same.

I would suggest, please use the statistical table for the row counts.
I am providing two different scripts for finding the rough row count in the PostgreSQL.

1
2
3
SELECT reltuples::bigint AS EstimatedCount
FROM pg_class
WHERE oid = 'public.TableName'::regclass;

1
2
3
4
5
6
SELECT
schemaname
,relname
,n_live_tup AS EstimatedCount
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;

Anvesh Patel
(追記) (追記ここまで)
PostgreSQL: Script to find a Missing Indexes of the schema MySQL: Query Interview Questions and Answers
Comments: 3
  1. more
    November 1, 2015 at 12:13 pm

    I simply want to tell you that I am all new to blogs and absolutely liked your website. Likely I’m want to bookmark your website . You absolutely have fabulous writings. Regards for sharing your web-site.

  2. idham
    August 23, 2016 at 11:48 am

    what if i have query like this ‘SELECT COUNT(*) FROM USERS WHERE address= ‘aaa’ AND name=’ddd’;

    • August 23, 2016 at 5:26 pm

      For specific filter we should apply proper indexing like, Partial Index, BRIN Index. Other we can use any third party OLAP system. I don’t think any other option is there.
      If anyone knows, they can share here.

Posting....

AltStyle によって変換されたページ (->オリジナル) /