Optimizing a Query

Here is a SQL Server 2012 query.  Is there a way that it could be re-structured to perform better?

SELECT pr.Bib, p.LastName, p.FirstName, p.Gender, pr.Age, ir.ChipTime, ir.FnlTime, ir.ChipStart, p.City, p.St
FROM Participant p JOIN IndResults ir ON p.ParticipantID = ir.ParticipantID
JOIN PartRace pr ON pr.RaceID = ir.RaceID AND pr.ParticipantID = p.ParticipantID
WHERE ir.RaceID = " & lRaceID & " AND ir.FnlTime IS NOT NULL AND ir.FnlTime > '00:00:00.000' AND p.Gender = '" & sGender & "' ORDER BY " & sOrderBy
Bob SchneiderCo-OwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
Maybe cluster the IndResults table on ( RaceID, ParticipantID ),
and cluster the RacePart table also on ( RaceID, ParticipantID ).  

That would certainly work well for queries where the RaceID is specified.
HuaMin ChenProblem resolverCommented:
Ensure you're using the relevant tables' indexes when joining the tables.
Vitor MontalvãoMSSQL Senior EngineerCommented:
This might be hard:
ORDER BY " & sOrderBy
You're ordering dynamically and that means you can't find the best index for this part of the query.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Bob SchneiderCo-OwnerAuthor Commented:
I have the clustered indexes.  I don't think I can get around the order by issue because of the different views that people request.  Is the structure of the query optimal?  Would changing the order of the fields and/or the joins matter at all?
Vitor MontalvãoMSSQL Senior EngineerCommented:
the order of the fields and/or the joins matter at all?
The order of fields should match the order of the indexes. The order of JOINS shouldn't matter.
Do you really need the ORDER BY or the sort can be made in the client side?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob SchneiderCo-OwnerAuthor Commented:
How could I make the sort on the client side?
Vitor MontalvãoMSSQL Senior EngineerCommented:
In your application. You let SQL Server return the data and then use the component (datagrid or whatever you're using) to sort it.
Scott PletcherSenior DBACommented:
I have the clustered indexes.

You have those specific clustered indexes on those tables on ( RaceID, ParticipantID )?

Then I don't see anything else you can do to easily speed up this query.
Bob SchneiderCo-OwnerAuthor Commented:
VItor, yeah I am using a simple html table to display the data.  Ultimately I should incorporate jquery but for now...

Thanks for all of the help.  And, yes I do have clustered indexes on those table fields so it is probably as good as it can get...  We will be timing roughly 3000 runners on July 4th and they will all be looking for their results via results emails that we send upon finishing so I am just trying to avoid a web crash.  We use SQL Server 2012 Web on our server.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.