[Solved] Sql query performance is low

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `investors_positivador` ADD INDEX `investors_positiva_idx_cod_cliente` (`cod_cliente`);
The optimized query:
SELECT
        investors_positivador.cod_cliente,
        investors_saldo_financeiro.nome_cliente,
        investors_base_assessores.squad,
        investors_base_assessores.nome_investor,
        investors_saldo_financeiro.saldo_d0,
        SUM(CASE 
            WHEN investors_posicao_geral.vencimento <= @vencimento THEN investors_posicao_geral.financeiro 
            ELSE 0 END) AS vencimentos_ate_data,
ROUND(SUM(CASE 
    WHEN investors_diversificacao.produto = 'Fundos' THEN investors_diversificacao.net / 6 
    ELSE 0 END),
2) AS fundos_ate_data,
investors_guia_fundos.liquidez_total,
investors_positivador.contatar_liquidity_map,
investors_positivador.id,
investors_posicao_geral.vencimento,
investors_base_assessores.nome_assessor 
FROM
investors_positivador 
INNER JOIN
investors_saldo_financeiro 
    ON investors_positivador.cod_cliente = investors_saldo_financeiro.cod_cliente 
INNER JOIN
investors_base_assessores 
    ON investors_base_assessores.cod_assessor = investors_base_assessores.cod_assessor 
INNER JOIN
investors_posicao_geral 
    ON investors_positivador.cod_cliente = investors_posicao_geral.cod_cliente 
LEFT OUTER JOIN
investors_diversificacao 
    ON investors_positivador.cod_cliente = investors_diversificacao.cod_cliente 
LEFT OUTER JOIN
investors_guia_fundos 
    ON investors_diversificacao.cnpj = investors_guia_fundos.cnpj 
WHERE
(
    investors_base_assessores.nome_investor = @investor_nome
) 
AND (
    investors_saldo_financeiro.saldo_d0 > 0
) 
OR (
    investors_base_assessores.nome_investor = @investor_nome
) 
AND (
    investors_posicao_geral.financeiro > 0
) 
OR (
    investors_base_assessores.nome_investor = @investor_nome
) 
AND (
    investors_diversificacao.net > 0
) 
GROUP BY
investors_positivador.cod_cliente 
ORDER BY
NULL

Related Articles



* original question posted on StackOverflow here.