Answer the question
In order to leave comments, you need to log in
How to make an efficient query on a date range?
Hi all! A problem is brewing, it is necessary to make it possible to filter by a range of dates with a complete match by dates. Roughly speaking, there is a hotel booking service, the user sets from April 1 to April 10, at the same time, the hotel owner has set in advance what dates he can book a room. It is necessary to filter the numbers by the complete coincidence of dates. It was decided to store the dates in a separate table with a date column that stores a specific day.
There is no good idea how to make a query more efficiently so that it would not have to be sorted out for each day. It seems to me that this will take a very long time with a large number of numbers. Or is there no other way out besides this solution? Thank you for your attention!
Answer the question
In order to leave comments, you need to log in
So there seems to be no problem.
The hotelier in the table indicates the dates when the room can be booked.
type
outdate indate room
20-01-2015 22-01-2015 125
But there are intervals in postgres. you can think about it this way..
From the table with free dates of numbers, select all records whose date falls within the range entered by the user and check that the number of records for each number is equal to the range.
Таблица FreeNumbers:
FreeDate - Дата, когда номер свободен
NumberNo - номер номера
SELECT count(NumberNo), datediff ('dd', @BeginDate, @EndDate), NumberNo
From FreeNumbers
WHERE FreeDate between @BeginDate and @EndDate
group by NumberNo, datediff ('dd', @BeginDate, @EndDate)
Are you starting from one hotel or is it an aggregator.
If from one, then there is no problem at all, even if everything is done very badly.
Let's say there are 400 rooms in a hotel, there are 365 days in a year, in the table we store the Id of the room, the date, the status of the room. This is about 8 bytes per entry (for postgresql), about a megabyte per year.
Past data can be archived immediately.
Query: dates where the number is not among the numbers for which there is occupied in the dates from, to.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question