1
Hey there, guys! I have this query (Mysql) to count amount of visits on a Mê in a given period, however it takes more than 1 minute to run. How can I optimize it? Thanks in advance.
SELECT MONTH(V1_DATA) AS MES,
YEAR(V1_DATA) AS ANO,
(SELECT COUNT(*) FROM `TV1-VISITAS` WHERE
(V1_PG LIKE '%download%'
OR V1_PG LIKE '%home%'
OR V1_PG LIKE '%empresa%'
OR V1_PG LIKE '%onde_comprar%'
OR V1_PG LIKE '%eventos%'
OR V1_PG LIKE '%contato%'
OR V1_PG LIKE '%produtos%'
OR V1_PG LIKE '%representantes%')
AND YEAR(V1_DATA) = ANO
AND MONTH(V1_DATA) = MES) AS INSTITUCIONAL,
(SELECT COUNT(*) FROM `TV1-VISITAS` WHERE V1_PG LIKE '%evento%' AND YEAR(V1_DATA) = ANO AND MONTH(V1_DATA) = MES) AS EVENTOS,
(SELECT COUNT(*) FROM `TV1-VISITAS` WHERE V1_PG LIKE '%produto%' AND YEAR(V1_DATA) = ANO AND MONTH(V1_DATA) = MES) AS PRODUTOS,
(SELECT COUNT(*) FROM `TV1-VISITAS` WHERE V1_PG LIKE '%download%' AND YEAR(V1_HORA) = ANO AND MONTH(V1_DATA) = MES) AS ARQUIVOS
FROM `TV1-VISITAS` WHERE YEAR(V1_DATA) = YEAR(NOW()) GROUP BY MES ORDER BY MES
Try trading the selects for CASE https://stackoverflow.com/questions/20320631/how-to-pivot-a-table-in-mysql-using-case-statements
– Motta