我怎样才能提高这个查询的性能?

How can I improve the performance of this query?

在这个查询中,我创建了一个 "left join" 和一个 table (ASPECT_CONTACT_EVENTS),它有很多寄存器(在此期间,250.000)。

table Promessas 在此期间有 200 个寄存器。

SET @inicio = '2015-02-24 00:00:00';

SET @fim = '2015-02-24 23:59:00';

SELECT Promessas.Data_acordo, if(Ativos.account_number IS NULL,
IF(Promessas.tipo_acordo='A VISTA',"11-QTDE DE PROMESSAS A VISTA (RECEPTIVO)","12-QTDE DE PROMESSAS PARCELADO (RECEPTIVO)"),
IF(Promessas.tipo_acordo='A VISTA',"05-QTDE DE PROMESSAS A VISTA (ATIVO)","06-QTDE DE PROMESSAS PARCELADO (ATIVO)"))  AS TIPO,

COUNT(DISTINCT Promessas.id_contr), Promessas.cluster FROM DWH_RBZ.tb_itau_banco_promessa_dia as Promessas
left join DWH_RBZ.ASPECT_CONTACT_EVENTS AS Ativos on ((Ativos.account_number = Promessas.id_contr) and (time_of_contact between (@inicio) and (@fim)) )
Where Promessas.data_acordo between (@inicio) and (@fim)
Group By tipo, Promessas.cluster;

我怎样才能提高这个性能?

优化器不能很好地处理@variables。请将值直接放入查询中,看看是否可以加快查询速度。

否则,Promessas需要一个以data_acordo开头的索引,希望WHERE子句可以使用该索引。第二个table,Ativos需要复合INDEX(account_number, time_of_contact),假设times_of_contact在那个table里面。请在所有字段上使用别名!