'POSTGRESQL: query optimization

I have a query that I cannot optimize:

SELECT evse_label AS evse_label,
evc_model AS evc_model,
address AS address,
city AS city,
message AS message,
reason AS reason,
min(timestamp) AS oldest_timestamp,
max(timestamp) AS latest_timestamp,
count(message) AS "# messages"
FROM public.om_logbook_mart
GROUP BY evse_label,
evc_model,
address,
city,
message,
reason
ORDER BY oldest_timestamp DESC
LIMIT 10000

I tried to create the following index:

CREATE INDEX evselabel_evc_model_address_city_reason_message_timestamp_idx
ON om_logbook_mart (evse_label,evc_model,address,city,message,reason,timestamp);

Unfortunately nothing changes and the query still lasts around 40s.

Doing an explain analyze, this is the output:

Limit  (cost=1184359.89..1184384.89 rows=10000 width=95) (actual time=39395.145..39451.451 rows=10000 loops=1)
  ->  Sort  (cost=1184359.89..1186097.37 rows=694989 width=95) (actual time=39394.488..39449.924 rows=10000 loops=1)
        Sort Key: (min("timestamp")) DESC
        Sort Method: top-N heapsort  Memory: 3375kB
        ->  Finalize GroupAggregate  (cost=856703.95..1134710.88 rows=694989 width=95) (actual time=30343.111..39339.675 rows=97445 loops=1)
              Group Key: evse_label, evc_model, address, city, message, reason
              ->  Gather Merge  (cost=856703.95..1096486.49 rows=1389978 width=95) (actual time=30343.062..39141.342 rows=231382 loops=1)
                    Workers Planned: 2
                    Workers Launched: 2
                    ->  Partial GroupAggregate  (cost=855703.92..935048.51 rows=694989 width=95) (actual time=29867.817..37735.438 rows=77127 loops=3)
                          Group Key: evse_label, evc_model, address, city, message, reason
                          ->  Sort  (cost=855703.92..862943.39 rows=2895788 width=79) (actual time=29867.788..35909.736 rows=2317226 loops=3)
                                Sort Key: evse_label, evc_model, address, city, message, reason
                                Sort Method: external merge  Disk: 215024kB
                                Worker 0:  Sort Method: external merge  Disk: 203704kB
                                Worker 1:  Sort Method: external merge  Disk: 219048kB
                                ->  Parallel Seq Scan on om_logbook_mart  (cost=0.00..287564.88 rows=2895788 width=79) (actual time=0.033..1852.787 rows=2317226 loops=3)
Planning Time: 1.285 ms
Execution Time: 39478.630 ms

Can you make me understand how to optimize it?

UPDATE: After setting work_mem to 500MB:

Limit  (cost=543440.12..543465.12 rows=10000 width=95) (actual time=6466.267..6468.723 rows=10000 loops=1)
  ->  Sort  (cost=543440.12..545184.77 rows=697860 width=95) (actual time=6466.265..6467.809 rows=10000 loops=1)
        Sort Key: (min("timestamp")) DESC
        Sort Method: top-N heapsort  Memory: 3646kB
        ->  HashAggregate  (cost=486607.40..493586.00 rows=697860 width=95) (actual time=6384.249..6425.960 rows=97403 loops=1)
              Group Key: evse_label, evc_model, address, city, message, reason
              Batches: 1  Memory Usage: 49169kB
              ->  Seq Scan on om_logbook_mart  (cost=0.00..329588.97 rows=6978597 width=79) (actual time=0.040..2362.520 rows=6978597 loops=1)
Planning Time: 0.135 ms
Execution Time: 6489.381 ms

Can i do something more?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source