I have a table that has 60 million rows in a Postgresql database.
Table is as follows:
id varchar PK
cutomerId Integer
orderId Integer
modified timestamp.
Now I want to create a summary of customer and number of orders on a particular day for example
SELECT customerId, COUNT(orderId)
FROM orders o
WHERE DATE(o.modified) = '2023-04-27' GROUP by customerId
This takes up to 5 minutes to return in SQL query UI.
Is there any way I can speed this up?
here is my explain analyze:
Finalize GroupAggregate (cost=778305.85..778574.65 rows=1061 width=12) (actual time=43412.506..43435.712 rows=1089 loops=1)
Group Key: customerId
Buffers: shared hit=1414708 read=576689
I/O Timings: read=123468.547
-> Gather Merge (cost=778305.85..778553.43 rows=2122 width=12) (actual time=43412.472..43435.024 rows=3267 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=1414708 read=576689
I/O Timings: read=123468.547
-> Sort (cost=777305.82..777308.47 rows=1061 width=12) (actual time=43341.758..43341.892 rows=1089 loops=3)
Sort Key: customerId
Sort Method: quicksort Memory: 100kB
Worker 0: Sort Method: quicksort Memory: 100kB
Worker 1: Sort Method: quicksort Memory: 100kB
Buffers: shared hit=1414708 read=576689
I/O Timings: read=123468.547
-> Partial HashAggregate (cost=777241.89..777252.50 rows=1061 width=12) (actual time=43340.904..43341.102 rows=1089 loops=3)
Group Key: customerId
Buffers: shared hit=1414694 read=576689
I/O Timings: read=123468.547
-> Parallel Index Scan using orders_idx on orders o (cost=0.43..770040.97 rows=1440183 width=8) (actual time=0.205..42743.905 rows=1097188 loops=3)
Index Cond: ((modified >= '2022-02-16'::date) AND (modified < '2022-02-17'::date))
Buffers: shared hit=1414694 read=576689
I/O Timings: read=123468.547
Planning Time: 10.171 ms
Execution Time: 43436.656 ms
EXPLAIN ANALYZE
say for this query? Also are you missing aGROUP BY customerId
clause?DATE(o.modified) = '2023-04-27'
with range condition:o.modified >= '2023-04-27'::date and o.modified < '2023-04-28'::date
, check that helpful index existsGROUP BY customerId
clause?