Last active
March 19, 2021 07:51
Star
You must be signed in to star a gist
Example of Migration to create a table with cached results: Materialised view table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MaterialisedView < ActiveRecord::Migration[5.2] | |
def change | |
execute <<-SQL | |
CREATE TABLE mv_complex_query | |
COMMENT 'Updated every 5m by the mv_complex_query event' | |
SELECT * FROM big_table | |
SQL | |
execute <<-SQL | |
CREATE EVENT dump_mv_complex_query | |
ON SCHEDULE EVERY 5 MINUTE | |
DO BEGIN | |
CREATE TABLE mv_complex_query_new | |
COMMENT 'Updated every 5m by the dump_mv_complex_query event' | |
SELECT * FROM big_table; | |
RENAME TABLE | |
mv_complex_query TO mv_complex_query_old, | |
mv_complex_query_new TO mv_complex_query; | |
DROP TABLE IF EXISTS mv_complex_query_old; | |
END | |
SQL | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
big_table
could be any complex query or views