SQL Query Optimization in Mysql and Index

Asked

Viewed 188 times

6

Guys I’m having a performance problem at a consultation SQL in the MySQL who is using my server a lot, I’ve done index and yet consumption does not decrease. The query I’m using is:

SELECT
    CONCAT(
        '2015-10-14 ',
        Horas.Hora,
        ':00:00'
    ) AS HORA,
    COUNT(ID_CONTATO) AS TOTAL_GERAL
FROM
    (
        SELECT '00' Hora  UNION SELECT '01' UNION SELECT '02'
        UNION SELECT '03' UNION SELECT '04' UNION SELECT '05'
        UNION SELECT '06' UNION SELECT '07' UNION SELECT '08'
        UNION SELECT '09' UNION SELECT '10' UNION SELECT '11'
        UNION SELECT '12' UNION SELECT '13' UNION SELECT '14'
        UNION SELECT '15' UNION SELECT '16' UNION SELECT '17'
        UNION SELECT '18' UNION SELECT '19' UNION SELECT '20'
        UNION SELECT '21' UNION SELECT '22' UNION SELECT '23'
    ) Horas
LEFT JOIN CONTATO D ON SUBSTRING(D.CONTATO_DATA, 12, 2) = Horas.Hora
AND D.CONTATO_DATA LIKE '2015-10-14%'
AND CONTATO_CEP_BLOQUEADO IS NULL
GROUP BY
    CONCAT(
        '2015-10-14',
        Horas.Hora,
        ':00:00'
    )
ORDER BY 1

Have 325.000 registros in this table and this query is leading on average 7 segundos.

Fez index in all columns used and did not change anything the return time. If anyone knows any optimization I can do would be of great help. Thank you.

Follows the EXPLAIN of the result: http://sqlfiddle.com/#! 9/c034e/1/0

  • 7 seconds with the server running or just with this query, and you will run it more or less once a day, hour, minute ?

  • In operation every 5 minutes.

  • You can put a EXPLAIN before the SELECT, execute and include the result in the question? Thank you.

  • I don’t know about Mysql, but it’s not possible to do GROUP BY 1 ?

  • @bfavaretto already included the EXPLAIN on the question, take a look, thank you.

  • eliminates the points and tests with the table modified to Innodb or Myisam and compare the performance.

Show 1 more comment

2 answers

4

Your problem is at the points where Voce uses the function CONCAT and SUBSTRING. When you use this type of function as a search filter or grouping function, the Database cannot use the indexes for your queries and needs to do the TABLESCAN complete every time.

Adjust your query to not use these functions and you will see a significant performance improvement.

0

See if the query below helps.

select D.HORA, COUNT(D.idBalanceteVersao) AS TOTAL_GERAL from
(
    SELECT  '2015-10-14 '+ Horas.Hora +':00:00'  AS HORA, idBalanceteVersao
    FROM 
    (
        SELECT '00' as Hora UNION SELECT '01' UNION SELECT '02'
        UNION SELECT '03' UNION SELECT '04' UNION SELECT '05'
        UNION SELECT '06' UNION SELECT '07' UNION SELECT '08'
        UNION SELECT '09' UNION SELECT '10' UNION SELECT '11'
        UNION SELECT '12' UNION SELECT '13' UNION SELECT '14'
        UNION SELECT '15' UNION SELECT '16' UNION SELECT '17'
        UNION SELECT '18' UNION SELECT '19' UNION SELECT '20'
        UNION SELECT '21' UNION SELECT '22' UNION SELECT '23'
    ) Horas
    LEFT JOIN sgp.balancete_versao D 
    ON SUBSTRING(D.ultimaTransmissao, 12, 2) = Horas.Hora
    AND D.ultimaTransmissao LIKE '2015-10-14%'
    --
) D
GROUP BY D.HORA
ORDER BY 1
  • Hello thanks for the reply, but is giving error and I could not identify why. Você tem um erro de sintaxe no seu SQL próximo a 'declare @Horas table
(
 Hora varchar(2)
)

  • Ah, yes your database is mysql... I edited the answer see if it helps.

  • Error again Coluna 'Horas.Hora' desconhecida em 'field list

  • well I had to do a Join with a table from my bank to do the test... changes the table sgp.balancete_version to your table..

  • I did the test, ran but nothing changed the performance. Continues with 7 seconds of execution.

  • Try searching for "sql profiler" for mysql to see what’s weighing most in the query. if the case is the CONTACT table, see if it is necessary to use LEFT JOIN change the AND D.CONTATO_DATA LIKE '2015-10-14%' to filter by year, Month and day... year(CONTATO_DATA ) = 2015 ....

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.