我被其他开发人员给了下面的MySQL查询并且它非常慢,我试图通过指定列而不是*并使用from语句而不是长度的IN语句来调整它,但它&# 39; s仍然非常慢。
如果我能从这里获得更快的页面加载,我将不胜感激!
背景:该代码适用于wordpress页面,该页面列出了在指定日期内具有任何可用性的属性。
SELECT DISTINCT(wposts.ID), wposts.*, wpostmeta5.meta_value as 'calendar'
FROM DvCs_posts wposts
LEFT JOIN DvCs_postmeta wpostmeta5 ON ( wposts.ID = wpostmeta5.post_id AND wpostmeta5.meta_key = 'calendar' )
INNER JOIN DvCs_bookingtypes bookingtypes ON ( wpostmeta5.meta_value = bookingtypes.booking_type_id )
LEFT JOIN DvCs_term_relationships tr ON ( wposts.ID = tr.object_id AND tr.term_taxonomy_id = 1 )
WHERE wposts.post_type = 'post'
AND(
SELECT tt.term_id FROM DvCs_term_taxonomy tt WHERE tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.term_id = 1 ) IS NULL
AND (
( SELECT booking_id FROM DvCs_bookingdates bd2 WHERE bd2.booking_id IN ( (SELECT booking_id FROM DvCs_booking bookings WHERE bookings.booking_type = bookingtypes.booking_type_id ) ) AND bd2.booking_date BETWEEN '2014-06-27' and '2014-06-28' LIMIT 1 ) IS NULL
OR ( SELECT booking_id FROM DvCs_bookingdates bd2 WHERE bd2.booking_id IN ( (SELECT booking_id FROM DvCs_booking bookings WHERE bookings.booking_type = bookingtypes.booking_type_id ) ) AND bd2.booking_date BETWEEN '2014-07-04' and '2014-07-10' LIMIT 1 ) IS NULL
OR ( SELECT booking_id FROM DvCs_bookingdates bd2 WHERE bd2.booking_id IN ( (SELECT booking_id FROM DvCs_booking bookings WHERE bookings.booking_type = bookingtypes.booking_type_id ) ) AND bd2.booking_date BETWEEN '2014-07-11' and '2014-07-17' LIMIT 1 ) IS NULL
OR ( SELECT booking_id FROM DvCs_bookingdates bd2 WHERE bd2.booking_id IN ( (SELECT booking_id FROM DvCs_booking bookings WHERE bookings.booking_type = bookingtypes.booking_type_id ) ) AND bd2.booking_date BETWEEN '2014-06-28' and '2014-06-29' LIMIT 1 ) IS NULL
OR ( SELECT booking_id FROM DvCs_bookingdates bd2 WHERE bd2.booking_id IN ( (SELECT booking_id FROM DvCs_booking bookings WHERE bookings.booking_type = bookingtypes.booking_type_id ) ) AND bd2.booking_date BETWEEN '2014-07-05' and '2014-07-11' LIMIT 1 ) IS NULL
OR ( SELECT booking_id FROM DvCs_bookingdates bd2 WHERE bd2.booking_id IN ( (SELECT booking_id FROM DvCs_booking bookings WHERE bookings.booking_type = bookingtypes.booking_type_id ) ) AND bd2.booking_date BETWEEN '2014-07-12' and '2014-07-18' LIMIT 1 ) IS NULL
)
AND wposts.post_status = 'publish'
AND wposts.id NOT IN
(
SELECT ID FROM DvCs_posts p
INNER JOIN DvCs_term_relationships r ON p.id = r.object_id
INNER JOIN DvCs_term_taxonomy t ON r.term_taxonomy_id = t.term_taxonomy_id
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND t.term_id = 29
)
ORDER BY RAND()