Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Integer vs DateTime index

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.

like image 735
David Kuridža Avatar asked Jan 04 '11 13:01

David Kuridža


People also ask

Should I use the datetime or TIMESTAMP data type in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

Is it better to use TIMESTAMP or datetime?

Timestamps are also lighter on the database and indexed faster. The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format.

Can we index date field in MySQL?

This makes your datetime column an excellent candidate for an index if you are going to be using it in conditions frequently in queries. If your only condition is BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 30 DAY) and you have no other index in the condition, MySQL will have to do a full table scan on every query.

Which column should be indexed in MySQL?

Mostly we create index when creating table. Any column in creating table statement declared as PRIMARY KEY, KEY, UNIQUE or INDEX will be indexed automatically by MySQL. In addition, you can add indexes to the tables which has data.


1 Answers

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.

like image 97
Bexol Avatar answered Oct 09 '22 13:10

Bexol