SQL Server Indexing – Considerations for Adding Indexes to a SQL Server Database

clustered-indexsql serverstatistics

I'm a .NET developer. So, I know enough about databases to be dangerous, but do not have the expertise of a proper DBA.

We have a slow running stored proc and one of the devs here ran a query optimization tool which suggested the addition of some indexes.

It suggested 9 Non-clustered indexes and 78 statistics.

I know this is a pretty huge area and I know enough to know that I don't know much. So, given that I don't have access to a proper DBA to review the indexes and statistics, would it be a reckless move to just proceed with those suggested changes?

Rewriting the Stored Proc is another option we are not too keen on, as it is returning the correct data right now and we are reticent to mess with that.

But if there is a big gotcha that I am unaware of, I'd really like to find out about it. So, advice would be greatly appreciated.

I was going to post the most expensive query plan of the batch to Pastetheplan.com . Alas, I was unable to get permission from my manager. I can't see how a bit of random SQL will have any security implications etc., but the business is more paranoid than me. I realise this would have helped a lot and am disappointed I cannot post to it. The 1 index I added has helped a lot and the advice here has been much appreciated. Choosing an answer is tough and I'm still working on that.

Best Answer

I dont think you can ignore the stored proc. This it ultimately query tuning. Queries use indexes. Indexes support queries. They go together. There is a limit to how much indexes can help a bad query.

You cant do too much harm adding a few indexes judiciously. However if you add too many indexes you could make performance even worse.

I would start by ensuring your current indexes and statistics are maintained and in good shape. If not you should start there. You may get a noticeable improvement just from rebuilding your indexes.

Next I would look at the queries in your stored proc and assess if they would benefit by adding or modifying indexes. As mentioned by others I would focus on covering indexes that support joins and filtering.

Dont go crazy, just start with a couple, test and review. Look at the execution plan and focus on the tables and functions that have the highest "cost".

If there are several queries within the same sp I believe you should consider them together. You may find one covering index may benefit several queries.

Keep in mind that different query types may benefit from different types of indexes or indexes on different columns depending upon what they do. For example a query designed to return a single row vs a query attempting to aggregate data.

Edit: Your comment about the reccomendation to create 78 statistics makes me suspicious that something isnt right there. This article about auto_create_statistics & auto_update_statistics might be of use to you.