Commit 927d459d92e85daa5cd8c72f790c94fa64f1800a
1 parent
6c7b80b0
Exists in
master
and in
1 other branch
add index to votes on created_at and question_id
this is used for a query in the admin area that lists gets count of votes by question for today
Showing
2 changed files
with
13 additions
and
1 deletions
Show diff stats
db/migrate/20110628160608_add_index_for_created_at_question_id_to_votes.rb
0 → 100644
| @@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
| 1 | +class AddIndexForCreatedAtQuestionIdToVotes < ActiveRecord::Migration | ||
| 2 | + # this is for a query in the admin area | ||
| 3 | + # EXPLAIN SELECT count(*) AS count_all, votes.question_id AS votes_question_id FROM `votes` INNER JOIN `questions` ON `questions`.id = `votes`.question_id WHERE (votes.created_at > '2011-06-01') AND (votes.valid_record = 1) GROUP BY votes.question_id; | ||
| 4 | + def self.up | ||
| 5 | + add_index :votes, [:created_at, :question_id] | ||
| 6 | + end | ||
| 7 | + | ||
| 8 | + def self.down | ||
| 9 | + remove_index :votes, [:created_at, :question_id] | ||
| 10 | + end | ||
| 11 | +end |
db/schema.rb
| @@ -9,7 +9,7 @@ | @@ -9,7 +9,7 @@ | ||
| 9 | # | 9 | # |
| 10 | # It's strongly recommended to check this file into your version control system. | 10 | # It's strongly recommended to check this file into your version control system. |
| 11 | 11 | ||
| 12 | -ActiveRecord::Schema.define(:version => 20110620185325) do | 12 | +ActiveRecord::Schema.define(:version => 20110628160608) do |
| 13 | 13 | ||
| 14 | create_table "appearances", :force => true do |t| | 14 | create_table "appearances", :force => true do |t| |
| 15 | t.integer "voter_id" | 15 | t.integer "voter_id" |
| @@ -250,6 +250,7 @@ ActiveRecord::Schema.define(:version => 20110620185325) do | @@ -250,6 +250,7 @@ ActiveRecord::Schema.define(:version => 20110620185325) do | ||
| 250 | end | 250 | end |
| 251 | 251 | ||
| 252 | add_index "votes", ["choice_id"], :name => "choice_id_idx" | 252 | add_index "votes", ["choice_id"], :name => "choice_id_idx" |
| 253 | + add_index "votes", ["created_at", "question_id"], :name => "index_votes_on_created_at_and_question_id" | ||
| 253 | add_index "votes", ["loser_choice_id"], :name => "loser_choice_id_idx" | 254 | add_index "votes", ["loser_choice_id"], :name => "loser_choice_id_idx" |
| 254 | add_index "votes", ["question_id"], :name => "question_id_idx" | 255 | add_index "votes", ["question_id"], :name => "question_id_idx" |
| 255 | add_index "votes", ["voter_id"], :name => "index_votes_on_voter_id" | 256 | add_index "votes", ["voter_id"], :name => "index_votes_on_voter_id" |