Commit c6f4a0acb98c32b6c86ae88aaab2413934b9e39c

Authored by Luke Baker
1 parent 27a7d480

add task to update cached Choice losses / wins

add indexes to votes table to speed up task, and should speed up other
queries we do
db/migrate/20101221143936_add_indexes_to_vote.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class AddIndexesToVote < ActiveRecord::Migration
  2 + def self.up
  3 + add_index(:votes, :loser_choice_id, :name => 'loser_choice_id_idx')
  4 + add_index(:votes, :choice_id, :name => 'choice_id_idx')
  5 + add_index(:votes, :question_id, :name => 'question_id_idx')
  6 + end
  7 +
  8 + def self.down
  9 + remove_index(:votes, :name => :loser_choice_id_idx)
  10 + remove_index(:votes, :name => :choice_id_idx)
  11 + remove_index(:votes, :name => :question_id_idx)
  12 + end
  13 +end
... ...
db/schema.rb
... ... @@ -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 => 20100727203816) do
  12 +ActiveRecord::Schema.define(:version => 20101221143936) do
13 13  
14 14 create_table "appearances", :force => true do |t|
15 15 t.integer "voter_id"
... ... @@ -220,6 +220,9 @@ ActiveRecord::Schema.define(:version =&gt; 20100727203816) do
220 220 t.string "validity_information"
221 221 end
222 222  
  223 + add_index "votes", ["choice_id"], :name => "choice_id_idx"
  224 + add_index "votes", ["loser_choice_id"], :name => "loser_choice_id_idx"
  225 + add_index "votes", ["question_id"], :name => "question_id_idx"
223 226 add_index "votes", ["voter_id"], :name => "index_votes_on_voter_id"
224 227  
225 228 end
... ...
lib/tasks/test_api.rake
1 1 namespace :test_api do
2 2  
  3 + desc "Updates cached values for losses and wins for for choices."
  4 + task :update_cached_losses_wins => :environment do
  5 + Question.all.each do |question|
  6 + question.choices.each do |choice|
  7 + choice.reload
  8 + true_losses = question.votes.count(:conditions => {:loser_choice_id => choice.id})
  9 + true_wins = choice.votes.count
  10 + Choice.update_counters choice.id,
  11 + :losses => (true_losses - choice.losses),
  12 + :wins => (true_wins - choice.wins)
  13 + end
  14 + end
  15 + end
  16 +
3 17 task :all => [:question_vote_consistency,:generate_density_information]
4 18  
5 19 desc "Don't run unless you know what you are doing"
... ...