根据营业时间有效地确定企业是否营业 [英] Efficiently determining if a business is open or not based on store hours

查看:90
本文介绍了根据营业时间有效地确定企业是否营业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个时间(例如,当前星期二下午4:24),我希望能够从一组业务中选择所有当前开放的业务.

Given a time (eg. currently 4:24pm on Tuesday), I'd like to be able to select all businesses that are currently open out of a set of businesses.

  • 我有一周中每一天每个公司的营业时间和营业时间
  • 让我们假设一家企业每小时只能在00、15、30、45分钟开/关
  • 我假设每周都有相同的时间表.
  • 我最感兴趣的是能够快速查找在特定时间开放的一组业务,而不是数据的空间要求.
  • 请介意,我的某些会员开放时间为一天晚上11点,第二天早上1点关闭.
  • 假期没关系-我将分别处理

存储这些打开/关闭时间的最有效方法是什么,这样我就可以迅速通过一个时间/星期几元组来快速找出哪些公司营业?

What's the most efficient way to store these open/close times such that with a single time/day-of-week tuple I can speedily figure out which businesses are open?

我正在使用Python,SOLR和mysql.我希望能够在SOLR中进行查询.但坦率地说,我乐于接受任何建议和替代方案.

I am using Python, SOLR and mysql. I'd like to be able to do the querying in SOLR. But frankly, I'm open to any suggestions and alternatives.

推荐答案

如果您愿意一次只看一个星期,则可以规范化所有开/关时间,将其设置为自开始以来的分钟数.周,例如周日0小时.对于每个商店,您将创建许多形式为[startTime,endTime,storeId]的元组. (对于跨过星期日午夜的几个小时,您必须创建两个元组,一个元组到周末,一个元组从周初开始).这组元组将在startTime和endTime上进行索引(例如,使用要预处理的树).元组不应该那么大:一周内只有大约1万分钟,可以容纳2个字节.在具有适当索引的MySQL表中,该结构将很优美,并且对常量插入和复制非常有弹性.随着信息的改变删除记录.您的查询将只是选择storeId,其中startTime< =时间和结束时间> = time",其中时间是自星期日午夜以来的规范化分钟.

If you are willing to just look at single week at a time, you can canonicalize all opening/closing times to be set numbers of minutes since the start of the week, say Sunday 0 hrs. For each store, you create a number of tuples of the form [startTime, endTime, storeId]. (For hours that spanned Sunday midnight, you'd have to create two tuples, one going to the end of the week, one starting at the beginning of the week). This set of tuples would be indexed (say, with a tree you would pre-process) on both startTime and endTime. The tuples shouldn't be that large: there are only ~10k minutes in a week, which can fit in 2 bytes. This structure would be graceful inside a MySQL table with appropriate indexes, and would be very resilient to constant insertions & deletions of records as information changed. Your query would simply be "select storeId where startTime <= time and endtime >= time", where time was the canonicalized minutes since midnight on sunday.

如果信息不是经常更改,并且您希望快速查找,则可以预先解决所有可能的查询并缓存结果.例如,一周中只有672个季度小时周期.有一个企业列表,每个企业都有一个开张列表和一个列表.像布兰登·罗德斯(Brandon Rhodes)的解决方案那样的关闭时间,您可以简单地遍历一周中的每15分钟一次,找出谁在打开,然后将答案存储在查找表或内存列表中.

If information doesn't change very often, and you want to have lookups be very fast, you could solve every possible query up front and cache the results. For instance, there are only 672 quarter-hour periods in a week. With a list of businesses, each of which had a list of opening & closing times like Brandon Rhodes's solution, you could simply, iterate through every 15-minute period in a week, figure out who's open, then store the answer in a lookup table or in-memory list.

这篇关于根据营业时间有效地确定企业是否营业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆