2016-07-27 109 views
0

我們有以下種類統計的一個相當大的MySQL的InnoDB表,MySQL數據範圍查詢優化

Rows - 8277943+      Server Ram - 128 Gb 
Avg row Length - 575    Processor - Deca Core Intel 
Data Size - 4.4 GB     Overall Database Size - 500Gb 

的問題是,我們有幾個查詢此表上,他們現在正在運行很慢,正在運行出於想法優化。例如下面給出一些查詢,

select TIMESTAMPDIFF(SECOND,w.msgCreatedOn,now()) as rnge 
from t_xxx_dtls w 
where w.profileId=17901 
    and w.orgId=1448 
    and w.actionStartDate BETWEEN '2016-07-27 05:08:00' and '2016-07-27 13:08:59' 
    and w.currentlyActive=true and w.`action` not in (6,9,17) 
    and (
    w.parentId NOT in (
     select CASE WHEN d.parentId IS NOT NULL THEN d.parentId ELSE d.id END as ticketId 
     from t_xxx_dtls d where d.profileId=17901 and d.orgId=1448 and d.actionStartDate  BETWEEN '2016-07-27 05:08:00' and '2016-07-27 13:08:59' and d.action in (2,4,7) 
     group by ticketId 
    ) 
    or (w.parentId is null and w.inReplyId is null) 
    ) 
    and w.msgId is not null 
order by rnge desc 
limit 0,1 

解釋計劃Explain Plan

雖然我們有簡單的查詢,如下面的一個,它開始被卡住,

select count(*) 
from t_xxx_dtls d 
where d.actionStartDate BETWEEN '2016-07-27 05:08:00' and '2016-07-27 13:08:59' 

這根據解釋計劃正在檢查大量的記錄,這怎麼能改進。我正在閱讀有關分區的信息,但現在確定它是否有幫助。

+1

請顯示錶格結構。你有一個關於actionStartDate的索引嗎? – rlanvin

+0

請顯示mysql版本,創建索引,oltp或olap數據庫,... –

回答

0

對於顯示的第一個查詢,我認爲按照的順序((orgId,profileId,actionStartDate)上的索引將是最有幫助的。

然而,它不會幫助簡單的第二個查詢;那個人可以從一個以actionStartDate開頭的索引中受益。第一個查詢也可以從這樣的索引中受益,但是我不確定MySQL是否會在使用actionStartDate BETWEEN條件時利用(actionStartDate,orgId,profileId)索引的後面部分。