Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 192263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:24:41+00:00 2026-05-11T16:24:41+00:00

In my question about searching for date ranges I tried simplifying the problem and

  • 0

In my question about searching for date ranges I tried simplifying the problem and inadvertently posed a different and simpler problem.

Rather than complicate that question with an edit I am going to ask the problem I actually intended.

I have two tables Property and Booking. Bookings have a foreign key to Properties and also start and end date.

The user is searching for free slots and supplies a desired duration in days. They also supply a range of start dates that they are interested in. Therefore a search will be along the lines of:
“Find me all properties I want a 3 day slot which starts anytime in May.”

Now I can do this by:
1. Running 31 queries for each potential start day
2. Finding all bookings in May, condense them into one array of 31 booleans representing days and loop through looking for slots.

I assume (2) is more efficient in most cases. Is there any better algorithms? Is there a pure SQL solution.

I will be using Django and my dataset is small so I will probably be OK with a ‘dumb’ approuch but I am curious to know what the best algorithm looks like.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:24:41+00:00Added an answer on May 11, 2026 at 4:24 pm

    Probably overkill for your application – but:

    A relatively simple way of improving your searches at the expense of making the ‘write’ process more complicated, would be to change the Booking table to make it an ‘Availability’ table.

    Add in a boolean column to indicate if the slot is free or booked (or better still put in the id of the customer who’s booked it, and use 0 if the slot is free).

    Start off with a single free slot, 1st Jan 2009 -> 31st Dec 20??

    When you get a booking split the free slot into 3 (two inserts and one update), the booked slot and the two available slots.

    Keep doing that and as the time frame gets more fragmented the booking process will consist of one of the following:

    • Assigning an entire ‘available slot’ to someone (one update)
    • Splitting an ‘available slot’ into two (one update and one insert)
    • Splitting a slot into 3 (as above) if someone books the middle section out of an available slot.

    That’s not incredibly complicated to manage and the search process becomes a simple query: finding any slots in the required time frame that are available (booked=false or customerid=0, whichever way you go with it) where enddate – startdate >= the number of days you want.

    It doubles the size of the booking/availability table, and makes bookings less simple, but the trade off is that the search process is about as easy as it gets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question. I have tried searching on Google and after about 6 searches, I
More than about LINQ to [insert your favorite provider here], this question is about
I've a question about something I'm searching for, ... for too long! We've build
This is not a programming question per se but a question about searching source
I was searching SO to see if there was a question about this but
My question is about throw and exception bubbling. I was searching around about file
I've been searching a lot but I haven't found anything about this question. I'm
My question about the reconfiguration delay when switching between Access 2003 and 2007 the
I recently asked a question about IIf vs. If and found out that there
I was reading this question about how to parse URLs out of web pages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.