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=4851961.56..4851961.56 rows=1 width=26) (actual time=137297.886..137297.906 rows=100 loops=1) Buffers: shared hit=10204088 read=1392198 dirtied=1175 written=18203, temp read=161729 written=161723 -> Sort (cost=4851961.56..4851961.56 rows=1 width=26) (actual time=137297.882..137297.890 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10204088 read=1392198 dirtied=1175 written=18203, temp read=161729 written=161723 -> HashAggregate (cost=4851961.54..4851961.55 rows=1 width=26) (actual time=137291.461..137293.791 rows=4009 loops=1) Buffers: shared hit=10204080 read=1392198 dirtied=1175 written=18203, temp read=161729 written=161723 -> Nested Loop Anti Join (cost=2680954.36..4851961.53 rows=1 width=26) (actual time=46730.531..137137.236 rows=39471 loops=1) Buffers: shared hit=10204080 read=1392198 dirtied=1175 written=18203, temp read=161729 written=161723 -> Nested Loop (cost=2680954.36..4851946.39 rows=1 width=34) (actual time=46730.168..119326.948 rows=708122 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7285419 read=754187 dirtied=1070 written=19, temp read=161729 written=161723 -> Hash Semi Join (cost=2680954.36..4851944.40 rows=1 width=38) (actual time=46729.707..103473.770 rows=1468912 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 382616 Buffers: shared hit=1411770 read=744329 dirtied=1068 written=6, temp read=161729 written=161723 -> Hash Join (cost=2441.58..1921497.99 rows=807543 width=34) (actual time=35.437..42779.749 rows=1523692 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=608818 read=470161 -> Seq Scan on lineitem l1 (cost=0.00..1835175.44 rows=20214812 width=8) (actual time=0.061..28928.841 rows=37965800 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22079072 Buffers: shared hit=606973 read=470147 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=35.240..35.240 rows=4010 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 251kB Buffers: shared hit=1842 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.832..33.060 rows=4010 loops=1) Buffers: shared hit=1842 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.015..0.020 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=1.810..31.384 rows=4010 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1841 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.401..1.401 rows=4010 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1683564.35..1683564.35 rows=60644435 width=8) (actual time=46669.777..46669.777 rows=60044872 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586575kB Buffers: shared hit=802952 read=274168 dirtied=1068 written=6, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1683564.35 rows=60644435 width=8) (actual time=0.021..25126.789 rows=60044872 loops=1) Buffers: shared hit=802952 read=274168 dirtied=1068 written=6 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.97 rows=1 width=4) (actual time=0.010..0.010 rows=0 loops=1468912) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5873649 read=9858 dirtied=2 written=13 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.55 rows=5 width=8) (actual time=0.024..0.024 rows=1 loops=708122) 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=2918661 read=638011 dirtied=105 written=18184 Total runtime: 137307.032 ms (56 rows) COMMIT; COMMIT