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 = 'SAUDI ARABIA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4813674.91..4813674.92 rows=1 width=26) (actual time=257035.302..257035.328 rows=100 loops=1) Buffers: shared hit=9877881 read=1693564 written=1129, temp read=161566 written=161560 -> Sort (cost=4813674.91..4813674.92 rows=1 width=26) (actual time=257035.297..257035.307 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9877881 read=1693564 written=1129, temp read=161566 written=161560 -> HashAggregate (cost=4813674.89..4813674.90 rows=1 width=26) (actual time=257028.889..257031.263 rows=4009 loops=1) Buffers: shared hit=9877873 read=1693564 written=1129, temp read=161566 written=161560 -> Nested Loop Anti Join (cost=2659795.19..4813674.89 rows=1 width=26) (actual time=80245.891..256838.112 rows=39437 loops=1) Buffers: shared hit=9877873 read=1693564 written=1129, temp read=161566 written=161560 -> Nested Loop (cost=2659795.19..4813659.94 rows=1 width=34) (actual time=80234.853..219608.621 rows=707551 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6818512 read=1199102 written=172, temp read=161566 written=161560 -> Hash Semi Join (cost=2659795.19..4813657.98 rows=1 width=38) (actual time=80234.594..178118.561 rows=1467664 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 382183 Buffers: shared hit=947055 read=1192020 written=144, temp read=161566 written=161560 -> Hash Join (cost=2441.58..1906344.54 rows=801942 width=34) (actual time=101.364..80088.097 rows=1522322 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=426405 read=644062 written=128 -> Seq Scan on lineitem l1 (cost=0.00..1820676.68 rows=20055165 width=8) (actual time=0.033..56905.619 rows=37928885 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056768 Buffers: shared hit=424558 read=644050 written=128 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=101.264..101.264 rows=4010 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 251kB Buffers: shared hit=1844 read=12 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=3.186..98.233 rows=4010 loops=1) Buffers: shared hit=1844 read=12 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.015..0.026 rows=1 loops=1) Filter: (n_name = 'SAUDI ARABIA'::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=3.162..95.735 rows=4010 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1843 read=12 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=2.816..2.816 rows=4010 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=2 read=12 -> Hash (cost=1670262.94..1670262.94 rows=60165494 width=8) (actual time=79752.037..79752.037 rows=59985653 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586043kB Buffers: shared hit=520650 read=547958 written=16, temp written=153749 -> Seq Scan on lineitem l2 (cost=0.00..1670262.94 rows=60165494 width=8) (actual time=0.069..49081.987 rows=59985653 loops=1) Buffers: shared hit=520650 read=547958 written=16 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.027..0.027 rows=0 loops=1467664) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5871457 read=7082 written=28 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.45 rows=5 width=8) (actual time=0.051..0.051 rows=1 loops=707551) 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=3059361 read=494462 written=957 Total runtime: 257036.930 ms (56 rows) COMMIT; COMMIT