Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Does the order of columns matter?
Message
From
26/08/2016 07:04:55
 
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
01639951
Message ID:
01640018
Views:
50
>>>>>>When creating a unique index and FK constraint on two columns, does the order of columns matter?
>>>>>>
>>>>>>For example, I want to have unique index on SITE_NO (Int) and CATEGORY (Char(20) and a FK Constraint on these two fields. Would [SITE_NO],[CATEGORY] create different "results" from [CATEGORY],[SITE_NO]?
>>>>>>
>>>>>>TIA
>>>>>
>>>>>Yes, order of columns matter in the index. Select most selective column first.
>>>>
>>>>What do you mean by "most selective"?
>>>
>>>If you have many categories and just few sites, then using Category as first column in the index may help with the queries. If it's the opposite situation of column's selectivity, then the index should start with the SiteNo column.
>>
>>I know little SQL but that sounds counter-intuitive. Wouldn't putting the field with the least matches first make more sense ?
>
>Take a look at this very good white paper.
>
>https://technet.microsoft.com/en-us/library/jj835095%28v=sql.110%29.aspx?f=255&MSPPError=-2147217396
>
>Somewhere in the middle of the article you can find the following:
>
>Consider using filtered indexes on columns that have well-defined subsets, for example sparse columns, columns with mostly NULL values, columns with categories of values, and columns with distinct ranges of values. A well-designed filtered index can improve query performance, reduce index maintenance costs, and reduce storage costs.
>Consider the order of the columns if the index will contain multiple columns. The column that is used in the WHERE clause in an equal to (=), greater than , less than , or BETWEEN search condition, or participates in a join, should be placed first. Additional columns should be ordered based on their level of distinctness, that is, from the most distinct to the least distinct.
>For example, if the index is defined as LastName, FirstName the index will be useful when the search criterion is WHERE LastName = 'Smith' or WHERE LastName = Smith AND FirstName LIKE 'J%'. However, the query optimizer would not use the index for a query that searched only on FirstName (WHERE FirstName = 'Jane').

If I read that right then my supposition was correct ? :
"Additional columns should be ordered based on their level of distinctness, that is, from the most distinct to the least distinct."
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform