本文介绍了当添加哪些指标在Rails的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • 我应该加上指数像xxx_id所有的外键?
  • 我应该加上指数来自动创建的ID列?
  • 我应该加上指数(唯一),以自动创建的ID列?

  • 如果我添加索引的两个外键一次( add_index(:用户,[:类别:STATE_ID]?),会发生什么这是怎么回事不同于添加索引每个键?

     类CreateUsers< ActiveRecord的::迁移
      高清self.up
        CREATE_TABLE:用户做| T |
          t.string:名称
          t.integer:CATEGORY_ID
          t.integer:STATE_ID
          t.string:电子邮件
          t.boolean:激活
          t.timestamps
        结束
      #我需要这个?它是毫无意义的索引添加到主键?
      #如果是这样,我需要:独特=>真正 ?
      add_index:用户:ID
      #我不认为我需要:独特=>真正在这里,对不对?
      add_index:用户:CATEGORY_ID#如果我需要这个?
      add_index:用户:STATE_ID#如果我需要这个?
      #是上述相同,如下?
      add_index(:用户,[:类别:STATE_ID])
      结束
    结束
     


大答案为止。其他的问题。

  • 我要补充指数具有独特的为xxx_id,对吧?
解决方案

这将是更好,因为它加速了在此列排序搜索。键和外键的东西搜索了很多。

没有,这一步已经完成由轨道

没有,同上

然后该索引是两列的一个组合的索引。这没有任何意义,除非你想为一个 CATEGORY_ID 一个 STATE_ID (应该是 CATEGORY_ID 不是)在同一时间。

像这样的指数将加速以下请求了:

 #导轨2
User.find(:所有,:条件=> {:STATE_ID => some_id,:CATEGORY_ID => some_other_id})

#导轨3
User.where(:STATE_ID => some_id,:CATEGORY_ID => some_other_id)
 

其中,

  add_index:用户:CATEGORY_ID
add_index:用户:STATE_ID
 

将加快这些请求:

 #导轨2 + 3
User.find_by_category_id(some_id)
User.find_by_state_id(some_other_id)

# 要么
#导轨2
User.find(:所有,:条件=> {:CATEGORY_ID => some_id})
User.find(:所有,:条件=> {:STATE_ID => some_other_id})

#导轨3
User.where(:CATEGORY_ID => some_id)
User.where(:STATE_ID => some_other_id)
 

没有,因为如果你这样做,只有一个用户可以在一个类别,但类别的意思是,你可以把更多的很多用户归为一类。在你的用户模型,你有这样的事情 belongs_to的:类并在分类模型类似的has_many:用户。如果你有一个的has_many 关系 foreign_key 字段不能是唯一!

有关此更详细的信息,你应该看一看 tadman 的伟大answer.

I have a question about Rails database.

  • Should I add "index" to all the foreign keys like "xxx_id"?
  • Should I add "index" to the automatically created "id" column?
  • Should I add "index(unique)" to the automatically created "id" column?

  • If I add index to two foreign keys at once (add_index (:users, [:category, :state_id]), what happens? How is this different from adding the index for each key?

    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
    


Great answer so far. Additional question.

  • I should add "index with unique" for xxx_id, right?

解决方案

It would be better, because it accelerates the search in sorting in this column. And Foreign keys are something searched for a lot.

No, this is already done by rails

No, same as above

Then the index is a combined index of the two columns. That doesn't make any sense, unless you want all entries for one category_id AND one state_id (It should be category_id not category) at the same time.

An Index like this would speed the following request up:

# 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)

Where

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

will speed up these requests:

# 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)

No, because if you do this, only one user can be in one category, but the meaning of category is that you can put more many user into one category. In your User model you have something like this belongs_to :category and in your Category model something like has_many :users. If you have a has_many relationship the foreign_key field must not be unique!

For more detailed information on this you should take a look at tadman's great answer.

这篇关于当添加哪些指标在Rails的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:56