在数百万数据的情况下,找到重叠日期范围的最佳方法是什么?

时间:2018-02-11 21:29:55

标签: mysql date overlap date-range

我正在开发一个工作正常的预订系统,我已经完成了以下工作,以查找日期范围内的可用汽车。

Table1 -> cars

| id | registration |
| 1  | 1111111      |
| 2  | 222222       |
| 3  | 3333         |
| 4  | 44444        |


Table2 -> reservation

| id |  dateFrom  |   dateTo   |
| 1  | 2017-12-01 | 2017-12-04 |
| 2  | 2017-12-04 | 2017-12-07 |
| 3  | 2017-12-01 | 2017-12-02 |
| 3  | 2017-12-03 | 2017-12-05 |
| 3  | 2017-12-06 | 2017-12-07 |
| 4  | 2017-12-01 | 2017-12-03 |

Now I need to get cars which don't have reservations from 2017-12-05 to 2017-12-06

SELECT * FROM cars  WHERE NOT EXISTS ( SELECT * FROM reservation 
WHERE cars.id = reservation.carId AND
dateFrom <= '2017-12-06' AND
dateTo >= '2017-12-05' ) 

它的工作顺利导致它遵循非常简单的逻辑。但我担心它的表现。如果我在预订表中有数百万的数据,那么我应该运行此查询来查找日期范围内的可用汽车吗?

如果不是什么解决方案?

0 个答案:

没有答案