Query Optimization - Mysql

Asked

Viewed 40 times

-1

Greetings to you, gentlemen! I come once again to seek the guidance of the masters of DB! As the last time I have a query that returns a very specific result and that works,a but is slow and as the bank grows, slower becomes. Follow the model below.

SELECT DISTINCT V1_PAIS_NOME_PT AS PAIS,
   (SELECT COUNT(*) FROM `TV1-VISITAS` WHERE V1_PAIS_NOME_PT = PAIS) AS TOTAIS,
   (SELECT COUNT(DISTINCT V1_IP) FROM `TV1-VISITAS` WHERE V1_PAIS_NOME_PT = PAIS) AS UNICOS

FROM `TV1-VISITAS` WHERE V1_OS NOT LIKE '%bot%'
    AND MONTH(V1_HORA) = MONTH(NOW())

ORDER BY TOTAIS DESC;

Thank you very much for your attention!

1 answer

0

Guys, I thank those who visualized even if they were not able to answer for any reason. I managed to solve and I will post here the solution for those who have the same problem can solve. Thing beyond simple, pure lack of attention... Rsrs

SELECT V1_PAIS_NOME_PT AS PAIS,
    COUNT(V1_PAIS_NOME_PT) AS TOTAIS,
    COUNT(DISTINCT V1_IP) AS UNICOS
FROM `TV1-VISITAS`
WHERE V1_OS NOT LIKE '%bot%'
    AND MONTH(V1_HORA) = MONTH(NOW())
GROUP BY PAIS
ORDER BY TOTAIS DESC;

Instead of making a new SELECT within each query, will only count according to the instructions using the data already recovered without the need to make a SELECT inside the other.

Thanks, guys! till next time!

Browser other questions tagged

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