I am new in programming. A have a query which sometimes works but sometimes doesn't works at all. I mean if the result is not only a row it become too slow. If the result is several hundred rows it doesn't works at all. I know it's not the best to use 'like' in the query but I have no other options (or I just don't know). Please help me to optimize my query. I should run this query with different "where" options (the first part would be always the same). Shall I store the first part in a function? Does it make the query faster?
SELECT a.*,
group_concat(DISTINCT concat(' ', b.name)),
group_concat(DISTINCT concat(' ', e.name)),
group_concat(DISTINCT concat(' ', g.name))
FROM t1 a
LEFT JOIN t2 c
INNER JOIN t3 b ON c.id = b.id ON a.id = c.id
LEFT JOIN t4 d
INNER JOIN t5 e ON e.code = d.code ON a.id = d.id
LEFT JOIN t6 f
INNER JOIN t7 g ON g.code = f.code ON a.id = f.id
WHERE a.x LIKE '%5%'
GROUP BY a.id;
Thank you in advance!