2016-07-27 111 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)索引的后面部分。