Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a database, is it better to store a time period as a start/end date, or a start date and a length of time?

This is a completely hypothetical question: let's say I have a database where I need to store memberships for a user, which can last for a specific amount of time (1 month, 3 months, 6 months, 1 year, etc).

Is it better to have a table Memberships that has fields (each date being stored as a unix timestamp):

user_id INT, start_date INT, end_date INT

or to store it as:

user_id INT, start_date INT, length INT

Either way, you can query for users with an active membership (for example). For the latter situation, arithmetic would need to be performed every time that query is ran, while the former situation only requires the end date to be calculated once (on insertion). From this point of view, it seems like the former design is better - but are there any drawbacks to it? Are there any common problems that can be avoided by storing the length instead, that cannot be avoided by storing the date?

Also, are unix timestamps the way to go when storing time/date data, or is something like DATETIME preferred? I have run into problems with both datatypes (excessive conversions) but usually settle on unix timestamps. If something like DATETIME is preferred, how does this change the answer to my previous design question?

like image 403
Logan Serman Avatar asked May 01 '12 13:05

Logan Serman


People also ask

Should I store timestamps in database?

Its best to use a timestamp datatype in both MySQL and T-SQL. Using timestamps allows you to perform calucations on the data far easier queries, addings or substrating days, months, years whatever you like.

How the date is stored in database?

MySQL comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD. DATETIME - format: YYYY-MM-DD HH:MI:SS. TIMESTAMP - format: YYYY-MM-DD HH:MI:SS.

How do you store time series?

Time series data is best stored in a time series database (TSDB) built specifically for handling metrics and events that are time-stamped. This is because time series data is often ingested in massive volumes that require a purpose-built database designed to handle that scale.

Should date be stored as string?

Is it safe to store dates as a string in mysql? It is safe as long as the format that you use to represent your dates is unambiguous (that is, each value maps to a unique date). But it is always inefficient not to use the proper datatype to store a value.


1 Answers

It really depends on what type of queries you'll be running against your date. If queries involve search by start/end time or range of dates then start/and date then definitely go with first option.

If you more interested in statistic (What is average membership period? How many people are members for more than one year?) then I'd chose 2nd option.

Regarding excessive conversion - on which language are you programming? Java/Ruby use Joda Time under hood and it simplifies date/time related logic a lot.

like image 78
Petro Semeniuk Avatar answered Sep 21 '22 16:09

Petro Semeniuk