Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Indexing basics

Author  Topic 

dextrous
Starting Member

15 Posts

Posted - 2009-07-31 : 00:13:10
Hello,
I have been trying to educate myself about indexes as this is all quite new to me.
1. Cluster index seems like it is the best way to go for most tables. Is there ever a reason not to use it?
2. Does having a unique cluster increase the performance of queries even more?
3. I have a table with county names and state. Within each state, the names are unique, but not within the whole column. Currently I have an index on just county name. Would it make more sense to create a "composite index" (i just ran into it on google!) for this structure?
4. Once I make an index on a column, when i query it, do I need to refer to the index's name at all--I read something about this and was confused.

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-07-31 : 00:14:38
1. Staging tables might not need them
2. Yes
3. Sounds right.
4. No, but you can if the query optimizer doesn't choose the right one. They are called index hints, and you'll rarely use them if ever.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

dextrous
Starting Member

15 Posts

Posted - 2009-07-31 : 02:39:35
Would it be a lot more efficient to have columns like these:

County, States County Found in
Hays, TX-FL-AL

than

County,State
Hays, TX
Hays, FL
Hays, AL

Would query performance improve a lot to use unique cluster index and use the first format?

I'm querying the county name repeatedly in the application to get the state.

The only problem is that
Hays, TX has its own unique record (lets say population, etc.) attached with it...not sure about the optimum db design!
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-07-31 : 14:21:04
probably more efficient like this

County,State
Hays, TX
Hays, FL
Hays, AL

put unique index over county/state

Even more efficient to use zip code.

Go to Top of Page

dextrous
Starting Member

15 Posts

Posted - 2009-08-03 : 23:42:41
One thing I cannot figure out is how do I create unique over two columns?
So, Hays,TX is of course unique...

I tried selecting cluster and added both column and then selected unique. But it gives an error message saying "Hays is not unique".

Thanks a lot.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-08-04 : 17:50:46
[code]
Create UNIQUE CLUSTERED INDEX on tableName (county, state)
[/code]
Go to Top of Page

dextrous
Starting Member

15 Posts

Posted - 2009-08-04 : 22:28:53
error...
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'on'.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-08-05 : 15:05:29
quote:
Originally posted by dextrous

error...
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'on'.




It's always a good idea to break open Books Online (SQL Server documentation) to verify syntax. It's much faster than waiting for a response from one of us here.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -