[英]SQL Server index usage question
假设以下内容:
/*
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 = ''
为什么第二个索引导致索引扫描而第一个导致索引搜寻? 按键的顺序有什么不同?
在索引“索引中的列排序”的情况下,“ where子句中的列排序”会遇到很多问题。 您可以参考以下链接:
http://ashishkhandelwal.arkutil.com/sql-server/quick-and-short-database-indexes/
•使用索引的最佳实践•如何获得最佳性能表单索引•聚集索引注意事项•非聚集索引注意事项
我确信这将在计划索引时为您提供帮助。
应用“电话簿”类比可能有助于理解。
第一个索引是“电话簿”,按“姓”,“姓”等排序。 如果您要求在此电话簿中查找Chris,则可以找到该索引一起列出的所有Chris。
在第二个索引中,是一本电话簿,按“电话号码”(或同样容易发送的电子邮件),名,姓等排序。 如果您要求使用此电话簿来查找克里斯的名字列表,那么您的电话簿运气就不会这样! 当然,如果要求您查找电子邮件地址example@example.com并命名为Chris,则可以先找到该电子邮件地址,然后再查找匹配的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.