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

  • Home
  • SEARCH
  • 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 3986378
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:00:19+00:00 2026-05-20T06:00:19+00:00

I have a has_many, :through relationship in my Rails database, and the :through table

  • 0

I have a has_many, :through relationship in my Rails database, and the :through table has these important fields:

Assignment.rb (subset)
company_id
project_id
importance
order

All these columns are used heavily by queries throughout the system. Depending on the situation, these queries look for up to 4 of these fields and in different combinations.

I have these indexes:

company_id
project_id

Do I need to add a new index for each specific combination I use leading to potentially lots of indexes, or for maximum performance can I just add:

company_id, project_id, importance, order

and the system will just search for whichever combination it is given?

  • 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-20T06:00:19+00:00Added an answer on May 20, 2026 at 6:00 am

    Assuming you are using MySQL, if you create an index on (company_id, project_id, importance, order) that means you can leverage that index for queries using:

    • company_id
    • company_id and project_id
    • company_id and project_id and importance
    • company_id and project_id and importance and order

    But you can’t index project_id or importance or order by itself. Hopefully this should help you with creating indexes. If you have a dependency, then create an index for it. For example, if you always search order when importance is involved, create an (importance, order) index. Keep in mind that indexes will be leveraged when used individually, so if you have the following indexes: (company_id), (project_id), (importance), (order) you’ll be able to leverage them when searching by company_id and project_id for example.

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

Sidebar

Related Questions

I have two models that has a manytomany relationship with a 'through' table in
I have a basic has_many :through relationship that is bi-directional: calendars have many calendar_calendar_events
I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
So Rails doesn't have support for :through associations through a habtm relationship. There are
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
Right now I have a table called Campaigns that has many Hits, if I
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to
I have 3 tables (archive has many sections, section (may) belong to many archives):
I have an application that has many different types of objects that each persist
I have a class which has many small functions. By small functions, I mean

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.