Sql-server – Will an index that is a partial key of the begining of another index perform any better than the longer index

indexperformancesql server

If I create an index that is a partial key of the begining of a second index, will SQL Server get any significant gains retreiving the results from the simpler index, when the search criteria matches the simpler index. For example if I had a non clustered index (TransactionDate, ClientID, State) and my search criteria is only TransactionDate and ClientID what search performance gains would I achieve by creating a second index of just TransactionDate and ClientID. Index maintenance (inserts, updates, deletes) and disk space usage is of no consideration. Low level detail of how sql server implements and utilizes indexes would be greatly appreciated.

The data types are TransactionDate DATETIME with a zero timestamp, ClientID VARCHAR(6), and State CHAR(2). The table has about 16 million rows, 100 dates with about 160,000 records for each day and 500 clients. The table is loaded daily with all the records having a new date.

Best Answer

There really isn't even any guarantee that the optimizer will use the index in the first place. The difference of only one column between the two indexes is (for the most part) trivial. But if it did, any performance gain you would see (if any) would be trivial.

The reason for this is in how the B-Tree index is implemented by SQL Server. Both indexes are equally capable of satisfying the predicate (WHERE clause) and therefore locating only the rows that meet the search parameters. The only difference between the two would be how many pages in the index SQL Server would have to read in order to locate the required rows. And how many pages will ultimately be determined by the size of the index rows (Storage size in bytes of (TransactionDate, ClientID) vs. the alternative, with only the size of the State column being the difference between the two.

Just as an example, if a single index row was 60 bytes, SQL Server would only have to read 2 pages in order to locate a row up until there were around 2.5 million rows in the index. Then it would only require 3 pages to be read until the table reached more than 300 million rows.

So, what does that mean? Whether it is looking through an index of 2 or all 3 columns, SQL Server is still only looking through the same number of pages to locate the rows, unless the State column is so large as to create a significant difference in the size of a single row, thus causing the index pages to fill considerably faster. Any noticeable performance difference would only be caused if SQL Server had to read through more levels of the index to satisfy the query (3 levels vs. 4 levels, etc) and a single column of text, especially if State is a 2 char state abbreviation, simply won't be enough to make a significant difference.

If you really want to boost performance of the query, and the table is quite large, you might be better off exploring table partitioning, or if not on Enterprise Edition perhaps a partitioned view, all depending on whether there are certain ranges that are primarily searched and others that are searched much less often.