Created
October 25, 2013 17:33
-
-
Save eccyan/7158622 to your computer and use it in GitHub Desktop.
ActiveRecord で MySQL のインデックスを指定して取得したい ref: http://qiita.com/eccyan/items/0641af416e6f9330f1e9
This file contains hidden or 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
scope :use_index, lambda { |*indexes| | |
from("#{self.table_name} USE INDEX(#{indexes.join(', ')})") | |
} |
This file contains hidden or 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
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