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 = 'VIETNAM' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4832812.89..4832812.90 rows=1 width=26) (actual time=184473.759..184473.787 rows=100 loops=1) Buffers: shared hit=9446930 read=2089616 dirtied=2128 written=4182, temp read=161676 written=161670 -> Sort (cost=4832812.89..4832812.90 rows=1 width=26) (actual time=184473.754..184473.766 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9446930 read=2089616 dirtied=2128 written=4182, temp read=161676 written=161670 -> HashAggregate (cost=4832812.87..4832812.88 rows=1 width=26) (actual time=184466.222..184468.817 rows=3987 loops=1) Buffers: shared hit=9446922 read=2089616 dirtied=2128 written=4182, temp read=161676 written=161670 -> Nested Loop Anti Join (cost=2670361.41..4832812.87 rows=1 width=26) (actual time=54388.111..184310.785 rows=39316 loops=1) Buffers: shared hit=9446922 read=2089616 dirtied=2128 written=4182, temp read=161676 written=161670 -> Nested Loop (cost=2670361.41..4832797.82 rows=1 width=34) (actual time=54374.761..157201.485 rows=704527 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6521618 read=1476064 dirtied=2128 written=2458, temp read=161676 written=161670 -> Hash Semi Join (cost=2670361.41..4832795.85 rows=1 width=38) (actual time=54373.509..131650.213 rows=1460504 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378572 Buffers: shared hit=756240 read=1391335 dirtied=2128 written=2208, temp read=161676 written=161670 -> Hash Join (cost=2441.58..1913916.83 rows=805397 width=34) (actual time=41.101..63320.544 rows=1514310 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=370394 read=704325 written=74 -> Seq Scan on lineitem l1 (cost=0.00..1827915.35 rows=20134916 width=8) (actual time=0.031..42695.092 rows=37966100 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22079210 Buffers: shared hit=368545 read=704311 written=71 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=40.929..40.929 rows=3987 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1846 read=14 written=3 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.052..38.750 rows=3987 loops=1) Buffers: shared hit=1846 read=14 written=3 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.017..0.025 rows=1 loops=1) Filter: (n_name = 'VIETNAM'::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=2.027..37.105 rows=3987 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1845 read=14 written=3 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.655..1.655 rows=3987 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 written=3 -> Hash (cost=1676903.48..1676903.48 rows=60404748 width=8) (actual time=54294.325..54294.325 rows=60045310 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586609kB Buffers: shared hit=385846 read=687010 dirtied=2128 written=2134, temp written=153904 -> Seq Scan on lineitem l2 (cost=0.00..1676903.48 rows=60404748 width=8) (actual time=0.042..28094.860 rows=60045310 loops=1) Buffers: shared hit=385846 read=687010 dirtied=2128 written=2134 -> Index Scan using pk_orders on orders (cost=0.00..1.96 rows=1 width=4) (actual time=0.016..0.016 rows=0 loops=1460504) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5765378 read=84729 written=250 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.50 rows=5 width=8) (actual time=0.037..0.037 rows=1 loops=704527) 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=2925304 read=613552 written=1724 Total runtime: 184475.715 ms (56 rows) COMMIT; COMMIT