Sql-server – indexes with unspecific columns

indexsql server

I have a third party database in which a large number of the tables have a 'companyID' column, and most of the queries against those tables include companyID = @companyID in the where clause.

[EDIT: In addition to the where clause, the companyID is generally also used in the order by clause of most queries: e.g. order by companyId, otherField1, otherField2 etc..
]

In our organisation the companyID column is always populated with 1 (though there is an outside chance that may change at some point in the future).

Now I am looking at creating some indexes on these tables. SQL Server recommends some 'missing indexes' and all the recommendations contain the companyID field as the first column in the index.

Going on the principle of specifying most selective column first I would think that I should put the companyID column last, or perhaps not even have it in the index at all. But then I'm wondering why SQL Server's missing indexes view always suggests putting it first – surely SQL Server is intelligent enough to know that every row contains the same value for that column?

Best Answer

The SQL Server knows only about the column content if you're creating a index or a statistic for this column. If the companyID is always populated with 1 you never want to create an index for it - not even as last part of a multi column index. The recommended indices are sometimes stupid and should be considered as guess.