54

Let me start by saying I have looked at many similar questions asked, but all of them relate to Timestamp and DateTime field type without indexing. At least that is my understanding.

As we all know, there are certain advantages when it comes to DateTime. Putting them aside for a minute, and assuming table's engine is InnoDB with 10+ million records, which query would perform faster when criteria is based on:

  1. DateTime with index
  2. int with index

In other words, it is better to store date and time as DateTime or UNIX timestamp in int? Keep in mind there is no need for any built-in MySQL functions to be used.

Update

Tested with MySQL 5.1.41 (64bit) and 10 million records, initial testing showed significant speed difference in favour of int. Two tables were used, tbl_dt with DateTime and tbl_int with int column. Few results:

SELECT SQL_NO_CACHE COUNT(*) FROM `tbl_dt`;
+----------+
| COUNT(*) |
+----------+
| 10000000 |
+----------+
1 row in set (2 min 10.27 sec)

SELECT SQL_NO_CACHE COUNT(*) FROM `tbl_int`;
+----------+
| count(*) |
+----------+
| 10000000 |
+----------+
1 row in set (25.02 sec)

SELECT SQL_NO_CACHE COUNT(*) FROM `tbl_dt` WHERE `created` BETWEEN '2009-01-30' AND '2009-12-30';
+----------+
| COUNT(*) |
+----------+
|   835663 |
+----------+
1 row in set (8.41 sec)

SELECT SQL_NO_CACHE COUNT(*) FROM `tbl_int` WHERE `created` BETWEEN 1233270000 AND 1262127600;
+----------+
| COUNT(*) |
+----------+
|   835663 |
+----------+
1 row in set (1.56 sec)

I'll post another update with both fields in one table as suggested by shantanuo.

Update #2

Final results after numerous server crashes :) Int type is significantly faster, no matter what query was run, the speed difference was more or less the same as results above.

"Strange" thing observed was execution time was more or less the same when two both field types are stored in the same table. It seems MySQL is smart enough to figure out when the values are the same when stored in both DateTime and int. Haven't found any documentation on the subject, therefore is just an observation.

Community
  • 1
  • 1
David Kuridža
  • 7,026
  • 5
  • 26
  • 25
  • Did you try it? Why not setup a quick benchmark and find out? – ircmaxell Jan 04 '11 at 13:49
  • Working on it as we speak, it takes some time to populate 10 million records with my hardware :) – David Kuridža Jan 04 '11 at 13:52
  • 1
    Make sure you SELECT SQL_NO_CACHE in your testing – coolgeek Jan 04 '11 at 16:01
  • 1
    You can store both the values on 2 columns, int will have indexes and datetime will be used for display purpose. If that is not an option, then store the date in datetime type. – shantanuo Jan 05 '11 at 06:35
  • @DavidKuridža the performace is because io bound and not cpu bound. So, if you use both two fields types you stores more data, this is the reason of the same execution time. – iuridiniz Aug 18 '16 at 00:38

3 Answers3

11

I see that in the test mentioned in the above answer, the author basically proves it that when the UNIX time is calculated in advance, INT wins.

Ollie
  • 544
  • 4
  • 22
Bexol
  • 141
  • 1
  • 5
9

My instinct would be to say that ints are always faster. However, this seems not to be the case

http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/

Edited to add: I realize that you're using InnoDB, rather than MyISAM, but I haven't found anything to contradict this in the InnoDB case. Also, the same author did an InnoDB test

http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-innodb/

coolgeek
  • 903
  • 6
  • 8
  • 7
    Just because he use ` WHERE datetime > UNIX_TIMESTAMP(’1970-01-01 01:30:00′) AND datetime < UNIX_TIMESTAMP(’1970-01-01 01:35:00′)` in his queries for testing INT - it's hilarious. – OZ_ Feb 14 '12 at 16:41
  • Domain is expired/not exist, so use cached versions: *myisam* : https://web.archive.org/web/20191123065201/http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/ *innodb* : https://web.archive.org/web/20180207083623/http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-innodb/ – Vladimir Ch Aug 15 '22 at 13:13
1

it depends on your application, as you can see in an awesome comparison and benchmark of DATETIME , TIMESTAMP and INT type in Mysql server in MySQL Date Format: What Datatype Should You Use? We Compare Datetime, Timestamp and INT. you can see in some situation INT has better perfomance than other and in some cases DATETIME has better performance. and It completely depends on your application

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Yuseferi
  • 7,931
  • 11
  • 67
  • 103