EXPLAIN SELECT host, count(DISTINCT ip_address) as host_count FROM analytics_visits WHERE user_id = '2c182b8f-ad65-4543-8ac9-2c47bb20172d' AND host <> '' AND status >= 200 AND status < 300 GROUP BY host ORDER BY host_count DESC; QUERY PLAN --------------------------------------------------------------------------------------------------------------------- Sort (cost=277960.43..277960.75 rows=128 width=23) Sort Key: (count(DISTINCT ip_address)) DESC -> GroupAggregate (cost=272617.22..277955.95 rows=128 width=23) Group Key: host -> Sort (cost=272617.22..274396.37 rows=711660 width=79) Sort Key: host -> Bitmap Heap Scan on analytics_visits (cost=10039.64..171818.42 rows=711660 width=79) Recheck Cond: (user_id = '2c182b8f-ad65-4543-8ac9-2c47bb20172d'::uuid) Filter: (((host)::text <> ''::text) AND (status >= 200) AND (status < 300)) -> Bitmap Index Scan on analytics_visits_user_id_idx (cost=0.00..9861.73 rows=876439 width=0) Index Cond: (user_id = '2c182b8f-ad65-4543-8ac9-2c47bb20172d'::uuid) JIT: Functions: 11 Options: Inlining false, Optimization false, Expressions true, Deforming true (14 rows)