Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Epoch vs. Date and time in mysql

Tags:

mysql

I have simple question. Which format of time is better for storing in mysql, epoch or standard date-time (human readable format). I want to know what format will be better later in table which stores millions of rows, regarding speed of select statements.

like image 454
Beetlegeuse Avatar asked Feb 01 '14 14:02

Beetlegeuse


People also ask

How do I get epoch in MySQL?

MySQL - UNIX_TIMESTAMP() Function Where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.

What is Unix_timestamp in MySQL?

15, “MySQL Server Time Zone Support”.) unix_timestamp is an internal timestamp value representing seconds since '1970-01-01 00:00:00' UTC, such as produced by the UNIX_TIMESTAMP() function. If format is omitted, this function returns a DATETIME value. If unix_timestamp or format is NULL , this function returns NULL .

How does Unix timestamp compare to MySQL?

UNIX_TIMESTAMP() function When this function used with a date argument, it returns the value of the argument as an unsigned integer in seconds since '1970-01-01 00:00:00' UTC. The argument may be a DATE, DATETIME,TIMESTAMP or a number in YYYYMMDD or YYMMDD.

How do I change datetime to date in MySQL?

In a MySQL database, the DATE_FORMAT() function allows you to display date and time data in a changed format. This function takes two arguments. The first is the date/datetime to be reformatted; this can be a date/time/datetime/timestamp column or an expression returning a value in one of these data types.


1 Answers

You want the unix epoch. I've done many extremely massive MySQL databases, one of which is a data tracking application that logs millions of records every day for some users.

The reasons epoch is better than datetime:

  • int is only 4 bytes versus 8 for datetime - better for storage/memory and ultimately indexing/performance. When you're dealing with millions of rows, it really matters
  • Most server-side languages need to convert from the string format to an int format anyway, before it can reformat the data
  • No dependency on timezones - with datetime you need convert strings that are not stored with timezone ident but with the epoch you only need to know the timezone converting to
like image 113
helion3 Avatar answered Oct 03 '22 19:10

helion3