简体   繁体   English

SQL Server索引使用问题

[英]SQL Server index usage question

Assuming the following: 假设以下内容:

/*

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 = ''

Why does the second index result in an index scan while the first results in an index seek? 为什么第二个索引导致索引扫描而第一个导致索引搜寻? What difference does the ordering of the keys make? 按键的顺序有什么不同?

The second index starts with the email field, but you're not filtering on email. 第二个索引以电子邮件字段开头,但您并未过滤电子邮件。 That makes the index useless. 这使索引无用。

An index is typically a b-tree that allows you to do a binary search . 索引通常是b树 ,可让您进行二分查找 The b-tree is sorted by its index fields. b树按其索引字段排序。 So if you know the first field, you can look it up quickly. 因此,如果您知道第一个字段,则可以快速查找它。 Within the same values of the first field, you can look up the second field very quickly. 在第一个字段的相同值内,您可以非常快速地查找第二个字段。

It's like a telephone book that's sorted on last name. 就像电话簿中按姓氏排序一样。 You can't use it to search for a specific telephone number. 您不能使用它来搜索特定的电话号码。

In the case of index "the column ordering in index", "column ordering in where clause" will metter a lot. 在索引“索引中的列排序”的情况下,“ where子句中的列排序”会遇到很多问题。 you could refer following link: 您可以参考以下链接:

http://ashishkhandelwal.arkutil.com/sql-server/quick-and-short-database-indexes/ http://ashishkhandelwal.arkutil.com/sql-server/quick-and-short-database-indexes/

•Best Practices to use indexes •How to get best performance form indexes •Clustered index Considerations •Nonclustered Indexes Considerations •使用索引的最佳实践•如何获得最佳性能表单索引•聚集索引注意事项•非聚集索引注意事项

I am sure this will help you when planning for index. 我确信这将在计划索引时为您提供帮助。

Applying the 'phone book' analogy may help in understanding. 应用“电话簿”类比可能有助于理解。

The first index is a 'phone book' sorted by FirstName, then Last Name and so on. 第一个索引是“电话簿”,按“姓”,“姓”等排序。 If your asked to look up Chris in this phone book, then you can find all the Chris' listed together by the index. 如果您要求在此电话簿中查找Chris,则可以找到该索引一起列出的所有Chris。

In the second index, is a phone book sorted by 'phone numbers' (or email just as easily) then first name, then last name and so on. 在第二个索引中,是一本电话簿,按“电话号码”(或同样容易发送的电子邮件),名,姓等排序。 If your asked to use this phone book to look up listings with the firstname of Chris, your out of luck the phone book is not sorted that way! 如果您要求使用此电话簿来查找克里斯的名字列表,那么您的电话簿运气就不会这样! Of course, if you were asked to look for email address example@example.com and name Chris, then you can find the email address first, and then look for matching names. 当然,如果要求您查找电子邮件地址example@example.com并命名为Chris,则可以先找到该电子邮件地址,然后再查找匹配的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM