Could you please help me in optimizing this mysql query or any other way of writing this mysql query, it runs but it takes too much time.
SELECT DISTINCT
A.item1,
A.item2,
A.item3,
FROM tableAA AS A
INNER JOIN(tableBB AS B)
ON(A.item4 = B.item4)
INNER JOIN(tableCC AS C)
ON(A.item4 = C.item4)
INNER JOIN(tableDD AS D)
ON(A.item4 = D.item4)
WHERE (B.item5 = '$selected1' AND
B.item6 LIKE '%$selected2%' AND
C.item7='$selected3' ) OR
(A.item8 LIKE '%$selected2%' AND
D.item5 = '$selected1' AND
C.item7='$selected3')
Is there any other way of writing this query?
edit: tableAA & tableBB & tableCC contains billions of entry, i think i have index the tables correctly.
edit2: But i think the problem is not in Like % condition. I have switched it off and again run the query. but it is still taking long like 565 seconds.
explainfor the query to provide the information about what it is doing inefficiently.EXPLAINor get an execution plan, put the results in the question as well.