I am trying to speed up the following query, which currently takes around 10 seconds to run:
SELECT a.id, a.name, a.slug, a.status, p.year FROM pensions_agency a LEFT OUTER JOIN pensions_pension p ON (a.id = p.agency_id) WHERE p.year <= 2016 OR p.year IS NULL GROUP BY a.id, p.year ORDER BY a.name asc, p.year desc;
When I ran this query using EXPLAIN ANALYZE the results are:
Sort (cost=284105.93..284107.09 rows=462 width=108) (actual time=9076.814..9076.848 rows=255 loops=1) Sort Key: a.name, p.year DESC Sort Method: quicksort Memory: 76kB -> HashAggregate (cost=284080.87..284085.49 rows=462 width=108) (actual time=9074.155..9074.256 rows=255 loops=1) Group Key: a.id, p.year -> Hash Right Join (cost=3.49..255565.03 rows=5703167 width=108) (actual time=0.184..6663.945 rows=5702979 loops=1) Hash Cond: (p.agency_id = a.id) Filter: ((p.year <= 2016) OR (p.year IS NULL)) Rows Removed by Filter: 60319 -> Seq Scan on pensions_pension p (cost=0.00..161907.97 rows=5763297 width=6) (actual time=0.014..1652.593 rows=5763297 loops=1) -> Hash (cost=2.66..2.66 rows=66 width=106) (actual time=0.142..0.142 rows=66 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 17kB -> Seq Scan on pensions_agency a (cost=0.00..2.66 rows=66 width=106) (actual time=0.019..0.064 rows=66 loops=1) Planning time: 0.700 ms Execution time: 9077.057 ms
I tried to add an index to the year column, but I don’t believe it had any improvements. Any idea on a strategy I can implement to speed up this query?
✓ Extra quality
ExtraProxies brings the best proxy quality for you with our private and reliable proxies
✓ Extra anonymity
Top level of anonymity and 100% safe proxies – this is what you get with every proxy package
✓ Extra speed
1,ooo mb/s proxy servers speed – we are way better than others – just enjoy our proxies!
USA proxy location
We offer premium quality USA private proxies – the most essential proxies you can ever want from USA
Our proxies have TOP level of anonymity + Elite quality, so you are always safe and secure with your proxies
Use your proxies as much as you want – we have no limits for data transfer and bandwidth, unlimited usage!
Superb fast proxy servers with 1,000 mb/s speed – sit back and enjoy your lightning fast private proxies!
99,9% servers uptime
Alive and working proxies all the time – we are taking care of our servers so you can use them without any problems
No usage restrictions
You have freedom to use your proxies with every software, browser or website you want without restrictions
Perfect for SEO
We are 100% friendly with all SEO tasks as well as internet marketing – feel the power with our proxies
Buy more proxies and get better price – we offer various proxy packages with great deals and discounts
We are working 24/7 to bring the best proxy experience for you – we are glad to help and assist you!