Consider that a table has 2 columns,
- column_1 has 60% distinct values and column_2 has 40% distinct values.
- Both has same data type, say integer
If one has to apply the indexes on this table, which column he should chose to index? why?
Consider that a table has 2 columns,
If one has to apply the indexes on this table, which column he should chose to index? why?
The most important consideration to create an index is if you perform search over that field so the Datatype is irrelevant.
Now while more distinct values more efficient is the the index because you discard rows faster. If most of the rows are the same the index will be the same as a FULL SCAN
So if you have querys like
SELECT * FROM Table WHERE fieldA = Value;
SELECT * FROM Table WHERE fieldB = Value;
You want two index, one for each field
But if you have querys like
SELECT * FROM Table WHERE fieldA = Value
AND fieldB = Value;
You want composte index with both fields (fieldA, fieldB)
Last tip is if you want return a single value on your select
SELECT fieldB FROM Table WHERE fieldA = Value;
You also want a composite index, because you will search the index for Value and dont have to make a lookup on the table to get the value of fieldB
because is already on the index.