简体   繁体   English

多个联接或子查询查询优化

[英]Multiple Join or subquery query optimization

I have this query, which executes in 1 or 2 seconds for a given case: 我有此查询,对于给定的情况,它将在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>

It counts the rows to build the pagination. 它计算行以建立分页。 When I introduced filtering by event type (types are related many to many to events) the query started taking no less than 45 seconds: 当我按事件类型(类型与事件多相关)进行过滤时,查询开始耗时不少于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>

I also tried a Join with event_type_to_event but it was even slower. 我还尝试了一个带有event_type_to_event的Join,但速度甚至更慢。
Any suggestions? 有什么建议么?

NOTE: Solved. 注意:已解决。 Using indices, the query execution time went down to less than a second. 使用索引,查询执行时间减少到不到一秒钟。

I suspect you need to add an index on the column event_type_id in table event_type_to_event, but if there is already an index there, then try the following: 我怀疑您需要在表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)

If Event_Id is the PK of table event_type_to_event you can also try a join instead of using Exists, 如果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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM