Hi,
I am attaching the result of EXPLAIN ANALYZE
on my laptop run from pgadmin4 and hope it helps to find the cause of the performance problem:
Sort (cost=229.03..229.53 rows=200 width=40) (actual time=12372.031..12372.032 rows=5 loops=1)
Sort Key: (round(sum(orders.total))) DESC
Sort Method: quicksort Memory: 25kB
-> HashAggregate (cost=218.89..221.39 rows=200 width=40) (actual time=12372.013..12372.017 rows=5 loops=1)
Group Key: users.source
-> Nested Loop (cost=0.00..213.89 rows=1000 width=40) (actual time=17.378..12270.460 rows=18760 loops=1)
-> Foreign Scan on orders (cost=0.00..100.00 rows=1000 width=16) (actual time=16.708..76.350 rows=18760 loops=1)
-> Index Scan using users_pkey on users (cost=0.00..0.11 rows=1 width=40) (actual time=0.588..0.588 rows=1 loops=18760)
Index Cond: (id = orders.user_id)
Planning Time: 6.541 ms
Execution Time: 12664.493 ms