Solved

Creating Indexes-sql server 2012 web

Posted on 2014-12-11
6
86 Views
Last Modified: 2014-12-14
I have a site that displays nordic ski and cross-country running results and, in times of high volume, the results render slowly.  I would like to know how to optimize the query and set indexes.  We did this with my fitness events database but  now I need to do that with the cc meet database and I am not sharp enough to do it alone.  Here is a query to start with:
			sql = "SELECT r.LastName, r.FirstName, t.TeamName, r.RosterID, r.Gender, ir.RaceTime, ir.Excludes, ir.TeamPlace, ir.Bib "
            sql = sql & "FROM Roster r INNER JOIN Grades g ON r.RosterID = g.RosterID INNER JOIN IndRslts ir ON r.RosterID = ir.RosterID "
            sql = sql & "INNER JOIN Teams t ON t.TeamsID = r.TeamsID WHERE ir.RacesID = " & lRaceID & " AND ir.Place <> 0 AND ir.RaceTime <> '00:00' "
			sql = sql & "ORDER BY ir.Place"

Open in new window


What do you need to know about the tables in order to help me?  Sorry for asking a similar question but I just am having a hard time bridging the two.
0
Comment
Question by:Bob Schneider
6 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 167 total points
ID: 40494473
i do not believe in this situation its about changing the syntax of the query you listed above. It looks pretty tight.
 Its about optimizing the tables by creating indexes on the tables.


it doesn't take long at all :)
http://msdn.microsoft.com/en-us/library/ms186342.aspx




http://msdn.microsoft.com/en-us/library/ms190457.aspx

and a great video
https://www.youtube.com/watch?v=ITcOiLSfVJQ
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40494478
Sorry use the top link first :)

i do not believe in this situation its about changing the syntax of the query you listed above. It looks pretty tight.
 Its about optimizing the tables by creating indexes on the tables.


it doesn't take long at all :)
http://msdn.microsoft.com/en-us/library/ms186342.aspx




http://msdn.microsoft.com/en-us/library/ms190457.aspx

and a great video
https://www.youtube.com/watch?v=ITcOiLSfVJQ
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 167 total points
ID: 40494561
The critical thing is to get the best clustered index on each table.  Everything else with indexes is secondary to that.

Iow, you get the clustering right, you'll get at least decent performance overall no matter what else you do.  Get the clustering wrong, you'll get poor performance overall until/unless you create lots of covering indexes.


Based on the very limited info so far, you could cluster the tables as follows:

Cluster keys for tables:
Roster  by  RosterID
Grades  by  RosterID --maybe an additional column, unknown at this time
--can't tell for sure, likely the first but maybe the second:
IndRslts  by  ( RacesID, RosterID ) OR ( RosterID, RacesID )
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 166 total points
ID: 40494609
check these indexes first...

Roster > RosterID
Grades > RosterID
IndRslts > RosterID, RacesID
Teams > TeamsID
0
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40495671
Would help if you can post the structure of the tables and respective PK, FK and Indexes. The Query Execution Plan would be useful as well.
At least you need to have indexes created on FK's and on columns used in the WHERE clause as well in the ORDER clause.

Assuming that Place and RaceTime can only have positive values I would change the '<>' to '>', like this:
			sql = "SELECT r.LastName, r.FirstName, t.TeamName, r.RosterID, r.Gender, ir.RaceTime, ir.Excludes, ir.TeamPlace, ir.Bib "
            sql = sql & "FROM Roster r INNER JOIN Grades g ON r.RosterID = g.RosterID INNER JOIN IndRslts ir ON r.RosterID = ir.RosterID "
            sql = sql & "INNER JOIN Teams t ON t.TeamsID = r.TeamsID WHERE ir.RacesID = " & lRaceID & " AND ir.Place > 0 AND ir.RaceTime > '00:00' "
			sql = sql & "ORDER BY ir.Place"

Open in new window

0
 

Author Comment

by:Bob Schneider
ID: 40498854
I guess I just need to learn how to create indexes.  I have PK-FK constraints in place and it sounds like my query is good so I will go ahead and learn this before posting back.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Query - Return results based on minimum value 8 32
SQL Error - Query 6 26
Alternative of IN Clause in SQL Server 3 21
Sql Query 6 67
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

803 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