2016-07-05 77 views
2

請告知如何改善這種緩慢的查詢。緩慢查詢與OR在哪裏條款

SELECT response.`reasonid` 
FROM response 
    INNER JOIN ACTION ON action.actionid = response.actionid 
WHERE 
    response.respdate BETWEEN 20160305 
    AND 20160905 
    AND 
    (
     (
     response.reasonid = 'Prospect Call' 
     AND response.typeid = '0' 
     AND action.typeid = '9' 
     ) 
    OR 
     (
     response.typeid = '1000' 
     AND action.typeid = '1' 
     ) 
    ) 

上有索引:

response.actionid/response.reasonid/response.typeid/action.typeid/response.respdate

解釋結果:

table type possible_keys     key   key_len ref    rows Extra 

ACTION range PRIMARY,idx_actiontypeid idx_actiontypeid 5  \N    310617 Using where; Using index 
response ref idx_respdate2,idx_actionid, idx_actionid  5  ACTION.actionid 1   Using where 
      idx_reasonid,idx_resptypeid 
+0

什麼數據類型是respdate?它是一個日期,日期時間,整數? 「慢」是什麼意思? 1秒? 1分鐘? – Olli

+0

respdate是一個日期字段。慢6秒。 – zima10101

回答

0

你的查詢不應該有它的單獨索引,但一個複合或甚至覆蓋索引。我很驚訝沒有人評論過。我爲指標

表索引 響應(typeid的,respdate,合理,actionid) 行動(actionid,typeid的)

後來我調整了查詢​​使用UNION代替OR

以下建議
select distinct 
     r.reasonid 
    from 
     response r join action a 
     on r.actionid = a.actionid 
     AND a.typeid = 9 
    where 
      r.typeid = 0 
     and r.respdate between 20160305 and 20160905 
     and r.reasonid = 'Prospect Call' 
union 
select 
     r.reasonid 
    from 
     response r join action a 
     on r.actionid = a.actionid 
     AND a.typeid = 1 
    where 
      r.typeid = 1000 
     and r.respdate between 20160305 and 20160905 
+0

嗨,好主意明顯更快。 – zima10101

0

試此查詢添加了一些索引列加入

SELECT response.`reasonid` 
FROM response 
    INNER JOIN ACTION ON action.actionid = response.actionid and response.typeid in('1000','0') and action.typeid in('0','1') 
WHERE 
    response.respdate BETWEEN 20160305 
    AND 20160905 
    AND 
    (
     (
     response.reasonid = 'Prospect Call' 
     AND response.typeid = '0' 
     AND action.typeid = '9' 
     ) 
    OR 
     (
     response.typeid = '1000' 
     AND action.typeid = '1' 
     ) 
    ) 
+0

請執行解釋此查詢 –

+0

謝謝Manesh - 但沒有改變查詢時間。上面已經添加了解釋。 – zima10101

0

試一下: 減少表下來的大小,只需要使用什麼:

select a.* 
from (
     SELECT response.`reasonid` 
     FROM response 
     WHERE response.respdate BETWEEN 20160305 AND 20160905 
    ) a 
INNER JOIN ACTION ON action.actionid = a.actionid 
WHERE (
    response.reasonid = 'Prospect Call' 
    AND response.typeid = '0' 
    AND action.typeid = '9' 
    ) 
OR 
    (
    response.typeid = '1000' 
    AND action.typeid = '1' 
    ) 
+0

謝謝,但仍然很慢 – zima10101

+0

但它是一個改進? – kostas

+0

不這麼認爲 - 由於緩存查詢運行速度比較快;) – zima10101

0

請儘量在查詢中指定的日期,因爲它們是在數據庫:

SELECT response.`reasonid` 
FROM response 
    INNER JOIN ACTION ON action.actionid = response.actionid 
WHERE 
    response.respdate BETWEEN "2016-03-05" 
    AND "2016-09-05" 
    AND 
    (
     (
     response.reasonid = 'Prospect Call' 
     AND response.typeid = '0' 
     AND action.typeid = '9' 
     ) 
    OR 
     (
     response.typeid = '1000' 
     AND action.typeid = '1' 
     ) 
    ) 

並檢查這是否加快您的查詢。 如果將其編寫爲數值(20160305),則數據庫在比較每行時必須對每行執行隱式類型轉換,這可能會導致性能下降。

+0

更改日期沒有幫助,但TQ – zima10101