Solved

SQL Server Indexes

Posted on 2013-01-13
2
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

632 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