Bring all the necessary results in a query - MariaDB

0

I need to reformulate a small system, which works now but opens several connections to the database, and when there are many records, the database crashes, or when this does not happen, the page gets very slow. >

So to get around this problem, I'm working on a query that brings me all the results all at once, and then I treat this information from the PHP side to improve performance.

I'm just having a small problem: the table, which I'll call test, can have multiple relationships with test2 or test3, so I've created an item_test table to list them, but the test2 table information repeats itself when I run the query.

Here's an example in SQL Fiddle

This is the query I'm using:

select distinct t1.teste, 
GROUP_CONCAT(t2.teste2 SEPARATOR '&') teste2 ,
GROUP_CONCAT(t3.teste3 SEPARATOR '&') teste3 
FROM item_teste it
join teste t1 on t1.id_teste = it.id_teste
join teste2 t2 on t2.id_teste2 = it.id_teste2
join teste3 t3 on t3.id_teste3 = it.id_teste3
where t1.teste like '%testando%'
group by t1.id_teste;

I do not know if this is the best way to optimize, so is there any other way to optimize queries or am I on the right track?

    
asked by anonymous 27.04.2017 / 19:22

1 answer

1

In order not to repeat test information2 (I did not test it but I believe it should work) add distinct ex: group_concat(distinct t2.teste2 SEPARATOR '&')

    
01.05.2017 / 15:35