Document refreshing concurrently
drnic opened this issue · comments
From @andyatkinson in #2
Also, re: indexes, passing on something else useful in case you didn't already know: refreshing a materialized view ideally is done "concurrently", so that other queries using it can continue to run while the results are replaced. Concurrent refreshes do require one unique index exists on the materialized view:
For here: https://github.com/drnic/searchcraft/blob/develop/lib/searchcraft/model.rb#L44
Docs on the concurrently keyword and unique index requirement: https://www.postgresql.org/docs/current/sql-refreshmaterializedview.html
SearchCraft does support concurrent refreshes; and there is a technique to add a new unique ID column; but these are not yet documented.
In brief:
- Each MV currently also gets a bonus sequence, even if it doesn't use it. See
Builder#create_sequence!
andBuilder#view_id_sequence_name
- To add an
id
column to your MV using Arel, try something likeArel::Nodes::SqlLiteral.new("nextval('#{view_id_sequence_name}') AS id")
- which could of course become a niceBuilder
helper in future.