Solved

SQL Server Indexes

Posted on 2013-01-13
2
313 Views
Last Modified: 2013-01-13
Greetings,

I am using visual studio queries to manage a SQL database.


If a table has the following fields:

1. SIID  <-- Identity and Primary Field
2. StudentID
3. ExamID
4. ClassID
5. Grades

Some inquiries will be grouped on StudentIDs and their grades, while others will be grouped on ClassIDs and the grades.

How should the non-clustered indexes be created?

Should I crate one index
StudentID
ExamID
ClassID
Grade

or should I create separate indexes, ie

Index 1:        a join used to get student Name
StudentId
Grade

Index 2:      a join used to get Class name AND student name
ClassID      
Grade

Index 3:      a join used to get Student and Exam name
ExamID      
Grade:

Basically, I guess what I am asking is:  Should an index be created for each separate query? Will the SQL server know which index to use?

V Whitehead
0
Comment
Question by:vaughnwhitehead
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 300 total points
ID: 38772263
There's a lot to consider here, so the answer is "it depends".

The optimizer is a clever beast (some would argue with that at times) but based upon the size of your table and the distribution of the data, sometimes it decides that even though an index is there that it's faster to do a scan of the clustered index to get the data you need.

In a non clustered index, the leaf node of the index contains a pointer to the clustered index key for the row being searched (it works slightly differently for a heap, but you seem to indicate the use of the clustered index).  So, what's really happening is 2 I/Os for a read on the index - reading the leaf node to get the clustered key and then reading the page from the clustered index.

While that may be expanding a little too much, it's important to understand for those times that you're pulling other columns in the query other than the columns in the index (because the columns defined in the index definition are stored in the leaf nodes of the index itself).  If you're just pulling data from the non clustered index, that data is stored on the index pages so a second lookup on the clustered index key doesn't happen.

You'd really need to play with it based upon the volume of your data.

You also need to consider the extra overhead of multiple indexes on inserts/updates/deletes - because it's not just the base table you have to update, it's the indexes as well (although two non clustered indexes doesn't seem like overkill to me).

After running a load for a while with typical queries on your tables, you can also refer to the DMV's to get an understanding of what the database engine considers to be missing indexes.

I'd suggest loading your data, update your statistics, running some queries, looking at the actual execution plans and seeing what the optimizer is doing with your queries.

On the whole though, to me it doesn't seem like a bad idea as a starting point if those queries are going to be common.

BOL Entry on Nonclustered Indexes for your reference: http://msdn.microsoft.com/en-us/library/ms177484.aspx
0
 

Author Closing Comment

by:vaughnwhitehead
ID: 38772482
Thanks

I appreciate your input; good info

V Whitehead
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now