Solved

Creating Indexes-sql server 2012 web

Posted on 2014-12-11
6
84 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:
ScottPletcher 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 45

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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

20 Experts available now in Live!

Get 1:1 Help Now