database design - Indexing foreign key columns -


Is there a rule of thumb for indexing all foreign key columns? It seems that they are often used in the table and take advantage of the index.

What if there are only 2 or 3 possible values ​​in the FK column? For example if this was referred to a status table. Will it still be appropriate to put an index on the FK field?

Edit I would like to redo the question [Is it appropriate to put an index on a column with some possible values ​​(2 or 3)?] END EDIT < / Strong>

(Two issues come together here: machine efficiency and DBA efficiency.)

As a rule of thumb, index performance on the columns used in the join clauses, and where the stories and, of course, display, display, update And delete suffers from the same index. (Because the index has to be updated with the table.)

If there are some values ​​in a column, and there is no compulsive reason for avoiding the actual value stored in the table, For example, ISO 5218 specifies these codes to represent human sexes, for example, to target a foreign key reference.

  Sex_id gender - 0 not known 1 male 2 female 9 do not apply   

I should remember that 9 Meaning not applicable If I had all this, I would have to be included in this table every time I need to produce production for people to read.

But I can add a column to that table, declare it unique, and use it as the target of foreign key references. I have to be involved in most of the time in this table. Heck, I may never be involved in this

  sex_id sex_code sex - 0 unrecognized No 1 M Male 2 F Female 9 NA Not Applicable   

Those three columns should be declared unique. And on most platforms, it means that each of those three columns will get an index. I index those columns that refer to the gender code, even if the selectivity is low.

Why? I have better things to re-evaluate to add an index now to now , because it to optimize it in resolution It is smart enough to do with DBA efficiency.

Comments