OK here are the explain analyse results:
Aggregate (cost=51432.61..51432.61 rows=1 width=12) (actual time=12384.43..12384.43
rows=1 loops=1)
-> Merge Join (cost=50838.66..51431.69 rows=367 width=12) (actual
time=12150.42..12383.34 rows=254 loops=1)
-> Sort (cost=12208.53..12208.53 rows=15223 width=6) (actual
time=3579.63..3592.50 rows=17918 loops=1)
-> Seq Scan on u (cost=0.00..11151.01 rows=15223 width=6) (actual
time=0.12..3441.75 rows=17918 loops=1)
-> Sort (cost=38630.13..38630.13 rows=136667 width=6) (actual
time=8569.49..8669.88 rows=140552 loops=1)
-> Seq Scan on d (cost=0.00..25751.95 rows=136667 width=6) (actual
time=0.06..7205.61 rows=140553 loops=1)
Total runtime: 12635.24 msec
EXPLAIN
=> set random_page_cost = .2;
Aggregate (cost=33423.86..33423.86 rows=1 width=12) (actual time=9678.22..9678.22 rows=1
loops=1)
-> Merge Join (cost=3585.41..33422.94 rows=367 width=12) (actual
time=1060.41..9677.13 rows=254 loops=1)
-> Index Scan using d_pkey on d (cost=0.00..29244.50 rows=136667 width=6)
(actual time=0.13..8821.37 rows=140552 loops=1)
-> Sort (cost=3585.41..3585.41 rows=15223 width=6) (actual time=689.15..706.23
rows=17918 loops=1)
-> Index Scan using u_p_key on u (cost=0.00..2527.89 rows=15223 width=6)
(actual time=0.18..550.03 rows=17918 loops=1)
Total runtime: 9681.19 msec
EXPLAIN
=> set random_page_cost = .02;
Aggregate (cost=23244.99..23244.99 rows=1 width=12) (actual time=2724.45..2724.45 rows=1
loops=1)
-> Merge Join (cost=1645.39..23244.08 rows=367 width=12) (actual time=254.97..2723.40
rows=254 loops=1)
-> Sort (cost=1645.39..1645.39 rows=15223 width=6) (actual time=244.75..262.10
rows=17918 loops=1)
-> Index Scan using u_p_key on u (cost=0.00..587.86 rows=15223 width=6)
(actual time=0.19..106.98 rows=17918 loops=1)
-> Index Scan using d_pkey on d (cost=0.00..21005.66 rows=136667 width=6)
(actual time=0.28..2297.81 rows=140552 loops=1)
Total runtime: 2728.00 msec