Below are less efficient MySQL queries involving wp posts, taxonomies, and a custom movie table search. The custom movies table contains around 30k rows, and wp posts have been generated with the inclusion of post_id
in a dedicated movies table to accommodate additional data. Is there anyone assist in optimizing the performance of the MySQL queries?
/* QUERY 1: (If Remove WHERE filters, approx. time 0.3000ms) */
SELECT pd.id, pd.title, pd.slug, mo.posterURLs, mo.imdbRating
FROM posts pd
INNER JOIN movies mo ON (pd.id = mo.post_id)
INNER JOIN term_relation tr ON (pd.id = tr.obj_id) /* Used with filter (Can be removed) */
WHERE pd.type = 'movies'
AND pd.status = 'publish'
AND mo.type = "movie" /* Used with filters (Can be removed) */
AND mo.streamingInfo REGEXP "jio|zee5|voot" /* Used with filter (Can be removed) */
AND tr.tax_id IN (123456,78910) /* Used with filter (Can be removed) */
GROUP BY pd.id
ORDER BY mo.year DESC, pd.date DESC /* Can be mo.year DESC or mo.imdbRating ASC / DESC */
LIMIT 0, 24;
/* QUERY 4 for getting movies by genres (approx. time 0.01400ms) */
SELECT pd.ID, pd.post_title, pd.guid, mo.posterURLs, mo.imdbRating
FROM posts pd
INNER JOIN movies mo
ON pd.id = mo.post_id
INNER JOIN term_relation tr
ON pd.id = tr.obj_id
WHERE pd.type = 'movie'
AND pd.status = 1
AND tr.tax_id IN (123456)
GROUP BY pd.id
ORDER BY mo.imdbRating DESC, pd.date DESC
LIMIT 0, 30
-- OR --
SELECT pd.ID, pd.post_title, pd.guid, mo.posterURLs, mo.imdbRating
FROM posts pd
INNER JOIN movies mo
ON pd.id = mo.post_id
INNER JOIN term_relation tr
ON pd.id = tr.obj_id
INNER JOIN term_taxonomy tt
ON (tr.tax_id = tt.tax_id)
INNER JOIN terms t
ON (tt.term_id = t.term_id)
WHERE 1=1
AND pd.type = 'movies'
AND pd.status = 'publish'
AND t.term_id IN (123456)
GROUP BY pd.id
ORDER BY mo.imdbRating DESC, pd.date DESC
LIMIT 0, 30
For a more detailed explanation of the result from the "EXPLAIN" statement, the creation of table structures, and sample row data, please follow Gist here.