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 = 'ROMANIA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4852009.34..4852009.34 rows=1 width=26) (actual time=178204.170..178204.199 rows=100 loops=1) Buffers: shared hit=9765449 read=1875192 dirtied=1031 written=19675, temp read=161748 written=161742 -> Sort (cost=4852009.34..4852009.34 rows=1 width=26) (actual time=178204.167..178204.181 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9765449 read=1875192 dirtied=1031 written=19675, temp read=161748 written=161742 -> HashAggregate (cost=4852009.32..4852009.33 rows=1 width=26) (actual time=178196.708..178199.192 rows=4029 loops=1) Buffers: shared hit=9765441 read=1875192 dirtied=1031 written=19675, temp read=161748 written=161742 -> Nested Loop Anti Join (cost=2680964.09..4852009.31 rows=1 width=26) (actual time=63544.408..178042.552 rows=39732 loops=1) Buffers: shared hit=9765441 read=1875192 dirtied=1031 written=19675, temp read=161748 written=161742 -> Nested Loop (cost=2680964.09..4851994.17 rows=1 width=34) (actual time=63539.702..153899.251 rows=712193 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6810514 read=1252748 dirtied=1031 written=2242, temp read=161748 written=161742 -> Hash Semi Join (cost=2680964.09..4851992.18 rows=1 width=38) (actual time=63539.633..132239.310 rows=1474773 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 381530 Buffers: shared hit=922430 read=1233669 dirtied=971 written=850, temp read=161748 written=161742 -> Hash Join (cost=2441.58..1921513.87 rows=808598 width=34) (actual time=56.315..54083.134 rows=1529041 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=445877 read=633102 written=3 -> Seq Scan on lineitem l1 (cost=0.00..1835180.29 rows=20214941 width=8) (actual time=0.044..37336.231 rows=37965800 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22079072 Buffers: shared hit=444032 read=633088 written=3 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=56.201..56.201 rows=4029 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 252kB Buffers: shared hit=1842 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.689..54.021 rows=4029 loops=1) Buffers: shared hit=1842 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.011..0.019 rows=1 loops=1) Filter: (n_name = 'ROMANIA'::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.671..52.774 rows=4029 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=2.269..2.269 rows=4029 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1683568.23..1683568.23 rows=60644823 width=8) (actual time=63459.933..63459.933 rows=60044872 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586575kB Buffers: shared hit=476553 read=600567 dirtied=971 written=847, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1683568.23 rows=60644823 width=8) (actual time=0.049..36534.005 rows=60044872 loops=1) Buffers: shared hit=476553 read=600567 dirtied=971 written=847 -> Index Scan using pk_orders on orders (cost=0.00..1.97 rows=1 width=4) (actual time=0.013..0.014 rows=0 loops=1474773) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5888084 read=19079 dirtied=60 written=1392 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.55 rows=5 width=8) (actual time=0.033..0.033 rows=1 loops=712193) 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=2954927 read=622444 written=17433 Total runtime: 178205.761 ms (56 rows) COMMIT; COMMIT