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 ?
– Tiago Oliveira de Freitas
In operation every 5 minutes.
– MoisesGama
You can put a
EXPLAIN
before theSELECT
, execute and include the result in the question? Thank you.– bfavaretto
I don’t know about Mysql, but it’s not possible to do
GROUP BY 1
?– Guilherme Lautert
@bfavaretto already included the
EXPLAIN
on the question, take a look, thank you.– MoisesGama
eliminates the points and tests with the table modified to Innodb or Myisam and compare the performance.
– denis