Skip to content

Instantly share code, notes, and snippets.

@eccyan
Created October 25, 2013 17:33
Show Gist options
  • Save eccyan/7158622 to your computer and use it in GitHub Desktop.
Save eccyan/7158622 to your computer and use it in GitHub Desktop.
ActiveRecord で MySQL のインデックスを指定して取得したい ref: http://qiita.com/eccyan/items/0641af416e6f9330f1e9
scope :use_index, lambda { |*indexes|
from("#{self.table_name} USE INDEX(#{indexes.join(', ')})")
}
Question.use_index(:index_questions_on_id).page.total_count
# (82.5ms) SELECT COUNT(*) FROM questions USE INDEX(index_questions_on_id)
# => 193887
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment