1

I have this query to get number of rows for the given filters, it takes too long time to get the result since db is too big, is there any way to optimise it i'm using postgresql 8.2

 SELECT COUNT(1) as numrows from ( select lower(column1) as column1, column2, column3, column4, sum(column5) as column5, sum(column6) as column6 
    from table_name tablename 
    where column_date >= 'SOME DATE' and column_date < 'SOME DATE' 
    group by lower(column1) as column1, column2, column3, column4 ORDER BY column5 desc) allRows

Here is what i got with EXPLAIN Query

XN Aggregate  (cost=849751.05..849751.05 rows=1 width=0)
  ->  XN Subquery Scan allrows  (cost=805802.05..842426.22 rows=2929933 width=0)
        ->  XN HashAggregate  (cost=805802.05..813126.89 rows=2929933 width=26)
              ->  XN Seq Scan on table_name tablename   (cost=0.00..512808.79 rows=29299326 width=26)
                    Filter: ((column_date < 'SOME DATE'::date) AND (column_date >= 'SOME DATE'::date))
rohitmb
  • 151
  • 1
  • 3
  • 13
  • Show us the output from EXPLAIN ANALYZE. – zero323 Sep 24 '13 at 12:46
  • 1
    Also (1), what is the reason for the sum() calls and the "order by"? (2) Why are you using a version that reached end-of-life some two years ago? http://www.postgresql.org/support/versioning/ – Richard Huxton Sep 24 '13 at 12:50

1 Answers1

1

The best way to optimize performance is to upgrade to a current version of PostgreSQL, as @Richard already commented. 8.2 is long dead and forgotten and can hardly keep up. It is also a security risk.

Also, get rid of the sum() calculations and ORDER BY, since neither contributes to the result. Actually, the whole SELECT list is of no use.

SELECT COUNT(*) AS numrows
FROM  (
   SELECT 1
   FROM   table_name
   WHERE  column_date >= 'SOME DATE'
   AND    column_date <  'SOME DATE' 
   GROUP  BY lower(column1), column2, column3, column4
   ) sub

Other than that, your EXPLAIN output indicates that you are lacking an index like:

CREATE INDEX table_name_column_date_idx ON table_name(column_date);
Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228