EXPLAIN select p_brand, p_type, p_size, count(distinct ps_suppkey) as supplier_cnt from partsupp, part where p_partkey = ps_partkey and p_brand <> 'Brand#55' and p_type not like 'PROMO BRUSHED%' and p_size in (9, 38, 7, 43, 26, 42, 16, 21) and ps_suppkey not in ( select s_suppkey from supplier where s_comment like '%Customer%Complaints%' ) group by p_brand, p_type, p_size order by supplier_cnt desc, p_brand, p_type, p_size; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=528403.14..528472.69 rows=27820 width=40) Sort Key: (count(DISTINCT partsupp.ps_suppkey)), part.p_brand, part.p_type, part.p_size -> GroupAggregate (cost=518652.55..526349.49 rows=27820 width=40) Group Key: part.p_brand, part.p_type, part.p_size -> Sort (cost=518652.55..520136.30 rows=593499 width=40) Sort Key: part.p_brand, part.p_type, part.p_size -> Hash Join (cost=100083.75..445509.80 rows=593499 width=40) Hash Cond: (partsupp.ps_partkey = part.p_partkey) -> Seq Scan on partsupp (cost=3430.03..274349.04 rows=4000120 width=8) Filter: (NOT (hashed SubPlan 1)) SubPlan 1 -> Seq Scan on supplier (cost=0.00..3430.00 rows=10 width=4) Filter: ((s_comment)::text ~~ '%Customer%Complaints%'::text) -> Hash (cost=90625.58..90625.58 rows=296732 width=40) -> Seq Scan on part (cost=0.00..90625.58 rows=296732 width=40) Filter: ((p_brand <> 'Brand#55'::bpchar) AND ((p_type)::text !~~ 'PROMO BRUSHED%'::text) AND (p_size = ANY ('{9,38,7,43,26,42,16,21}'::integer[]))) (16 rows)