Home » Performance » MySQL » Optimizing a search query
Optimizing a search query [message #1553] Wed, 25 July 2007 06:54 Go to next message
kaahbonk  is currently offline kaahbonk
Messages: 3
Registered: February 2007
Junior Member
I need help optimizing this search query. It takes anything from 3s up to 10s to execute it.

SELECT SUM( IF( tag_id =1 || tag_id =5, 1, NULL ) ) AS tagmatches, movie . * , tag_id, DATE_FORMAT( movie.post_date, '%b %e, %Y' ) AS post_date, (
avg_con + avg_cre + avg_edi + avg_qul
) /4 AS avg, category.en AS categoryname, class.en AS classname
FROM movie
LEFT JOIN search_tags ON movie.id = search_tags.movie_id
LEFT JOIN search_tagwords ON tag_id = search_tagwords.id
LEFT JOIN category ON movie.category = category.id
LEFT JOIN class ON movie.class = class.id
WHERE (
search_tags.tag_id =1
OR search_tags.tag_id =5
)
OR (
(
tag_id IS NULL
OR (
search_tags.tag_id !=1
AND search_tags.tag_id !=5
)
)
AND (
movie.title
REGEXP '[[:<:]](pvp|warrior)[[:>:]]'
)
)
AND approved !=0
AND hide =0
GROUP BY movie.id
ORDER BY tagmatches DESC , movie.downloads DESC
LIMIT 0 , 5


EXPLAIN
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE movie index approved PRIMARY 3 NULL 13178 Using temporary; Using filesort
1 SIMPLE search_tags ref movie_id movie_id 3 wcm.movie.id 4 Using where
1 SIMPLE search_tagwords eq_ref PRIMARY PRIMARY 4 wcm.search_tags.tag_id 1 Using index
1 SIMPLE category eq_ref PRIMARY PRIMARY 3 wcm.movie.category 1
1 SIMPLE class eq_ref PRIMARY PRIMARY 3 wcm.movie.class 1


Any advice/help offered will be much appreciated Smile
Thanks!
Re: Optimizing a search query [message #1606 is a reply to message #1553 ] Thu, 16 August 2007 09:07 Go to previous message
Peter  is currently offline Peter
Messages: 405
Registered: August 2006
Senior Member
Super Guru
Simple answer would be

1) Do not use regexp for search - it can't be indexed. Look at MySQL Full text search or external tools like Sphinx

2) Denormalize - Joins are expensive.


Peter Zaitsev, MySQL Performance Expert
MySQL Performance Blog - http://www.mysqlperformanceblog.com
MySQL Consulting http://www.mysqlperformanceblog.com/mysql-consulting/
Previous Topic:Performance Optimizing INNODB
Next Topic:Sporadically slow MyISAM inserts
Goto Forum:
  


Current Time: Fri Jul 10 01:25:47 EDT 2009

Total time taken to generate the page: 0.02936 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software

MySQL is a trademark of Sun Microsystems.
InnoDB is a trademark of Oracle Corp.

Percona Performance Forums are a service of Percona, Inc.
Not affiliated with Sun Microsystems or Oracle Corp.