Try to visualize the indexes. As soon as you get int the habit of doing that, most of these
questions will be simple to answer.
In your example, you have a cl ix on col1, col2. Cl means that the leaf pages is the data.
The query does ANDed equal comparison on col1, col2 and col3. It also returns only these three
columns. Since you are using AND, SQL Server can use the index on (col1, col2) to find the rows that
satisfies that condition. For each row found (in the leaf level of the cl is, which also is the data
pages), SQL server will look at each row to see if it also satisfied the condition on the col3
column.
So, the gain in adding the col3 column is really the difference in how many rows does the query
return if you only have the condition on col1 and col2 vs. if you have the condition on col1, col2
and col3. The number of rows is the difference. But since we count pages, you need to consider how
mane rows you fit on each page. After calculating this (formulas is in Books Online), you can take
number of rows per page divided by the difference of rows returned if you have condition on two or
all three columns.
As you can see, it is possible to calculate these things, but it is easier to just test the
different strategies and check the difference (looking at the execution plan, number of I/O etc).
You can be even fancier, by creating anon-clustered index on (col1, col2, col3). Now that index
would cover the query. So SQL Server navigate the index and when in the leaf level, no need to jump
down to a data page. How much gain compared to a clustered index (as per above)? It depends on the
row size in the non-clustered index (col1, col2, col3) vs. the row size in a data page. You can
calculate for these two alternatives number of pages that fit a row (in the nc index, vs. a data
page) and then calculate the difference.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp http://www.solidqualitylearning.com/ Blog:
http://solidqualitylearning.com/blogs/tibor/ [quoted text, click to view] "Sniper" <kakopappa@hotmail.com> wrote in message
news:A92A20B1-FAFC-460B-8890-943CBFD963D0@microsoft.com...
> hi all
>
> thank you for the information .. just one more thing extra ..
>
>
> +-------+--------+---------
> COL1 COL2 COL3
> +-------+--------+---------
> TC AAA ITEM1
> TC AAA ITEM2
> TC AAB ITEM3
>
> in this is Table if i have created a clustred key on COL1, COL2
>
> to match with this query
>
> SELECT COL1, COL2, COL3 FROM Table
> WHERE COL1 = 'TC' AND COL2 = 'AAB' AND COL3 = 'ITEM3'
>
> should I create a non-clustred index on colum COL3 ?
>
> or should I create a non-clustred key for COL1, COL2, COL3 ??
>
>
> thanks in advice again
> -Aruna
>
>
>
>
>
>
> "Tibor Karaszi" wrote:
>
>> You create indexes to assist your queries. without knowing that information, we really can't help
>> you. If you typically, for instance, search on Companycode, then you should have an index on that
>> column (or at least that column as the first column in an index). Start with your queries.
>>
>> --
>> Tibor Karaszi, SQL Server MVP
>>
http://www.karaszi.com/sqlserver/default.asp >>
http://www.solidqualitylearning.com/ >> Blog:
http://solidqualitylearning.com/blogs/tibor/ >>
>>
>> "Sniper" <kakopappa@hotmail.com> wrote in message
>> news:A68BC694-8B80-4D26-8593-11BDCA7F4550@microsoft.com...
>> > Hi guys
>> >
>> > I have a small question. :)
>> >
>> > I just wanted to know what is the best method out of these two methods when
>> > creating indexes on a very heavy transactional database
>> >
>> > 1. Create a wider CLUSTRED INDEX , for an example,
>> > in the Customers table, Put the CLUSTRED KEY AS
>> > CustomerID,PostralCode, CompanyCode
>> >
>> > or
>> > 2.Create the CLUSRED INDEX on CustomerID and create indexes for dah all
>> > other columns.
>> >
>> > ? which one is the best ??
>> >
>> >
>> > thanks for your advice and time
>> >
>> > -Aruna
>> >
>> >
>>
>>