Help us understand the problem. What is going on with this article?

ruby on rails & mysql use force index

More than 3 years have passed since last update.

datatablesのscrollerでOFFSETがある程度を超えると
mysqlのオプティマイザがおかしなindexを使って遅くなったのでforce indexを使いたくなったので調べた。

scopeをmodelに記述。ちょっと意味が分からなかったけどノールックで追加。

class NAME::User < ActiveRecord::Base

User classの一番下にとりあえず追加しておいた。

USE INDEX

  scope :use_index, ->(index) { 
    from("#{self.table_name} USE INDEX(#{index})")
  }

FORCE INDEX

  scope :force_index, lambda { |*indexes|
    from("#{self.table_name} FORCE INDEX(#{indexes.join(', ')})")
  }

使い方

users = @user.users.force_index(:index_users_on_test_name).where(user_flag: true, disabled: false)

参考
http://stackoverflow.com/questions/13904035/use-specific-mysql-index-with-rails

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away