我有此查询,对于给定的情况,它将在1或2秒内执行:

    Select Count(*) as qtty
    From event e
    Join org o On o.orgID = e.orgID
    Join venue v On v.venueID = e.venueID
    Where Match( e.name, e.description ) Against ( $keywords )
        And e.site_id = $site_id
        And e.display <> 0</code>


它计算行以建立分页。当我按事件类型引入过滤时(类型与事件有很多关联),查询开始耗时不少于45秒:

    And Exists (
      Select ete.id
      From event_type_to_event ete
      Where ete.event_id = e.eventID
      And ete.event_type_id = $category )</code>


我还尝试了一个带有event_type_to_event的Join,但速度甚至更慢。
有什么建议么?

注意:已解决。使用索引,查询执行时间减少到不到一秒钟。

最佳答案

我怀疑您需要在表event_type_to_event的event_type_id列上添加索引,但是如果那里已经有索引,请尝试以下操作:

Select Count(*) as qtty
From event e
   Join org o On o.orgID = e.orgID
   Join venue v On v.venueID = e.venueID
Where Match( e.name, e.description ) Against ( $keywords )
   And e.site_id = $site_id
   And e.display <> 0
   And Exists
      (Select * From event_type_to_event
       Where event_id = e.eventID
          And event_type_id = $category)


如果Event_Id是表event_type_to_event的PK,那么您也可以尝试联接而不是使用Exists,

Select Count(*) as qtty
From event e
   Join org o On o.orgID = e.orgID
   Join venue v On v.venueID = e.venueID
   Join event_type_to_event t
       On t.event_id = = e.eventID
Where Match( e.name, e.description ) Against ( $keywords )
   And e.site_id = $site_id
   And e.display <> 0
   And t.event_type_id = $category

07-24 19:41
查看更多