0

I am running select count() against large table in postgresql, over 10 billion rows. The count() query is taking over 25 minutes to return. Slow is one thing, but that extraordinary and leads to to think there is something wrong within DB, table, ? What might be the cause od such slow performance? DB is AWS Serverless but appears to be adequately resourced (ACU's, etc) Query Plan is below. Query does Parallel Seq Scan that is taking all of time, but why 25 mins?

                        QUERY PLAN                         
-----------------------------------------------------------
 [                                                        +
   {                                                      +
     "Plan": {                                            +
       "Node Type": "Aggregate",                          +
       "Strategy": "Plain",                               +
       "Partial Mode": "Finalize",                        +
       "Parallel Aware": false,                           +
       "Startup Cost": 222432514.88,                      +
       "Total Cost": 222432514.89,                        +
       "Plan Rows": 1,                                    +
       "Plan Width": 8,                                   +
       "Actual Startup Time": 1524678.987,                +
       "Actual Total Time": 1524798.515,                  +
       "Actual Rows": 1,                                  +
       "Actual Loops": 1,                                 +
       "Shared Hit Blocks": 2605348,                      +
       "Shared Read Blocks": 163231630,                   +
       "Shared Dirtied Blocks": 0,                        +
       "Shared Written Blocks": 0,                        +
       "Local Hit Blocks": 0,                             +
       "Local Read Blocks": 0,                            +
       "Local Dirtied Blocks": 0,                         +
       "Local Written Blocks": 0,                         +
       "Temp Read Blocks": 0,                             +
       "Temp Written Blocks": 0,                          +
       "I/O Read Time": 1044087497.561,                   +
       "I/O Write Time": 0.000,                           +
       "Plans": [                                         +
         {                                                +
           "Node Type": "Gather",                         +
           "Parent Relationship": "Outer",                +
           "Parallel Aware": false,                       +
           "Startup Cost": 222432514.67,                  +
           "Total Cost": 222432514.88,                    +
           "Plan Rows": 2,                                +
           "Plan Width": 8,                               +
           "Actual Startup Time": 1524678.978,            +
           "Actual Total Time": 1524798.508,              +
           "Actual Rows": 3,                              +
           "Actual Loops": 1,                             +
           "Workers Planned": 2,                          +
           "Workers Launched": 2,                         +
           "Single Copy": false,                          +
           "Shared Hit Blocks": 2605348,                  +
           "Shared Read Blocks": 163231630,               +
           "Shared Dirtied Blocks": 0,                    +
           "Shared Written Blocks": 0,                    +
           "Local Hit Blocks": 0,                         +
           "Local Read Blocks": 0,                        +
           "Local Dirtied Blocks": 0,                     +
           "Local Written Blocks": 0,                     +
           "Temp Read Blocks": 0,                         +
           "Temp Written Blocks": 0,                      +
           "I/O Read Time": 1044087497.561,               +
           "I/O Write Time": 0.000,                       +
           "Plans": [                                     +
             {                                            +
               "Node Type": "Aggregate",                  +
               "Strategy": "Plain",                       +
               "Partial Mode": "Partial",                 +
               "Parent Relationship": "Outer",            +
               "Parallel Aware": false,                   +
               "Startup Cost": 222431514.67,              +
               "Total Cost": 222431514.68,                +
               "Plan Rows": 1,                            +
               "Plan Width": 8,                           +
               "Actual Startup Time": 1524661.376,        +
               "Actual Total Time": 1524661.376,          +
               "Actual Rows": 1,                          +
               "Actual Loops": 3,                         +
               "Shared Hit Blocks": 2605348,              +
               "Shared Read Blocks": 163231630,           +
               "Shared Dirtied Blocks": 0,                +
               "Shared Written Blocks": 0,                +
               "Local Hit Blocks": 0,                     +
               "Local Read Blocks": 0,                    +
               "Local Dirtied Blocks": 0,                 +
               "Local Written Blocks": 0,                 +
               "Temp Read Blocks": 0,                     +
               "Temp Written Blocks": 0,                  +
               "I/O Read Time": 1044087497.561,           +
               "I/O Write Time": 0.000,                   +
               "Workers": [                               +
               ],                                         +
               "Plans": [                                 +
                 {                                        +
                   "Node Type": "Seq Scan",               +
                   "Parent Relationship": "Outer",        +
                   "Parallel Aware": true,                +
                   "Relation Name": "alf_node_properties",+
                   "Alias": "alf_node_properties",        +
                   "Startup Cost": 0.00,                  +
                   "Total Cost": 211112638.93,            +
                   "Plan Rows": 4527550293,               +
                   "Plan Width": 0,                       +
                   "Actual Startup Time": 1.430,          +
                   "Actual Total Time": 1238163.306,      +
                   "Actual Rows": 3624835814,             +
                   "Actual Loops": 3,                     +
                   "Shared Hit Blocks": 2605348,          +
                   "Shared Read Blocks": 163231630,       +
                   "Shared Dirtied Blocks": 0,            +
                   "Shared Written Blocks": 0,            +
                   "Local Hit Blocks": 0,                 +
                   "Local Read Blocks": 0,                +
                   "Local Dirtied Blocks": 0,             +
                   "Local Written Blocks": 0,             +
                   "Temp Read Blocks": 0,                 +
                   "Temp Written Blocks": 0,              +
                   "I/O Read Time": 1044087497.561,       +
                   "I/O Write Time": 0.000,               +
                   "Workers": [                           +
                   ]                                      +
                 }                                        +
               ]                                          +
             }                                            +
           ]                                              +
         }                                                +
       ]                                                  +
     },                                                   +
     "Planning": {                                        +
       "Shared Hit Blocks": 225,                          +
       "Shared Read Blocks": 0,                           +
       "Shared Dirtied Blocks": 0,                        +
       "Shared Written Blocks": 0,                        +
       "Local Hit Blocks": 0,                             +
       "Local Read Blocks": 0,                            +
       "Local Dirtied Blocks": 0,                         +
       "Local Written Blocks": 0,                         +
       "Temp Read Blocks": 0,                             +
       "Temp Written Blocks": 0,                          +
       "I/O Read Time": 0.000,                            +
       "I/O Write Time": 0.000                            +
     },                                                   +
     "Planning Time": 17.793,                             +
     "Triggers": [                                        +
     ],                                                   +
     "Execution Time": 1524798.554                        +
   }                                                      +
 ]
(1 row)

I ran Explain Plan on query. Looking at count() in reference to other slow queries involving this table. Also, vacuum/analyze has been run on table as recently as hour before running the count() query. Have monitored instance via AWS RDS monitoring and not seen anything would indicate DB is resource constrained I realize table is large but no query should take 25 minutes to complete

2down2
  • 1
  • 4
  • 10 billion or 10 million rows? Are there `WHERE` conditions or joins? – i486 Jul 19 '23 at 09:44
  • On postgres unlike Oracle and other more evolved databases the use of * in a count means it is going to retrieve all the data, so a full sequential read of evry column from that table is performed and very likely you are hitting the physical memory limit and running with Virtual memory. Surely you will get a faster response if you change that count * with the name of a uniquely indexed field or if it is possible by reducing the table record occupancy, .i.e. changing to text or varchar a column type char(30) in where there are a lot of shorter values. – A. Lion Jul 19 '23 at 09:57
  • The io read times do not match the actual times. AWS serverless must be using non standard units. You might need to ask them what is going on. But frankly i don't understand your disbelief, counting 12 billion rows will be slow. – jjanes Jul 19 '23 at 13:09
  • Read [Count estimate](https://wiki.postgresql.org/wiki/Count_estimate). – Adrian Klaver Jul 19 '23 at 15:12

1 Answers1

1

You are reading 1.3TB from disk at an average speed of 1.3MB/s. You can try to increase max_parallel_workers_per_gather, but it looks like you are I/O bound and would need faster disks.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Ok. I think other activity against DB may be related to seeing only 1.3MB/s with count() query. Also, tried query replacing count(*) with count(column) and it was equally slow. Will need to re-test when process causing traffic completes. Looking at DB I/O, it is consistently at 400-600 MB/s – 2down2 Jul 20 '23 at 10:02
  • `count(column)` is *slower* than `count(*)`, which is a parameterless aggregate. – Laurenz Albe Jul 20 '23 at 10:19