tags:

views:

28

answers:

1

So this was a small site that got extremely popular very fast and now and im having major problems with the below sql query.

I understand that my DB design is not great. I have text field for subjects and programs witch contains a serialized array and i search it using like.

the below query takes about a minute.

SELECT p.*, e.institution
FROM   cv_personal p
LEFT   JOIN cv_education e
ON     p.id = e.user_id
LEFT   JOIN cv_literacy l
ON     p.id = l.user_id
WHERE  1 = 1
AND    (e.qualification LIKE '%php%' OR e.subjects LIKE '%php%' OR l.programs LIKE '%php%')
GROUP  BY p.id
ORDER  BY p.created_on DESC
A: 

What an EXPLAIN show ? I think you can add conditions to a join to reduce number of records which are used :

SELECT p.*, e.institution
FROM   cv_personal p
LEFT   JOIN cv_education e
ON     (e.qualification LIKE '%php%' OR e.subjects LIKE '%php%') AND p.id = e.user_id
LEFT   JOIN cv_literacy l
ON     l.programs LIKE '%php%' AND p.id = l.user_id
ORDER  BY p.created_on DESC

And why do you use GROUP BY ?

MatTheCat
Users can have many educational records so i use the group so it only retrieves one distinct row. if that makes any sense.below is the explain.http://typhon.svo.co.za/explain.jpg
Lylo
You won't obtain all educational records with this GROUP BY, only one. You should remove it and use PHP to display : http://stackoverflow.com/questions/3916164/how-separate-search-results-by-category-mysql-php/3916215#3916215
MatTheCat
Thanks for the help matthecat but when i take the `group by` out of the statement it still takes about a minute.
Lylo
Even changing JOIN condition like in my post ?
MatTheCat
I get the following error with your query `Cross dependency found in OUTER JOIN; examine your ON conditions`
Lylo
I've edited the query, I think it should work.
MatTheCat