Solved

SQL Server Indexes

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrading to SQL Server 2015 Express 2 29
sql query 5 38
c#, case, if 4 16
getting error while running below query  in sql 2 13
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

685 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