Pergunta

I have a huge (millions of rows) table that contains the following columns:

[When] datetime2(0),
[What] tinyint,
[Who] bigint

It's basically a table of events ('What') that happened to various entities ('Who') at a time ('When'). There are about 10 different values for 'What' - it is an enumeration. There are about 10,000 values for 'Who' at the moment.

I want to be able to query this table, to ask whether something happened to a one or more entities between a certain time. For instance, did [What = 0, 1, 2, 3] happen to [Who = 0, 1, 2, 3] between [When = '2012-10-01' to '2012-11-01'].

I'm looking for advice on how best to index this table. I'm fairly sure a composite index is the way to go, but I'm unsure of the exact configuration. For instance, what should the column order be? I've read that the 'most selective' columns should go to the left, which I think in this case would be [When], [Who], [What]. Is this correct?

Sorry if this question seems vague, but I'd be grateful for any input. I'm using SQL Server 2012.

Foi útil?

Solução

The myth about put the most selective column to the left is crap - sorry.

Your composite index will only be useful if you use the n left-most arguments, e.g. if you have the index on

(when, who, what)

then that index can answer question asking about

(when)

or about

(when, who)

or even about

(when, who, what)

but it cannot answer questions about

(who, what)

(since the left-most column isn't being used here).

That should be the point you consider - order the columns in such a way that you can answer the most of your questions with such a composite index.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top