1

写出以下查询的更好/更有效的方法是什么?

当前的 SQL 查询可以很好地完成它的工作,但我担心随着数据库变得更大的处理时间。

SELECT *
FROM room
JOIN catering ON room.room_id = catering.room_id
WHERE capacity BETWEEN '$minCapacity' AND '$maxCapacity' 
AND room.room_id NOT IN 
    (SELECT room_id
    FROM room_booking
    WHERE date_booked = '$us_date')
AND catering.grade = '$grade' ORDER BY room.capacity
4

1 回答 1

1

您可以将 更改NOT INNOT EXISTS. 如果有很多数据可以提高性能:

SELECT *
FROM room
JOIN catering ON room.room_id = catering.room_id
WHERE capacity BETWEEN '$minCapacity' AND '$maxCapacity' 
AND NOT EXISTS
(
    SELECT
        NULL
    FROM
        room_booking
    WHERE
        date_booked = '$us_date'
        AND room.room_id=room_booking.room_id
)
AND catering.grade = '$grade' 
ORDER BY room.capacity
于 2012-05-03T07:47:52.203 回答