skip to Main Content

We are structuring a project where some tables will have many records, and we intend to use 4 numeric foreign keys and 1 numeric primary, our assumption is that if we create an index for each foreign key and the default index of the primary key, the postgres planning would use all the starts (5 in total) to perform the query.

95% of the time the queries would be providing at least the 4 foreign keys.

  1. Would each index be used to position the search faster in the sequential section of records?
  2. Would having 4 indexes increase the speed of the query or would it suffice with a single index of the parent level (branch_id)?

Thank you for your time and experience.

example: if all foreign keys have an index

SELECT * FROM products WHERE 
account_d=1 AND 
organization_id=2 AND 
business_id=3 AND 
branch_id=4 AND 
product_id=5;

example: if I only indicate the id of the primary key

SELECT * FROM products WHERE product_id=5;

table

2

Answers


  1. Indexes are (predominantly) used when filtering or joining tables, so whether the indexes you are proposing are useful is entirely dependent on the SQL you are running and whether the query optimiser determines that using an index would be beneficial.

    For example, if you ran SELECT * FROM TABLE then none of the indexes would be used.

    I can’t comment on Postgresql specifically but many/most DBMSs automatically create indexes when you define PKs/FKs – so you will get the indexes anyway, regardless of any performance tuning you are trying to implement

    Update
    Having individual indexes on each column is not going to help with the query you’ve provided, the optimiser will only use one of them, probably the PK. A compound index on multiple columns would help, but the more columns you add to the index, the more restrictive the pattern of queries it will benefit.

    Say you have 3 columns A, B, C and include them all in WHERE clause, then having a compound index of A+B+C would be highly beneficial.
    If you keep this index but your WHERE clause only has columns A, B it will still benefit significantly as the query can still use the A+B subset of the index.
    If your WHERE clause only has columns A,C then it would benefit only slightly, as it would select all records from the index that start with the A value – but then would have to filter them to find the subset with the C value

    Login or Signup to reply.
  2. If all 4 columns are specified by equality, it is possible to combine the single-column indexes using BitmapAnd. However, this would be less efficient than using one multi-column index on all four columns.

    Since that will apparently be a very common query, it would make sense to have that multi-column index.

    Usually you will want to index each foreign key column. Otherwise, if you want to delete an organization, for example, it would need to scan the whole table to verify that no records were still referencing it. Whichever column is the first one in the multi-column index will not need to also have a single-column index for it. But the other 3 which are not first probably still need their own indexes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search