代码之家  ›  专栏  ›  技术社区  ›  TK.

什么时候在Rails的表中添加什么索引

  •  126
  • TK.  · 技术社区  · 15 年前

    我有一个关于Rails数据库的问题。

    • 我应该在自动创建的“id”列中添加“index”吗?
    • 我应该在自动创建的“id”列中添加“index(unique)”吗?

    • 如果我同时给两个外键添加索引( add_index (:users, [:category, :state_id]) ,会发生什么?这与为每个键添加索引有何不同?

      class CreateUsers < ActiveRecord::Migration
        def self.up
          create_table :users do |t|
            t.string :name
            t.integer :category_id 
            t.integer :state_id
            t.string :email
            t.boolean :activated
            t.timestamps
          end
        # Do I need this? Is it meaningless to add the index to the primary key?
        # If so, do I need :unique => true ?
        add_index :users, :id 
        # I don't think I need ":unique => true here", right?
        add_index :users, :category_id # Should I need this?
        add_index :users, :state_id # Should I need this?
        # Are the above the same as the following?
        add_index (:users, [:category, :state_id])
        end
      end
      

    • 我应该为xxx\u id添加“index with unique”,对吗?
    3 回复  |  直到 15 年前
        1
  •  176
  •   jigfox    7 年前

    我应该为所有外键添加“index”吗,比如“xxx\u id”?

    这样会更好,因为它加快了此列中排序的搜索速度。外键是人们经常搜索的东西。

    由于rails版本5,索引将自动创建,有关更多信息,请参阅 here

    我应该在自动创建的“id”列中添加“index(unique)”吗?

    不,同上

    add_index (:users, [:category_id, :state_id]) ,会发生什么?这与为每个键添加索引有何不同?

    然后索引是两列的组合索引。这没有任何意义,除非你想要所有的条目都是一个 category_id 以及 state_id (应该是 category )同时。

    # rails 2
    User.find(:all, :conditions => { :state_id => some_id, :category_id => some_other_id })
    
    # rails 3
    User.where(:state_id => some_id, :category_id => some_other_id)
    

    add_index :users, :category_id
    add_index :users, :state_id
    

    将加速这些请求:

    # rails 2+3
    User.find_by_category_id(some_id)
    User.find_by_state_id(some_other_id)
    
    # or
    # rails 2
    User.find(:all, :conditions => {:category_id => some_id})
    User.find(:all, :conditions => {:state_id => some_other_id})
    
    # rails 3
    User.where(:category_id => some_id)
    User.where(:state_id => some_other_id)
    

    我应该为xxx\u id添加“index with unique”,对吗?

    许多的 将用户分为一个类别。在你的 User 你有这样的模型吗 belongs_to :category 在你的分类模型中 has_many :users . 如果你有 has_many foreign_key 字段不能唯一!

    有关这方面的更多详细信息,请参阅 tadman 太好了 answer .

        2
  •  113
  •   tadman    15 年前

    索引可能是一件棘手、微妙的事情,但有一些通用的规则可以使确定使用哪种方法变得更加容易。

    • 如果你有 belongs_to - has_many 关系配对时,需要对使用的外键建立索引。
    • 如果你有 has_many :through 关系时,联接表中所涉及的两个属性上都应该有一个唯一的索引作为复合键。
    • 如果您直接使用唯一标识符(如用户名或电子邮件)获取记录,则该标识符应为唯一索引。
    • 如果你从一个 使用范围时,请确保有一个包含 外键和作用域列的顺序。

    使用索引的目的是消除数据未正确索引时出现的可怕的“表扫描”或“文件排序”操作。

    简单来说,请查看应用程序生成的查询,并确保 WHERE HAVING 条件和 ORDER BY 从句是按那个顺序表示的。

        3
  •  14
  •   Community Mohan Dere    9 年前
    • 总是索引外键
    • 总是索引您要排序的列
    • add_index :users, :email, unique: true )
    • 如果按两件事排序,或按两件事搜索,例如: order by [a, b] find where( a and b ) ,则需要双索引:

    具体例子:

    如果您有:

    default_scope :order => 'photos.created_at DESC, photos.version DESC'
    

    add_index :photos, [:created_at, :version]
    

    注: 索引会占用磁盘上的额外空间,并且会减慢创建和更新每个记录的速度,因为它必须重建每个索引。

    学分:

    https://tomafro.net/2009/08/using-indexes-in-rails-choosing-additional-indexes , rails - created_at when user for ordering, Should you add an Index to the table? ,以及上面的答案。