Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:33:23+00:00 2026-05-16T18:33:23+00:00

I have a question about Rails database. Should I add index to all the

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:33:24+00:00Added an answer on May 16, 2026 at 6:33 pm

    Should I add “index” to all the foreign keys like “xxx_id”?

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

    Since Version 5 of rails the index will be created automatically, for more information see here.

    Should I add “index” to the automatically created “id” column?

    No, this is already done by rails

    Should I add “index(unique)” to the automatically created “id” column?

    No, same as above

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

    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)
    

    I should add “index with unique” for xxx_id, right?

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about best practices regarding how one should approach storing complex
I have a question about design winforms. Should I use, or not, group boxes
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about using streams in .NET to load files from disk.
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have a question about tables in MySQL. I'm currently making a website where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.