Commit 00bda324488503251b6a0348938ed39eaba2ffcf
1 parent
4d7446b1
Exists in
master
and in
29 other branches
improving the runtime of migrations
Showing
5 changed files
with
16 additions
and
9 deletions
Show diff stats
db/migrate/20140708121356_index_articles_filtered_fields.rb
1 | 1 | class IndexArticlesFilteredFields < ActiveRecord::Migration |
2 | 2 | def self.up |
3 | 3 | %w[articles article_versions].each do |table| |
4 | - add_index table, [:path] | |
5 | - add_index table, [:path, :profile_id] | |
4 | + add_index table.to_sym, [:path] | |
5 | + add_index table.to_sym, [:path, :profile_id] | |
6 | 6 | end |
7 | 7 | add_index :articles, [:type] |
8 | 8 | add_index :articles, [:type, :parent_id] | ... | ... |
db/migrate/20140709212646_add_spam_comments_counter_cache_to_articles.rb
... | ... | @@ -2,7 +2,10 @@ class AddSpamCommentsCounterCacheToArticles < ActiveRecord::Migration |
2 | 2 | def self.up |
3 | 3 | add_column :articles, :spam_comments_count, :integer, :default => 0 |
4 | 4 | add_column :article_versions, :spam_comments_count, :integer, :default => 0 |
5 | - execute "update articles set spam_comments_count = (select count(*) from comments where comments.source_id = articles.id and comments.source_type = 'Article' and comments.spam = 't');" | |
5 | + | |
6 | + execute("SELECT comments.source_id as source_id, count(comments.id) as comments_count FROM comments LEFT OUTER JOIN articles ON articles.id = source_id WHERE comments.source_type = 'Article' AND comments.spam = 't' GROUP BY comments.source_id;").each do |data| | |
7 | + execute("UPDATE articles SET spam_comments_count = '#{data['comments_count']}' WHERE id = #{data['source_id']}") | |
8 | + end | |
6 | 9 | end |
7 | 10 | |
8 | 11 | def self.down | ... | ... |
db/migrate/20140709224246_create_real_relation_between_article_and_author.rb
... | ... | @@ -4,8 +4,10 @@ class CreateRealRelationBetweenArticleAndAuthor < ActiveRecord::Migration |
4 | 4 | add_column :article_versions, :author_id, :integer |
5 | 5 | |
6 | 6 | # Set article's author as the first version's last_changed_by_id. |
7 | - execute "update articles set author_id = (select article_versions.last_changed_by_id from article_versions where article_versions.article_id = articles.id and article_versions.version = 1 limit 1)" | |
8 | - end | |
7 | + execute("UPDATE article_versions SET author_id = last_changed_by_id") | |
8 | + | |
9 | + execute("UPDATE articles SET author_id = article_versions.author_id FROM article_versions WHERE article_versions.article_id = articles.id AND article_versions.version = 1") | |
10 | + end | |
9 | 11 | |
10 | 12 | def self.down |
11 | 13 | remove_column :articles, :author_id | ... | ... |
db/migrate/20140724180943_add_index_to_blog_posts_sort.rb
1 | 1 | class AddIndexToBlogPostsSort < ActiveRecord::Migration |
2 | 2 | def self.up |
3 | 3 | %w[articles article_versions].each do |table| |
4 | - add_index table, [:published_at, :id] | |
4 | + add_index table.to_sym, [:published_at, :id] | |
5 | 5 | end |
6 | 6 | end |
7 | 7 | |
8 | 8 | def self.down |
9 | 9 | %w[articles article_versions].each do |table| |
10 | - remove_index table, [:published_at, :id] | |
10 | + remove_index table.to_sym, [:published_at, :id] | |
11 | 11 | end |
12 | 12 | end |
13 | 13 | end | ... | ... |
db/schema.rb
1 | -# This file is auto-generated from the current state of the database. Instead of editing this file, | |
1 | +# This file is auto-generated from the current state of the database. Instead of editing this file, | |
2 | 2 | # please use the migrations feature of Active Record to incrementally modify your database, and |
3 | 3 | # then regenerate this schema definition. |
4 | 4 | # |
... | ... | @@ -9,7 +9,7 @@ |
9 | 9 | # |
10 | 10 | # It's strongly recommended to check this file into your version control system. |
11 | 11 | |
12 | -ActiveRecord::Schema.define(:version => 20140709224246) do | |
12 | +ActiveRecord::Schema.define(:version => 20140724180943) do | |
13 | 13 | |
14 | 14 | create_table "abuse_reports", :force => true do |t| |
15 | 15 | t.integer "reporter_id" |
... | ... | @@ -100,6 +100,7 @@ ActiveRecord::Schema.define(:version => 20140709224246) do |
100 | 100 | add_index "article_versions", ["article_id"], :name => "index_article_versions_on_article_id" |
101 | 101 | add_index "article_versions", ["path", "profile_id"], :name => "index_article_versions_on_path_and_profile_id" |
102 | 102 | add_index "article_versions", ["path"], :name => "index_article_versions_on_path" |
103 | + add_index "article_versions", ["published_at", "id"], :name => "index_article_versions_on_published_at_and_id" | |
103 | 104 | |
104 | 105 | create_table "articles", :force => true do |t| |
105 | 106 | t.string "name" |
... | ... | @@ -154,6 +155,7 @@ ActiveRecord::Schema.define(:version => 20140709224246) do |
154 | 155 | add_index "articles", ["path", "profile_id"], :name => "index_articles_on_path_and_profile_id" |
155 | 156 | add_index "articles", ["path"], :name => "index_articles_on_path" |
156 | 157 | add_index "articles", ["profile_id"], :name => "index_articles_on_profile_id" |
158 | + add_index "articles", ["published_at", "id"], :name => "index_articles_on_published_at_and_id" | |
157 | 159 | add_index "articles", ["slug"], :name => "index_articles_on_slug" |
158 | 160 | add_index "articles", ["translation_of_id"], :name => "index_articles_on_translation_of_id" |
159 | 161 | add_index "articles", ["type", "parent_id"], :name => "index_articles_on_type_and_parent_id" | ... | ... |