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 = 'IRAN' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4856779.29..4856779.29 rows=1 width=26) (actual time=125055.378..125055.400 rows=100 loops=1) Buffers: shared hit=9571923 read=2038389 dirtied=861 written=6009, temp read=161739 written=161733 -> Sort (cost=4856779.29..4856779.29 rows=1 width=26) (actual time=125055.373..125055.382 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9571923 read=2038389 dirtied=861 written=6009, temp read=161739 written=161733 -> HashAggregate (cost=4856779.27..4856779.28 rows=1 width=26) (actual time=125050.473..125052.410 rows=4023 loops=1) Buffers: shared hit=9571915 read=2038389 dirtied=861 written=6009, temp read=161739 written=161733 -> Nested Loop Anti Join (cost=2683612.31..4856779.26 rows=1 width=26) (actual time=39375.073..124915.777 rows=39957 loops=1) Buffers: shared hit=9571915 read=2038389 dirtied=861 written=6009, temp read=161739 written=161733 -> Nested Loop (cost=2683612.31..4856764.09 rows=1 width=34) (actual time=39373.974..108591.586 rows=709694 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6688309 read=1357383 dirtied=700 written=795, temp read=161739 written=161733 -> Hash Semi Join (cost=2683612.31..4856762.11 rows=1 width=38) (actual time=39373.927..94214.165 rows=1469873 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 381786 Buffers: shared hit=812864 read=1345384 dirtied=699 written=768, temp read=161739 written=161733 -> Hash Join (cost=2441.58..1923403.80 rows=808522 width=34) (actual time=31.068..40624.318 rows=1524555 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=430204 read=649856 written=147 -> Seq Scan on lineitem l1 (cost=0.00..1836996.19 rows=20234885 width=8) (actual time=0.042..27671.341 rows=37966174 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078820 Buffers: shared hit=428346 read=649842 written=147 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=30.954..30.954 rows=4023 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 252kB Buffers: shared hit=1855 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.810..28.360 rows=4023 loops=1) Buffers: shared hit=1855 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.014 rows=1 loops=1) Filter: (n_name = 'IRAN'::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.797..26.684 rows=4023 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1854 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.392..1.392 rows=4023 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1685234.55..1685234.55 rows=60704655 width=8) (actual time=39318.850..39318.850 rows=60044994 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586568kB Buffers: shared hit=382660 read=695528 dirtied=699 written=621, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1685234.55 rows=60704655 width=8) (actual time=0.056..20278.394 rows=60044994 loops=1) Buffers: shared hit=382660 read=695528 dirtied=699 written=621 -> Index Scan using pk_orders on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.009..0.009 rows=0 loops=1469873) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5875445 read=11999 dirtied=1 written=27 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.56 rows=5 width=8) (actual time=0.022..0.022 rows=1 loops=709694) 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=2883606 read=681006 dirtied=161 written=5214 Total runtime: 125056.787 ms (56 rows) COMMIT; COMMIT