In our multi-tenant app where account_id = ... is added to (almost) every query made. Almost every table has an account_id column.
In such a setup, when adding indexes to tables for their foreign keys, should I always include the account_id in the index?
So for example:
add_index 'projects', ["customer_id"]
OR
add_index 'projects', ["customer_id","account_id"]
The way our application is setup it will never run a query on the projects table without a condition on account_id, so I would expect the second option would be correct. But I am no db expert..
What is the best approach in our case?
This depends to a great extent on how many rows will exist in the Projects table for any particular customer Id value. If you’re never going to have more than perhaps a couple hundred or so, I’m not sure I would bother to add the account Id column. Of course, the best way to really determine this is to do some performance testing, with and without the index.