Solved

SQL Server Indexes

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

751 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