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 = 'CHINA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4813657.46..4813657.46 rows=1 width=26) (actual time=397465.816..397465.837 rows=100 loops=1) Buffers: shared hit=10200050 read=1332926 dirtied=333 written=9645, temp read=161677 written=161671 -> Sort (cost=4813657.46..4813657.46 rows=1 width=26) (actual time=397465.812..397465.821 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10200050 read=1332926 dirtied=333 written=9645, temp read=161677 written=161671 -> HashAggregate (cost=4813657.44..4813657.45 rows=1 width=26) (actual time=397460.676..397462.683 rows=3988 loops=1) Buffers: shared hit=10200042 read=1332926 dirtied=333 written=9645, temp read=161677 written=161671 -> Nested Loop Anti Join (cost=2659782.62..4813657.43 rows=1 width=26) (actual time=97083.150..397206.670 rows=39961 loops=1) Buffers: shared hit=10200042 read=1332926 dirtied=333 written=9645, temp read=161677 written=161671 -> Nested Loop (cost=2659782.62..4813642.49 rows=1 width=34) (actual time=97082.723..292329.428 rows=705871 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7317696 read=670790 dirtied=169 written=737, temp read=161677 written=161671 -> Hash Semi Join (cost=2659782.62..4813640.53 rows=1 width=38) (actual time=97082.653..211357.304 rows=1460360 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378102 Buffers: shared hit=1538598 read=600473 dirtied=86 written=111, temp read=161677 written=161671 -> Hash Join (cost=2441.58..1906339.30 rows=802064 width=34) (actual time=101.881..99637.100 rows=1514372 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=743515 read=326948 written=31 -> Seq Scan on lineitem l1 (cost=0.00..1820670.80 rows=20055008 width=8) (actual time=0.032..69967.324 rows=37966418 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078961 Buffers: shared hit=741675 read=326933 written=31 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=101.737..101.737 rows=3988 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1837 read=15 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.166..99.058 rows=3988 loops=1) Buffers: shared hit=1837 read=15 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.010..0.017 rows=1 loops=1) Filter: (n_name = 'CHINA'::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.150..97.347 rows=3988 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1836 read=15 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.781..1.781 rows=3988 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1670258.24..1670258.24 rows=60165024 width=8) (actual time=96958.673..96958.673 rows=60045379 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586617kB Buffers: shared hit=795083 read=273525 dirtied=86 written=80, temp written=153903 -> Seq Scan on lineitem l2 (cost=0.00..1670258.24 rows=60165024 width=8) (actual time=0.053..66598.895 rows=60045379 loops=1) Buffers: shared hit=795083 read=273525 dirtied=86 written=80 -> Index Scan using pk_orders on orders (cost=0.00..1.95 rows=1 width=4) (actual time=0.053..0.054 rows=0 loops=1460360) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5779098 read=70317 dirtied=83 written=626 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.45 rows=5 width=8) (actual time=0.147..0.147 rows=1 loops=705871) 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=2882346 read=662136 dirtied=164 written=8908 Total runtime: 397467.297 ms (56 rows) COMMIT; COMMIT