Indexes with included columns, what's the difference?

前端 未结 3 488
孤独总比滥情好
孤独总比滥情好 2021-02-02 12:24

I\'ve never really understood the difference between these two indexes, can someone please explain what the difference is (performance-wise, how the index structure will look li

3条回答
  •  天涯浪人
    2021-02-02 12:54

    In first Index, in Index page only PostalCode is the key column and AddressLine1, AddressLine2, City, StateProvinceID are part of leaf node to avoid key/RID lookup

    I will prefer first index when my table will be filtered always on PostalCode and any of this columns AddressLine1, AddressLine2, City, StateProvinceID will be part of select and not filtration

    select AddressLine1, AddressLine2, City, StateProvinceID
    from Person.Address 
    Where PostalCode=  
    

    In second index, in Index page there will be five key columns PostalCode, AddressLine1, AddressLine2, City, StateProvinceID

    I will prefer second index when I have possiblity to filter data like

    Where PostalCode = And AddressLine1 = 
    

    or

    Where PostalCode = And AddressLine2 = 
    

    or

    Where PostalCode = And AddressLine1  = and AddressLine2 = 
    

    and so on..

    At any case the first column in index should be part of filtration to utilize the index

提交回复
热议问题