Solved

Creating Indexes-sql server 2012 web

Posted on 2014-12-11
6
85 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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
Via a live example, show how to shrink a transaction log file down to a reasonable size.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now