4

I understand how single indexed column works in SQL Server and how it is implemented using balanced trees. There are plenty of interesting videos on YouTube on this topic. However I don't understand how it works if the index is based on multiple columns. For example:

CREATE NONCLUSTERED INDEX idxItemsCatState
ON Items (Category,OfferState)
INCLUDE ([Id],[Ranking])

And how it can speedup query like

SELECT ID, Ranking FROM Items where Category = 1 AND OfferState < 3

It is still implemented as B-Tree? How it can evaluate the combination of values? What are the restrictions for such feature?

asked Oct 17, 2015 at 15:12

3 Answers 3

5

To store rows in a b-tree and perform a seek, all that is needed is an ordering in which the rows should be sorted. Just like you can sort on (Category), you can also sort on the tuple (Category, OfferState). In the latter case, rows are first sorted by Category and then any ties are broken by sorting by OfferState.

The resulting index will use the same b-tree structure, but the value for each entry in the b-tree will be a (Category, OfferState) tuple.

And how it can speedup query like...

For your query, SQL Server can perform a seek in the following way:

  • Seek to the first row that matches Category = 1. This can be done using the same b-tree seek you are familiar with, with SQL Server only needing to use the Category portion of each (Category, OfferState) tuple.
  • Begin reading rows, and continue until a row with OfferState >= 3 is found

In this way, SQL Server will be able to seek directly to the beginning of the range of rows that are needed, read those rows, and stop at the end of the range of rows. Note that you can see how this seek works by looking at the Seek Predicate property of the Index Seek operator in your query plan.

More generally, a multi-column index across columns (a, b, c, d, ...) can support a seek on any leading subset of columns, such as (a) or (a, b, c), when you are matching for equality (using =).

If you are looking for a range (e.g., b < 3), SQL Server can no longer seek on any columns that fall later in the index. In such a case, it would have to perform a separate seek within each distinct value of b, which is not supported (except in a more specific case you probably don't need to worry about: across partitions of a partitioned table).

answered Oct 17, 2015 at 16:36
4

Traditional indexes are implemented as B-trees, whether single or multi column key.

And how it can speedup query like

SELECT ID, Ranking FROM Items where Category = 1 AND OfferState < 3

The order of composite index keys is significant for query optimization. Specify column(s) used in equality predicates first (in order of selectivity), followed by column(s) used with inequality predicates. This will facilitate touching only those rows that match the equal condition, and then access rows within the specified range. So for this query, the column order in the index should be Category and OfferState rather than vice-versa.

Hannah Vernon
71.1k22 gold badges178 silver badges324 bronze badges
answered Oct 17, 2015 at 16:46
3

Logically, the individual columns are put into a tuple (Category, OfferState). Those tuples are the index keys and the b-tree algorithm works unmodified.

B-trees do not need any kind of "condition". They work on any value domain that has a total order defined. The only operations that is ever being done in a data structure sense is to compare two keys. SQL Server in addition uses two more operations: serialization to bytes and deserialization from bytes.

answered Oct 17, 2015 at 16:25
2
  • I understand of comparison of one key. But what if the comparison of one key of tuple is true and second key is false? Is the B-tree going left or right? Or does need to be scanned entire subtree? Commented Oct 17, 2015 at 18:52
  • @qub1n the tuples are compared as a whole. The first non-equal value determines the outcome. (a, 3) is smaller than (a, 4). The b-tree itself does not care about how tuples are compared. Commented Oct 17, 2015 at 18:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.