BEGIN; BEGIN 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#45' and p_type not like 'ECONOMY ANODIZED%' and p_size in (35, 29, 23, 16, 49, 9, 7, 5) 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=416437.24..416506.71 rows=27788 width=40) Sort Key: (count(DISTINCT partsupp.ps_suppkey)), part.p_brand, part.p_type, part.p_size -> GroupAggregate (cost=406698.36..414386.19 rows=27788 width=40) Group Key: part.p_brand, part.p_type, part.p_size -> Sort (cost=406698.36..408180.35 rows=592796 width=40) Sort Key: part.p_brand, part.p_type, part.p_size -> Hash Join (cost=57971.63..349857.59 rows=592796 width=40) Hash Cond: (partsupp.ps_partkey = part.p_partkey) -> Seq Scan on partsupp (cost=16.23..270974.23 rows=4000000 width=8) Filter: (NOT (hashed SubPlan 1)) SubPlan 1 -> Index Scan using supplier_s_suppkey_idx_like on supplier (cost=0.14..15.98 rows=100 width=4) -> Hash (cost=54250.43..54250.43 rows=296398 width=40) -> Bitmap Heap Scan on part (cost=5735.43..54250.43 rows=296398 width=40) Recheck Cond: (p_size = ANY ('{35,29,23,16,49,9,7,5}'::integer[])) Filter: ((p_brand <> 'Brand#45'::bpchar) AND ((p_type)::text !~~ 'ECONOMY ANODIZED%'::text)) -> Bitmap Index Scan on part_p_size_idx (cost=0.00..5661.33 rows=319720 width=0) Index Cond: (p_size = ANY ('{35,29,23,16,49,9,7,5}'::integer[])) Planning time: 4.994 ms (19 rows) COMMIT; COMMIT