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 = 'UNITED KINGDOM' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4866410.24..4866410.25 rows=1 width=26) (actual time=162122.821..162122.841 rows=100 loops=1) Buffers: shared hit=9932839 read=1569494 dirtied=49 written=698, temp read=161643 written=161637 -> Sort (cost=4866410.24..4866410.25 rows=1 width=26) (actual time=162122.817..162122.824 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9932839 read=1569494 dirtied=49 written=698, temp read=161643 written=161637 -> HashAggregate (cost=4866410.22..4866410.23 rows=1 width=26) (actual time=162117.831..162119.633 rows=3973 loops=1) Buffers: shared hit=9932831 read=1569494 dirtied=49 written=698, temp read=161643 written=161637 -> Nested Loop Anti Join (cost=2688918.71..4866410.22 rows=1 width=26) (actual time=56746.128..161968.314 rows=39566 loops=1) Buffers: shared hit=9932831 read=1569494 dirtied=49 written=698, temp read=161643 written=161637 -> Nested Loop (cost=2688918.71..4866395.00 rows=1 width=34) (actual time=56733.551..147031.941 rows=700698 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6673915 read=1309645 dirtied=40 written=17, temp read=161643 written=161637 -> Hash Semi Join (cost=2688918.71..4866393.01 rows=1 width=38) (actual time=56733.381..129306.865 rows=1453292 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 376675 Buffers: shared hit=873247 read=1289268 written=2, temp read=161643 written=161637 -> Hash Join (cost=2441.58..1927212.87 rows=810999 width=34) (actual time=31.922..54672.469 rows=1506996 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=554376 read=527820 written=2 -> Seq Scan on lineitem l1 (cost=0.00..1840630.19 rows=20274965 width=8) (actual time=0.025..35613.386 rows=37966096 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078937 Buffers: shared hit=552511 read=527808 written=2 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=31.853..31.853 rows=3973 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 249kB Buffers: shared hit=1862 read=12 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.189..29.892 rows=3973 loops=1) Buffers: shared hit=1862 read=12 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.009..0.013 rows=1 loops=1) Filter: (n_name = 'UNITED KINGDOM'::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.175..28.558 rows=3973 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1861 read=12 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=0.893..0.893 rows=3973 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=2 read=12 -> Hash (cost=1688567.95..1688567.95 rows=60824895 width=8) (actual time=56681.358..56681.358 rows=60045033 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586575kB Buffers: shared hit=318871 read=761448, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1688567.95 rows=60824895 width=8) (actual time=0.057..27590.161 rows=60045033 loops=1) Buffers: shared hit=318871 read=761448 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.011..0.011 rows=0 loops=1453292) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5800668 read=20377 dirtied=40 written=15 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.58 rows=5 width=8) (actual time=0.020..0.020 rows=1 loops=700698) 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=3258916 read=259849 dirtied=9 written=681 Total runtime: 162124.037 ms (56 rows) COMMIT; COMMIT