Lets say I have...create table test ( pk int not null, dt smalldatetime null, constraint pk primary key nonclustered (pk) )create unique clustered index cx on test(pk,dt)create table test2 ( pk int not null constraint fk references test(pk), status char(1) not null default 'a', constraint pk2 primary key nonclustered (pk))
...and then I create...create unique clustered index cx_2 on test2(status,pk)
...and execute....select test.pkfrom test inner join test2 on test.pk = test2.pkwhere test2.status = 'a' and dt is null
This does a index scan on test.pk and a seek of test2.pk2, however if instead of the above cx_2, I create...create unique clustered index cx_2 on test2(status,pk)
...it does an index seek on test.pk and a seek on test2.cx_2.Why is this? I always thought that a multi column index should be create in selectivity order, and in this case pk (being unique) should be more selective than status. However, (pk,status) index causes the optimizer to not consider the cx_2 index at all and rather use a scan of the nonclustered pk2 index.... Is this because I don't have any data in the table and thus no valuable statistics, or am I missing something? SQL Server 7....<O>