簡體   English   中英

多個聯接或子查詢查詢優化

[英]Multiple Join or subquery query optimization

我有此查詢,對於給定的情況,它將在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM