I want to create an index on a table and I'm trying to decide the order of the columns in the index, based on column selectivity.
So that the most selective column (the one that narrows further choices down the most) will be the first in the index, followed by the second most selective column etc.
How do I calculate selectivity of the column?
It's a simple ratio for each column:
(Number of Unique Values for the Column) to (Number of Rows In Table)
Calculating the numbers depends on your RDBMS. For SQL Server, you could get the numbers as follows:
SELECT COUNT(DISTINCT ColumnName) FROM TableName
SELECT COUNT(*) FROM TableName