Solved

SQL Server Indexes

Posted on 2013-01-13
2
316 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

785 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