Skip to content

Add indices on history table foreign keys#32

Merged
multigl merged 4 commits intomasterfrom
index-history-foreign-keys
Aug 11, 2017
Merged

Add indices on history table foreign keys#32
multigl merged 4 commits intomasterfrom
index-history-foreign-keys

Conversation

@pauljz
Copy link
Copy Markdown
Member

@pauljz pauljz commented Aug 9, 2017

This is only relevant for UUID columns, but results in some expensive joins. The way we build the GiST indices on history tables means we don't have any implicit index on the entity foreign key the way we do with the clock table. This adds an explicit index so you don't have to remember to cast your UUID primary keys to text when joining to column history.


Without casts. Note that the index scan only uses the vclock portion of the exclusion constraint and triggers an expensive join filter:

explain select * from my_table as t
left join trg_history.my_table_clock as c
  on t.id = c.entity_id
left join trg_history.my_table_history_my_col as hc
  on hc.entity_id = t.id and hc.vclock @> c.tick
Limit  (cost=0.70..2903.08 rows=100 width=1944)
  ->  Nested Loop Left Join  (cost=0.70..9313203.03 rows=320882 width=1944)
        Join Filter: (hs.entity_id = pa.id)
        ->  Index Scan using my_table_history_my_col_excl_vclock on my_table_history_my_col hc  (cost=0.28..16.97 rows=949 width=77)
              Index Cond: (vclock @> c.tick)

With casts! If we join on the UUIDs casted to text, we're back to being efficient:

explain select * from my_table as t
left join trg_history.my_table_clock as c
  on t.id = c.entity_id
left join trg_history.my_table_history_my_col as hc
  on hc.entity_id::text = t.id::text and hc.vclock @> c.tick
Limit  (cost=0.71..127.08 rows=100 width=1944)
  ->  Nested Loop Left Join  (cost=0.71..405509.70 rows=320882 width=1944)
        ->  Index Scan using my_table_history_my_col_excl_vclock on my_table_history_my_col hc  (cost=0.29..1.06 rows=1 width=77)
              Index Cond: (((entity_id)::text = (pa.id)::text) AND (vclock @> c.tick))

@multigl
Copy link
Copy Markdown
Contributor

multigl commented Aug 11, 2017

LGTM

@multigl
Copy link
Copy Markdown
Contributor

multigl commented Aug 11, 2017

tests run pretty fast here

@multigl multigl merged commit 246f0f5 into master Aug 11, 2017
@multigl multigl deleted the index-history-foreign-keys branch August 11, 2017 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants