BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select s_name, count(*) as numwait from supplier, lineitem l1, orders, nation where s_suppkey = l1.l_suppkey and o_orderkey = l1.l_orderkey and o_orderstatus = 'F' and l1.l_receiptdate > l1.l_commitdate and exists ( select * from lineitem l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey ) and not exists ( select * from lineitem l3 where l3.l_orderkey = l1.l_orderkey and l3.l_suppkey <> l1.l_suppkey and l3.l_receiptdate > l3.l_commitdate ) and s_nationkey = n_nationkey and n_name = 'IRAQ' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4861613.79..4861613.80 rows=1 width=26) (actual time=117449.647..117449.666 rows=100 loops=1) Buffers: shared hit=10363287 read=1436075 written=47, temp read=161724 written=161718 -> Sort (cost=4861613.79..4861613.80 rows=1 width=26) (actual time=117449.643..117449.652 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10363287 read=1436075 written=47, temp read=161724 written=161718 -> HashAggregate (cost=4861613.77..4861613.78 rows=1 width=26) (actual time=117445.040..117446.920 rows=4095 loops=1) Buffers: shared hit=10363279 read=1436075 written=47, temp read=161724 written=161718 -> Nested Loop Anti Join (cost=2686282.87..4861613.77 rows=1 width=26) (actual time=37665.701..117317.594 rows=40936 loops=1) Buffers: shared hit=10363279 read=1436075 written=47, temp read=161724 written=161718 -> Nested Loop (cost=2686282.87..4861598.58 rows=1 width=34) (actual time=37659.978..104185.983 rows=723982 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7290063 read=872794 written=28, temp read=161724 written=161718 -> Hash Semi Join (cost=2686282.87..4861596.58 rows=1 width=38) (actual time=37659.906..91210.522 rows=1498601 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 388776 Buffers: shared hit=1392265 read=768113, temp read=161724 written=161718 -> Hash Join (cost=2441.58..1925317.31 rows=809328 width=34) (actual time=17.729..37677.600 rows=1554216 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=593952 read=487164 -> Seq Scan on lineitem l1 (cost=0.00..1838826.05 rows=20255041 width=8) (actual time=0.037..25062.489 rows=37928434 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22057104 Buffers: shared hit=592112 read=487150 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=17.635..17.635 rows=4095 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 256kB Buffers: shared hit=1837 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.312..16.264 rows=4095 loops=1) Buffers: shared hit=1837 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.008..0.015 rows=1 loops=1) Filter: (n_name = 'IRAQ'::bpchar) Rows Removed by Filter: 24 Buffers: shared hit=1 -> Bitmap Heap Scan on supplier (cost=79.26..2350.26 rows=4000 width=34) (actual time=1.300..15.350 rows=4095 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1836 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=0.997..0.997 rows=4095 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1686913.24..1686913.24 rows=60765124 width=8) (actual time=37456.587..37456.587 rows=59985538 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 585996kB Buffers: shared hit=798313 read=280949, temp written=153752 -> Seq Scan on lineitem l2 (cost=0.00..1686913.24 rows=60765124 width=8) (actual time=0.032..18696.744 rows=59985538 loops=1) Buffers: shared hit=798313 read=280949 -> Index Scan using pk_orders on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.008..0.008 rows=0 loops=1498601) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5897798 read=104681 written=28 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.57 rows=5 width=8) (actual time=0.017..0.017 rows=1 loops=723982) Index Cond: (l_orderkey = l1.l_orderkey) Filter: ((l_receiptdate > l_commitdate) AND (l_suppkey <> l1.l_suppkey)) Rows Removed by Filter: 1 Buffers: shared hit=3073216 read=563281 written=19 Total runtime: 117450.663 ms (56 rows) COMMIT; COMMIT