V
V
Vladimir Kuts2018-09-25 12:00:25
PostgreSQL
Vladimir Kuts, 2018-09-25 12:00:25

Most efficient redundancy algorithm?

There are, say, models of the following form:

class Car(models.Model):
        vin = models.Charfield(max_length=64)


class Order(models.Model):
       car = models.ForeignKey(Car)
       start_date = models.DatetimeField()
       end_date = models.DatetimeField()

In the Order model, start_date and end_date are the start and end numbers of the reservation.
There are a lot of cars and a lot of orders. One car may already have several bookings at different non-overlapping time intervals.
I set start_date and end_date - what is the most efficient way to select free cars for a given period?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Telepnev, 2018-09-25
@TelepnevDmitriy

start_date and end_date - in daterange + index
or hang a functional index (daterange(start_date, end_date))

select * from car_orders where start_end_range && daterange('2018-01-01', '2018-01-02')

V
Vadim Shatalov, 2018-09-25
@netpastor

Start by finding a free car at a specific point in time
Type

Car.objects.exclude(Q(order__end_date__gte=datetime.now())&Q(order__start_date__lte=datetime.now()))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question