User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 423,758 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,253 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser: Programming Forums
Views: 438 | Replies: 1 | Solved
Reply
Join Date: Oct 2007
Location: South Africa
Posts: 60
Reputation: RoryGren is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
RoryGren's Avatar
RoryGren RoryGren is offline Offline
Junior Poster in Training

Help with speeding up a query with included sub-queries

  #1  
Mar 25th, 2008
Hi all

The following query works and gives me the desired results. The only problem is that it is very slow, and I suspect it is because of the multiple sub-queries included in it.

The table, RESULT, has multiple records per athlete, per stroke, per distance stored in the order of the events and not in rank order. I am pulling out the top time for each athlete for the selected stroke/distance/age/gender combination and ranking the result. ... In other words, I'm selecting the relevant swimmers' top times for a selected event and ranking those.

Variables are passed to the script - Selected Athlete_Id - 1234 - to generate age and gender for ranking - stroke_id and distance. These are already included in the code below.

The query returns 45 rows, which is correct, and, according to my SQLYog, it takes, on average, 14 seconds to process...

Any suggestions?

Thanks very much!

Rory

  1. select a.ATHLETE,
  2. rtrim(a.LAST) as Surname,
  3. rtrim(a.FIRST) as FirstName,
  4.  
  5. (select z.SCORE from RESULT z
  6. where z.ATHLETE = r.ATHLETE
  7. and z.STROKE = 1
  8. and z.DISTANCE = 50
  9. and z.SCORE > 0
  10. and z.COURSE = 'L'
  11. order by z.SCORE limit 0,1) as SCORE, -- Selects top time for selected event & dist.
  12.  
  13. (select m.MNAME from RESULT w
  14. Join MEET m on (w.MEET = m.MEET)
  15. where w.ATHLETE = r.ATHLETE
  16. and w.STROKE = 1
  17. and w.DISTANCE = 50
  18. and w.SCORE > 0
  19. and w.COURSE = 'L'
  20. order by w.SCORE limit 0,1) as SWMEET -- Selects gala where the time was achieved
  21.  
  22. from RESULT r
  23. Join ATHLETE a on (a.ATHLETE = r.ATHLETE)
  24. Join TEAM t on (t.TEAM = a.TEAM1)
  25. Join stroke s on (r.STROKE = s.stroke_id)
  26. WHERE a.AGE = (select AGE from ATHLETE where ATHLETE = 1234)
  27. and a.SEX = (select SEX from ATHLETE where ATHLETE = 1234)
  28. and r.COURSE = 'L'
  29. and r.STROKE = 1
  30. and r.DISTANCE = 50
  31. and r.SCORE > 0
  32. and t.LSC = 'KZ'
  33. Group by r.ATHLETE
  34. Order by SCORE
  35.  
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: South Africa
Posts: 60
Reputation: RoryGren is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 7
RoryGren's Avatar
RoryGren RoryGren is offline Offline
Junior Poster in Training

Re: Help with speeding up a query with included sub-queries

  #2  
Mar 26th, 2008
Any suggestions?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb MySQL Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the MySQL Forum

All times are GMT -4. The time now is 1:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC