5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ruby on rails & mysql use force index

Posted at

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

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?