How can I store resource available by date (year/month/day/our) in database?

狂风中的少年 提交于 2020-01-05 08:43:49

问题


I have a program which manage resources (agents). In the program there is a feature to select a date from a calendar (e.g.: 08/11/2013 5 a.m.) and get back the number of available resources at selected time. How can I store the status of the resources for every hour of all day in the year effectively in a relational database?

Resource status just a string: "accessible" or "busy".

Have you any idea or strategy for this?

Thanks for help!


回答1:


The best way to store effective date(/time) ranges is as date(/time) ranges. In other words, store records with an effective date/time and an expiry date time.

Don't try to break times into chunks with records for each. This is inflexible and multiplies your data maintenance load very badly.

So for your case, where you want to know when an agent is available, have a record structure something like this:

  • Agent_ID
  • Effective_Datetime
  • Expiry_Datetime

Note you don't need a status ("accessible" vs "busy") since agents have to be in the table to be accessible. It is presumed that if there is no record for an agent effective at a particular point in time, then they must not be accessible.

If you want to know which agents are available at 08/11/2013 05:00 then just query with a WHERE clause that includes: WHERE Effective_Datetime <= '2013-08-11 05:00' and Expiry_Datetime >= '2013-08-11 05:00'



来源:https://stackoverflow.com/questions/17253921/how-can-i-store-resource-available-by-date-year-month-day-our-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!