MYSQL WHERE EXISTS需要花费太多时间来执行

时间:2015-04-28 06:47:48

标签: jquery mysql

我在WHERE EXISTS查询中使用MYSQL,但执行时间过长。你能告诉我如何加快这个查询或建议我任何替代查询。

这是我的查询

SELECT a.property_id, count( a.booking_id ) AS total_bookings, group_concat( concat( date_format( a.check_in, '%m/%d/%Y' ) , '  To  ', date_format( a.check_out, '%m/%d/%Y' ) , '  ', '(', (

CASE a.book_status
WHEN 1
THEN 'Pending'
WHEN 3
THEN 'Confirm'
END
), ')', '  |  
<a href=\'manage_booking.php?action=ed&id=', a.booking_id, '\' target=_blank class=linkun >View Detail</a>', '<br>' ) ) AS dates
FROM apl_property_booking AS a
WHERE EXISTS (

SELECT booking_id
FROM apl_property_booking AS b
WHERE b.booking_id != a.booking_id
AND b.property_id = a.property_id
AND (
b.check_in < a.check_out
AND a.check_in < b.check_out
)
AND b.book_status
IN ( 1, 3 )
AND (
'2015-04-01' <= date_add( b.check_out, INTERVAL -1
DAY )
AND '2015-04-27' >= date_add( b.check_in, INTERVAL +1
DAY )
)
)
AND a.book_status
IN ( 1, 3 )
AND (
'2015-04-01' <= date_add( a.check_out, INTERVAL -1
DAY )
AND '2015-04-27' >= date_add( a.check_in, INTERVAL +1
DAY )
)
GROUP BY a.property_id
ORDER BY a.check_in DESC
LIMIT 0 , 30

1 个答案:

答案 0 :(得分:0)

我通过删除MYSQL WHERE EXISTS并替换为LEFT JOIN来解决此问题。 使用WHERE EXISTS进行查询的时间为#34; 37.4117秒&#34;在LEFT JOIN查询时执行&#34; 0.0097秒&#34;执行

这是我的新查询

SELECT a.property_id, count( a.booking_id ) AS total_bookings, group_concat( concat( date_format( a.check_in, '%m/%d/%Y' ) , '  To  ', date_format( a.check_out, '%m/%d/%Y' ) , '  ', '(', (

CASE a.book_status
WHEN 1
THEN 'Pending'
WHEN 3
THEN 'Confirm'
END
), ')', '  |  
<a href=\'manage_booking.php?action=ed&id=', a.booking_id, '\' target=_blank class=linkun >View Detail</a>', '<br>' ) ) AS dates
FROM apl_property_booking AS a
LEFT JOIN apl_property_booking AS b ON b.property_id = a.property_id
WHERE 
b.booking_id != a.booking_id AND 
(b.check_in < a.check_out
AND a.check_in < b.check_out
)
AND b.book_status
IN ( 1, 3 )
AND (
'2015-04-01' <= date_add( b.check_out, INTERVAL -1
DAY )
AND '2015-04-27' >= date_add( b.check_in, INTERVAL +1
DAY )
)
AND a.book_status
IN ( 1, 3 )
AND (
'2015-04-01' <= date_add( a.check_out, INTERVAL -1
DAY )
AND '2015-04-27' >= date_add( a.check_in, INTERVAL +1
DAY )
)
GROUP BY a.property_id
ORDER BY a.check_in DESC
LIMIT 0 , 30