1
At a time, I developed a page of reports, users have an option of filters, this working, but as the data in the database are growing this getting slower and do not know what is the best option to solve this problem, some of the things I thought were:
If it is always better to bring the data already calculated by the query or if in some cases it is better to return all the data, and make these calculations with use of php, for example?
Am I creating the proper querys? What I mean by that, I am doing the accounts in several sub-querys, follows an example:
SELECT DISTINCT t.nome_tag, ( SELECT count(vw.tag_id) FROM view_relatorio vw WHERE vw.tag_id = t.id AND vw.cliente_id IN (1,2,3,10,20) AND vw.conforme = 1 AND vw.dt_validate >= '2016-09-09' ) as conforme, ( SELECT count(vw.tag_id) FROM view_relatorio vw WHERE vw.tag_id = t.id AND vw.cliente_id IN (1,2,3,10,20) AND vw.conforme = 0 ) as naoConforme, ( SELECT count(vw.tag_id) FROM view_relatorio vw WHERE vw.tag_id = t.id AND vw.cliente_id IN (1,2,3,10,20) ) as totalAtribuido FROM view_relatorio view JOIN tag t ON t.id = view.tag_id WHERE view.cliente_id IN (1,2,3,10,20)
Remembering that this is a small example and has several other querys.
I have built views to improve, as I read in some places, I have also indexed columns in the necessary tables, finally, as to improve the part of the database I have made several adjustments.
face makes a routine to generate the result per day and keeps in a view in mysql dai vc so le esta view , the processing will be much better
– Jasar Orion
this was an option I also thought, I’ve used this in another system, but in this case, want the data in real time, but thank you very much for the idea.
– Marcelo Diniz
A suggestion, the first step is to measure the time qto takes the query, the second I would do an experiment would exchange these sub-consultations for 3 queries and in the end would make their Union now improves performance just by measuring. Don’t forget the commands Analyze and explain they can help identify bottlenecks in consultations.
– rray
I may be mistaken, but it seems that you are trying to get the data in repeated ways for each item of the internal loop, when you could resolve this in the
JOIN
and in PHP would group the data.– Guilherme Nascimento
I’m going to try to do this test @rray commented on, but from what I’m observing in other querys that I have, it doesn’t seem to get much better, but it doesn’t hurt to test.
– Marcelo Diniz
I also need to test what @Guilhermenascimento commented, as it may be a solution, although a little more laborious now, but it’s an option.
– Marcelo Diniz