0

我需要找到讲特定语言并且在有效位置或教师的主要位置不需要出现的可用位置的教师。多个教师可以在同一个位置。此查询有效,但速度很慢。我应该使用哪些索引?

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 != ""
4

1 回答 1

1

首先尝试这是否会给您带来更好的结果:

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

然后在非空字段上添加索引,看起来 tdb_teacher_languages.end_date 和 tdb_teacher_languages.language_id 是你最好的候选人。之后,您可以尝试在此处粘贴解释计划。

于 2013-10-11T20:45:01.793 回答