all groups > sql server programming > december 2005 >
You're in the

sql server programming

group:

Creating Clustred/Index Keys


Creating Clustred/Index Keys Sniper
12/27/2005 10:12:02 PM
sql server programming:
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

Re: Creating Clustred/Index Keys Sniper
12/27/2005 11:27:02 PM
Hi Uri,

Thanks for your reply..

do you think it's better to create one non-clustred
(CustomerID,CountryCode,PostralCode) togather or shoud i create a
non-clustred key for each column?

thanks again,
-Aruna


[quoted text, click to view]
Re: Creating Clustred/Index Keys Sniper
12/28/2005 12:47:04 AM
Hi Tibor ,

Thanks for your reply.

I just wanted to know what what are the difference performance implications
between creating a single non-clsutred index for each colum say like you
create one key for PostralCode , one key for Country Code... and multipal
colums non-clsutred indexes. like you include all the keys in a one
none-clsured index

thanks
-Aruna




[quoted text, click to view]
Re: Creating Clustred/Index Keys Amish Shah
12/28/2005 4:16:35 AM
You can use covering index if you are using more than one columns in
your query , Create the column with the highest cardinality is the
first column.

Column combination for index depends on your query.

Please read the article suggested by Uri.

Regards
Amish

Re: Creating Clustred/Index Keys Uri Dimant
12/28/2005 8:32:40 AM
Hi
I have always ( yep it depends:-)))) tried to keep a clustred index as small
as possible

Good start

http://www.sql-server-performance.com/clustered_indexes.asp
http://www.sql-server-performance.com/nonclustered_indexes.asp



[quoted text, click to view]

Re: Creating Clustred/Index Keys JT
12/28/2005 9:11:21 AM
The two options you listed below are not functionally equivalent. If you
create a compund index (clustered or non-clustered) and the user queries on
a column other than the leading column (in this case PostalCode) then a
non-indexed scan will probably result. However, if PostalCode is indexed
seperately, then that index can be used.

When designing indexes, always have a specific goal in mind. For example,
improving queries against a specific column or a primary key constraint.

Clustered Index Design Guidelines
http://msdn2.microsoft.com/en-us/library/ms190639.aspx
Planning and Creating Indexes
http://www.microsoft.com/technet/prodtechnol/sql/70/books/c0618260.mspx

[quoted text, click to view]

Re: Creating Clustred/Index Keys JT
12/28/2005 9:16:49 AM
Clustered indexes are more benefitial for single lookups, ordered results,
and group by queries. However, it requires more I/O than non-clustered
indexes when inserting and updating, especially on large tables. I tend to
avoid the use of clustered indexes in a transactional (OLTP) database and
use them mostly in data warehouse or reporting databases.

[quoted text, click to view]

Re: Creating Clustred/Index Keys Tibor Karaszi
12/28/2005 9:31:48 AM
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/


[quoted text, click to view]
Re: Creating Clustred/Index Keys Uri Dimant
12/28/2005 10:35:12 AM
Hi
Again it depends on many things like what you are going to put in WHERE
condition,
only you do know your table's structure

Also keep in mind that SQL Server keeps statistics on left (first) column
in the composite index
It does not matter that the optimizer will not be able using indexes ,just
try to keep the first colum more selective and think what is the best column
to choose as a first one




[quoted text, click to view]

Re: Creating Clustred/Index Keys Sniper
12/28/2005 7:53:02 PM
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






[quoted text, click to view]
Re: Creating Clustred/Index Keys Tibor Karaszi
12/29/2005 10:49:15 AM
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]
AddThis Social Bookmark Button