September 13, 2010 at 9:50 am
Hello,
We are a small team of developers with a lot of experience in VB, C#, etc, but no DBAs. We have an application that has a DB with about 250 tables. None of our tables have clustered indexes. Few of these tables have any kind of index other than a non-clustered primary key. It would not be easy to recreate the database with tables with clustered primary keys becuse every thing would need to be scripted since the application is installed at about 800 locations.
A typical table has DDL something like this...
CREATE TABLE Foo
(
FooID int IDENTITY,
FooTooID int NOT NULL,
FooDT datetime NULL,
FooValue int NULL,
FooComment varchar(255) NULL
)
go
ALTER TABLE Foo ADD PRIMARY KEY NONCLUSTERED (FooID)
go
Should we create, at a minimum, indexes on the primary keys on all tables?
If so, should these be clustered or non-clustered.
Does it matter that some of these tables have millions of rows and some have only a few rows?
Thanks in advance for any help in this area.
---Sam
[font="Verdana"]Samjazz[/font]
September 13, 2010 at 9:53 am
The primary key is an index, always. Whether it's better as clustered or nonclustered is hard to say without analysing the system. The queries that run against the tables determine the indexes. Without seeing the queries, it's near impossible to advise meaningfully on indexes.
If I may suggest, ask management about getting a consultant in to help you with indexing and performance tuning. There's no better way to learn than from someone who knows what they are doing.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 13, 2010 at 10:00 am
Thanks for your quick response. I was afraid that that would be the answer. I was hoping there were some general rules that could be applied. Is there any good book or source for such principals. I don't think our company can afford to hire a consultant at this time so the job is falling on me.
[font="Verdana"]Samjazz[/font]
September 13, 2010 at 10:03 am
September 13, 2010 at 11:45 am
The book Steve recommended is a good one.
The thing is, perf tuning's a huge area and one that's not easy to learn by yourself. I got a start by learning from and working with someone who really knew what he was doing. It's as much an art as a science.
This may help a little:
p.s. You don't necessarily have to get someone in for a long engagement. As an example, I do perf tuning for 3 clients at the moment, just 2 days a month.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 13, 2010 at 12:43 pm
Blanket statements are usually worthless, so with that in mind...
I'd suggest, at a minimum, making the PK's clustered instead of non-clustered. Probably they're the best access path to the data in most of the queries you have. That would help performance some. But, not knowing all the details of your situation, that could be weak advice.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
September 14, 2010 at 7:00 am
Thank you all for the good advice. I will be ordering that book, reading the given links, and etc...
Is there a negative imact to adding a clustered index (as below to table Foo) that is the same as the non-clustered primary key? Or would appropriate clustered indexes have a performance gain significant enough to justify adding a second (clustered) index?
CREATE UNIQUE CLUSTERED INDEX IXFooID ON Foo(FooID)
go
If having the two indexes on the same PK column is unnecessary and degrades performance, I will have to recreate the PKs to change them to clustered.
Is there a script that would generate the DDL to remove all RI, recreate the PKs (as clustered) and reapply the RI to all tables in a DB?
[font="Verdana"]Samjazz[/font]
September 14, 2010 at 7:14 am
SamJazz (9/14/2010)
Is there a negative imact to adding a clustered index (as below to table Foo) that is the same as the non-clustered primary key?
Yes. Slower insert/update/delete as both indexes will have to be updated. Wastes space on disk, in backups. Longer backup time, longer index maintenance
Is there a script that would generate the DDL to remove all RI, recreate the PKs (as clustered) and reapply the RI to all tables in a DB?
I don't have one. Maybe Redgate's SQL Refactor will help (don't know offhand). Otherwise such a script is not impossible to generate, just difficult.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 14, 2010 at 7:23 am
It just so happens that today is T-SQL Tuesday and the theme is indexes, you may want to look here. The book that Steve recommended was written by Grant and is very good, I am working my way through it at the moment 😀
Use Grant's book and suppliment with BOL, the #SQLHelp tag on twitter is very good for instant responses to problems. There are also a plethora of good advice in the form of blog entries over at SQLSkills.com which are really deep dives!
September 14, 2010 at 8:14 am
Thank you so much! 🙂 I guess I will get crackin on that script.
[font="Verdana"]Samjazz[/font]
September 16, 2010 at 4:02 am
Hi
Its very impotent that what type of query U will fire against the table 'Foo', on the basis of query type U may decide the which index is best for,
Ali
MCTS SQL Server2k8
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply