Когда добавлять какие индексы в таблицу в Rails

У меня вопрос по базе данных Rails.

  • Должен ли я добавить «index» ко всем внешним ключам, таким как «xxx_id»?
  • Должен ли я добавить «index» в автоматически созданный столбец «id»?
  • Должен ли я добавить «index (unique)» в автоматически созданный столбец «id»?

  • Если я добавлю index сразу к двум внешним ключам (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_id, верно?
129
задан TK. 8 September 2010 в 01:18
поделиться