Question

I need to find teachers that speak a particular language and has future availability at a valid location OR the teacher's primary location which does not need to have availability to show up. Multiple teachers can be at the same location. This query works but is very slow. What indexes should I be using?

Select
  teachers.teacher_id, 
  teacherLocations.location_id
From
  tdb_teachers AS teachers
Join 
  tdb_teacher_languages As teacherLanguages
    On teacherLanguages.teacher_id = teachers.teacher_id And
       teacherLanguages.language_id = 33
Left Join
  tdb_availability As locAvail
    On locAvail.teacher_id = teachers.teacher_id And
       locAvail.end_date >= 1381449600
Join
  tdb_locations AS teacherLocations
    On (
      teacherLocations.location_id = locAvail.location_id Or
      teacherLocations.location_id = teachers.location_id
   ) And
   teacherLocations.latitude != "" And
   teacherLocations.longitude != ""
Was it helpful?

Solution

Firs try whether this gives you better results:

Select
  teachers.teacher_id, 
  teacherLocations.location_id
From
  tdb_teachers AS teachers
Join 
  tdb_teacher_languages As teacherLanguages
    On teacherLanguages.teacher_id = teachers.teacher_id 
Left Join
  tdb_availability As locAvail
    On locAvail.teacher_id = teachers.teacher_id And
Join
  tdb_locations AS teacherLocations
    On (
      teacherLocations.location_id = locAvail.location_id Or
      teacherLocations.location_id = teachers.location_id
   ) 
WHERE
   teacherLocations.latitude != "" 
   And teacherLocations.longitude != ""
   AND locAvail.end_date >= 1381449600
   And teacherLanguages.language_id = 33

Then add indexes on the non null fields, looks like tdb_teacher_languages.end_date and tdb_teacher_languages.language_id are your best candidates. After that you can try pasting over here an explain plan.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top