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=4852002.73..4852002.73 rows=1 width=26) (actual time=100558.656..100558.676 rows=100 loops=1) Buffers: shared hit=9348628 read=2193434 written=321, temp read=161516 written=161510 -> Sort (cost=4852002.73..4852002.73 rows=1 width=26) (actual time=100558.652..100558.659 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9348628 read=2193434 written=321, temp read=161516 written=161510 -> HashAggregate (cost=4852002.71..4852002.72 rows=1 width=26) (actual time=100553.611..100555.532 rows=3988 loops=1) Buffers: shared hit=9348620 read=2193434 written=321, temp read=161516 written=161510 -> Nested Loop Anti Join (cost=2680975.39..4852002.70 rows=1 width=26) (actual time=34176.018..100451.047 rows=39939 loops=1) Buffers: shared hit=9348620 read=2193434 written=321, temp read=161516 written=161510 -> Nested Loop (cost=2680975.39..4851987.56 rows=1 width=34) (actual time=34172.205..89434.000 rows=705285 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6380216 read=1619201 written=104, temp read=161516 written=161510 -> Hash Semi Join (cost=2680975.39..4851985.57 rows=1 width=38) (actual time=34171.423..77246.500 rows=1458860 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 377710 Buffers: shared hit=885243 read=1270852 written=2, temp read=161516 written=161510 -> Hash Join (cost=2441.58..1921510.64 rows=807707 width=34) (actual time=25.470..31795.057 rows=1512878 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=596452 read=482523 written=2 -> Seq Scan on lineitem l1 (cost=0.00..1835185.45 rows=20215079 width=8) (actual time=0.033..22157.790 rows=37927978 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056848 Buffers: shared hit=594611 read=482509 written=2 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=25.383..25.383 rows=3988 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1838 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.308..23.923 rows=3988 loops=1) Buffers: shared hit=1838 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.009..0.014 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=1.294..23.006 rows=3988 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1837 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=0.991..0.991 rows=3988 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1683572.36..1683572.36 rows=60645236 width=8) (actual time=34128.062..34128.062 rows=59984826 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 585983kB Buffers: shared hit=288791 read=788329, temp written=153751 -> Seq Scan on lineitem l2 (cost=0.00..1683572.36 rows=60645236 width=8) (actual time=0.020..16730.352 rows=59984826 loops=1) Buffers: shared hit=288791 read=788329 -> Index Scan using pk_orders on orders (cost=0.00..1.97 rows=1 width=4) (actual time=0.008..0.008 rows=0 loops=1458860) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5494973 read=348349 written=102 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.55 rows=5 width=8) (actual time=0.015..0.015 rows=1 loops=705285) 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=2968404 read=574233 written=217 Total runtime: 100560.037 ms (56 rows) COMMIT; COMMIT