QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=3360980.80..3361005.80 rows=10000 width=2077) (actual time=3323.649..3331.759 rows=10000 loops=1) CTE all_popularity -> GroupAggregate (cost=0.42..78769.78 rows=12984 width=35) (actual time=0.383..1598.115 rows=16993 loops=1) Group Key: spy_track_popularity.track_spyid -> Index Scan using idx_spy_track_popularity_spyid on spy_track_popularity (cost=0.42..49545.92 rows=726539 width=35) (actual time=0.005..540.417 rows=726539 loops=1) CTE playlists -> GroupAggregate (cost=3301.15..3708.35 rows=16966 width=46) (actual time=72.079..119.267 rows=16966 loops=1) Group Key: spy_playlist_track.track_spyid -> Sort (cost=3301.15..3366.19 rows=26017 width=46) (actual time=72.051..78.865 rows=26017 loops=1) Sort Key: spy_playlist_track.track_spyid Sort Method: quicksort Memory: 2801kB -> Seq Scan on spy_playlist_track (cost=0.00..1393.17 rows=26017 width=46) (actual time=0.010..9.739 rows=26017 loops=1) -> Sort (cost=3278502.67..3281256.25 rows=1101433 width=2077) (actual time=3323.648..3328.608 rows=10000 loops=1) Sort Key: spy_tracks.first_seen DESC Sort Method: external merge Disk: 11920kB -> Hash Join (cost=104454.00..197644.64 rows=1101433 width=2077) (actual time=1154.223..3271.330 rows=16966 loops=1) Hash Cond: ((all_popularity.track_spyid)::text = (spy_tracks.spyid)::text) -> CTE Scan on all_popularity (cost=0.00..259.68 rows=12984 width=64) (actual time=0.388..1631.643 rows=16993 loops=1) -> Hash (cost=99883.92..99883.92 rows=16966 width=2077) (actual time=1153.327..1153.327 rows=16966 loops=1) Buckets: 2048 Batches: 16 Memory Usage: 2366kB -> Hash Left Join (cost=43406.79..99883.92 rows=16966 width=2077) (actual time=665.218..1095.431 rows=16966 loops=1) Hash Cond: ((spy_tracks.primary_artist_spyid)::text = (sa.spyid)::text) -> Hash Right Join (cost=26632.02..76161.87 rows=16966 width=1273) (actual time=619.355..993.479 rows=16966 loops=1) Hash Cond: ((p5.track_spyid)::text = (spy_tracks.spyid)::text) Join Filter: ((((spy_tracks.first_seen)::date + '5 days'::interval) = (p5.as_of)::date) AND ((spy_tracks.first_seen)::date > (('now'::cstring)::date - '60 days'::interval))) Rows Removed by Join Filter: 604767 -> Seq Scan on spy_track_popularity p5 (cost=0.00..15138.74 rows=612103 width=35) (actual time=0.023..207.690 rows=611108 loops=1) Filter: (value > 30) Rows Removed by Filter: 115431 -> Hash (cost=23751.94..23751.94 rows=16966 width=1261) (actual time=269.776..269.776 rows=16966 loops=1) Buckets: 4096 Batches: 8 Memory Usage: 2832kB -> Hash Join (cost=20274.34..23751.94 rows=16966 width=1261) (actual time=144.592..235.838 rows=16966 loops=1) Hash Cond: ((playlists.track_spyid)::text = (spy_tracks.spyid)::text) -> CTE Scan on playlists (cost=0.00..339.32 rows=16966 width=64) (actual time=72.081..130.917 rows=16966 loops=1) -> Hash (cost=17522.93..17522.93 rows=16993 width=1197) (actual time=72.431..72.431 rows=16993 loops=1) Buckets: 4096 Batches: 8 Memory Usage: 2698kB -> Seq Scan on spy_tracks (cost=0.00..17522.93 rows=16993 width=1197) (actual time=0.663..34.726 rows=16993 loops=1) -> Hash (cost=15310.12..15310.12 rows=12212 width=850) (actual time=45.714..45.714 rows=12212 loops=1) Buckets: 8192 Batches: 4 Memory Usage: 2784kB -> Seq Scan on spy_artists sa (cost=0.00..15310.12 rows=12212 width=850) (actual time=0.007..26.798 rows=12212 loops=1) Planning time: 2.314 ms Execution time: 3339.839 ms (42 rows)