Skip to content

Instantly share code, notes, and snippets.

@varunchitale
Last active October 11, 2018 08:42
Show Gist options
  • Save varunchitale/440027e522f35d17516b86d3d68b8f03 to your computer and use it in GitHub Desktop.
Save varunchitale/440027e522f35d17516b86d3d68b8f03 to your computer and use it in GitHub Desktop.
Materialized View operations
--Create the materialized view
CREATE materialized view <schema_name>.mv_prompts AS
SELECT id, ngram, freq
FROM <schema_name>.prompts
WHERE add_conditions_here
ORDER BY freq DESC;
--Create a search index using GIN
CREATE INDEX prompts_ngram_idx
ON <schema_name>.mv_prompts
USING GIN (to_tsvector('english', ngram));
--Refresh this view when needed
REFRESH MATERIALIZED VIEW <schema_name>.mv_prompts WITH DATA;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment