问题
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