"Using Temporary" with a Custom Wordpress Search
I currently use a custom wordpress MySQL query to get related products for some of my shopping sites. I've just profiled my pages and i've noticed that this query takes about 3 seconds, yet i'm unsure how to optimise it. The query is below:
explain select
p . *,
unix_timestamp(p.post_modified) as post_modified_ut,
unix_timestamp(p.post_date) as post_date_ut,
(((2.3 * (MATCH (p.post_title) AGAINST ('Motorola+MBP+36+Digital+Video+Monitor' IN BOOLEAN MODE)))) + (0.6 * (MATCH (p.post_content) AGAINST ('Motorola+MBP+36+Digital+Video+Monitor' IN BOOLEAN MODE)))) AS relevance
from
wp_posts as p,
wp_terms as t,
wp_term_taxonomy as tt,
wp_term_relationships as tr
where
(MATCH (p.post_title , p.post_content) AGAINST ('Motorola+MBP+36+Digital+Video+Monitor' IN BOOLEAN MODE))
and tr.object_id = p.ID
and tr.term_taxonomy_id = tt.term_taxonomy_id
and tt.term_id = t.term_id
and p.post_type = 'post'
and p.post_status in ('inherit' , 'publish')
group by p.ID , p.post_title
order by relevance desc
limit 5;
The result from the explain is:
+----+-------------+-------+--------+-------------------------------------------------+------------------+---------+------------------------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+-------------------------------------------------+------------------+---------+------------------------------------+------+---------------------------------+
| 1 | SIMPLE | tt | ALL | PRIMARY,term_id_taxonomy | NULL | NULL | NULL | 2822 | Using temporary; Using filesort |
| 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 8 | reviewexplorer.tt.term_id | 1 | Using index |
| 1 | SIMPLE | tr | ref | PRIMARY,term_taxonomy_id | term_taxonomy_id | 8 | reviewexplorer.tt.term_taxonomy_id | 5 | |
| 1 | SIMPLE | p | eq_ref | PRIMARY,type_status_date,searches,searches_more | PRIMARY | 8 | reviewexplorer.tr.object_id | 1 | Using where |
+----+-------------+-------+--------+-------------------------------------------------+------------------+---------+------------------------------------+------+---------------------------------+
As you can see i'm using temporary tables and i dont want to, I'd like to create an index to speed up this query but i dont fully understand what the explain is telling me.
Solutions
Apparently, MySQL was not able to use any index on the
wp_term_taxonomy
table and have to examine+sort in temporary table all the 2822 rows. That being said, 2822 rows is not so large that it could explain such long query time. Is the load on DB or disk high? anyway...
By looking more closely to your query, it is very confusing. Apparently you have a
wp_term_taxonomy.term_taxonomy_id
and
wp_term_relationships.term_taxonomy_id
but
a possible index is on
wp_term_taxonomy.term_id_taxonomy
.
See the difference?
term_taxonomy_id
≠
term_id_taxonomy
.
Is this a typo?
An error
? A "feature"?