假設如下:的SQL Server索引的使用問題
/*
drop index ix_vouchers_nacsz on dbo.vouchers;
drop index ix_vouchers_nacsz2 on dbo.vouchers;
create index ix_vouchers_nacsz on dbo.Vouchers(
FirstName, LastName,
Address, Address2, City,
State, Zip, Email
);
create index ix_vouchers_nacsz2 on dbo.Vouchers(
Email, FirstName, LastName,
Address, Address2, City,
State, Zip
);
*/
select count(firstname) from vouchers
with (index(ix_vouchers_nacsz))
where
firstname = 'chris' and
lastname = '' and
address = '' and
address2 = '' and
city = '' and
state = '' and
zip = ''
select count(firstname) from vouchers
with (index(ix_vouchers_nacsz2))
where
firstname = 'chris' and
lastname = '' and
address = '' and
address2 = '' and
city = '' and
state = '' and
zip = ''
爲什麼第二索引的結果索引掃描,而在指數第一的成績求?密鑰的排序有什麼不同?
好的一點,是的,在索引中包含「電子郵件」列是從來沒有使用的呈現它對這個查詢 – 2009-08-10 05:38:37
我沒有使用電子郵件列另一個查詢。我將它添加到這個索引中,希望這兩個查詢都可以使用它,而不是有兩個大的索引。我應該創建兩個索引嗎? – Chris 2009-08-10 05:52:41
根據問題中的信息,我會在(名字,姓氏)和(電子郵件)上創建兩個索引。這些索引幾乎應該唯一標識一行。 – Andomar 2009-08-10 06:12:07