HashAggregate
(cost=60905.73..60935.73 rows=3000 width=4) (actual
time=3366.165..3367.354 rows=3492 loops=1)
-> Nested Loop (cost=17546.26..60898.23 rows=3000
width=4) (actual time=438.332..3357.918 rows=13037
loops=1)
-> Hash Join (cost=17546.26..25100.94 rows=98
width=4) (actual time=408.554..415.767 rows=2092 loops=1)
Hash Cond: (a2.context_key =
collection_data.collection_context_key)
-> Index Only Scan using
virtual_ancestors_pkey on virtual_ancestors a2
(cost=0.00..233.32 rows=270 width=4) (actual
time=8.532..10.703 rows=1960 loops=1)
-> Hash (cost=10015.56..10015.56
rows=602456 width=8) (actual time=399.708..399.708
rows=602570 loops=1)
Buckets: 65536 Batches: 1 Memory Usage:
23538kB
-> Index Only Scan using virtual_ancestors_pkey
on virtual_ancestors a1 (cost=0.00..360.70 rows=458
width=8) (actual time=1.339..1.403 rows=6 loops=2092)
Index Cond: (ancestor_key =
collection_data.context_key)
Table "public.virtual_ancestors"
Column | Type | Modifiers
--------------+----------+-----------
ancestor_key
| integer | not null
context_key | integer | not null
degree
| smallint | not null
Indexes:
"virtual_ancestors_pkey" PRIMARY KEY, btree
(ancestor_key, context_key)
"virtual_context_key_idx" btree (context_key)
Foreign-key constraints:
"virtual_ancestors_ancestor_key_fkey" FOREIGN KEY
(ancestor_key) REFERENCES contexts(context_key)
"virtual_ancestors_context_key_fkey" FOREIGN KEY
(context_key) REFERENCES contexts(context_key)
Table "public.collection_data"
Column | Type
| Modifiers
------------------------+----------------------+-----------
collection_context_key | integer |
not null
context_key | integer
| not null
type | character
varying(1) | not null
source | character
varying(1) | not null
Indexes:
"collection_data_context_key_idx" btree
(context_key)
"collection_data_context_key_index" btree
(collection_context_key) CLUSTER
Foreign-key constraints:
"collection_data_collection_context_key_fkey"
FOREIGN KEY (collection_context_key) REFERENCES
contexts(context_key) ON DELETE CASCADE
"collection_data_context_key_fkey" FOREIGN KEY
(context_key) REFERENCES contexts(context_key) ON
DELETE CASCADE
Can
anyone suggest a way that we can get postgres to use the
collection_data_context_key_index properly? I thought
that it might be related to the fact that
collection_data_context_key_index is a CLUSTERED index,
but we did some basic experimentation that seems to
indicate otherwise, i.e. the bad plan persists despite
re-clustering the index.
We
are using PostgreSQL 9.2.5 on x86_64-unknown-linux-gnu,
compiled by gcc (Debian 4.4.5-8) 4.4.5, 64-bit
Interestingly,
on an instance running PostgreSQL 9.2.4 on
x86_64-unknown-linux-gnu, compiled by gcc (Debian
4.4.5-8) 4.4.5, 64-bit where I copied the 2 tables over
to a temporary database, the plan comes out differently:
HashAggregate
(cost=39692.03..39739.98 rows=4795 width=4) (actual
time=73.285..75.141 rows=3486 loops=1)
Buffers: shared
hit=22458
-> Nested
Loop (cost=0.00..39680.05 rows=4795 width=4)
(actual time=0.077..63.116 rows=13007 loops=1)
Buffers:
shared hit=22458
->
Nested Loop (cost=0.00..32823.38 rows=164 width=4)
(actual time=0.056..17.685 rows=2084 loops=1)
Buffers: shared hit=7529
->
Index Only Scan using virtual_ancestors_pkey on
virtual_ancestors a2 (cost=0.00..1220.85 rows=396
width=4) (actual time=0.025..2.732 rows=1954
loops=1)
Index Cond: (ancestor_key = 1072173)
Heap Fetches: 1954
Buffers: shared hit=1397
########
Note the index scan here - this is what it SHOULD be
doing ##############
-> Index Scan using
collection_data_context_key_index on
collection_data (cost=0.00..79.24 rows=56
width=8) (actual time=0.004..0.005 rows=1
loops=1954)
Index Cond: (collection_context_key =
a2.context_key)
Buffers: shared hit=6132
-> Index
Only Scan using virtual_ancestors_pkey on
virtual_ancestors a1 (cost=0.00..35.40 rows=641
width=8) (actual time=0.007..0.015 rows=6
loops=2084)
Index
Cond: (ancestor_key = collection_data.context_key)
Heap
Fetches: 13007
Buffers: shared hit=14929
Total runtime:
76.431 ms
Why
can't I get the Postgres 9.2.5 instance to use the
optimal plan?
Thanks
in advance!