Following SQL query cause deadlocks on production MySQL DB on a Wordpress site with heavy load.
SELECT
lessons.ID AS lesson_id,
resultmeta.meta_value AS status,
lessons.post_title AS title,
lessons.post_name AS slug,
count(resultmeta.meta_value) AS nr_of_results
FROM
mv_posts AS lessons
LEFT JOIN
mv_posts AS results
ON
results.post_parent = lessons.ID AND
results.post_type = 'we_result' AND
results.post_author IN($students_string)
LEFT JOIN
mv_postmeta AS resultmeta
ON
results.ID = resultmeta.post_id AND
resultmeta.meta_key = 'we_result_status_code'AND
results.post_author IN($students_string)
WHERE
lessons.post_type = 'we_lesson' AND
lessons.post_status = 'publish' AND
lessons.ID IN($lessons_id)
GROUP BY
lessons.ID,
resultmeta.meta_value;
With exactly the same DB on a test environment the query is executed in 0.005 seconds. The mv_postmeta table has over 3 million rows and the mv_post has over 1 million rows.
Probably concurrent INSERTS cause this problem and maybe a DB cache overload.
I tried to do READ COMMITED through wordpress wp_query with no success.
Do anyone have a tip for this? The dealocks shuts down the site for around 2 minutes.