MySQL timestamp and long storage time efficiency comparison

Source: Internet
Author: User

 Show create table 20130107date;CREATE TABLE ' 20130107date '(' id ' int( One) not NULLAuto_increment,' C_date ' timestamp  not NULL DEFAULT Current_timestamp,' C_date_long ' int( -) not NULL,' Idx_date ' timestamp  not NULL DEFAULT ' 0000-00-00 00:00:00 ',' Idx_date_long ' int( One) not NULL,PRIMARY KEY(' id '),KEY ' 20130107date_idx_date '(' Idx_date '),KEY ' 20130107date_idx_long '(' Idx_date_long ')) Engine=innodb

There are 90w of data, which are random times.
See full table scan without index first

1:

select COUNT(*) from 20130107datewhere c_date BETWEEN DATE(‘20110101‘) and DATE(‘20110102‘)

This needs 1.54s.

2:

select COUNT(*) from 20130107datewhere c_date_long BETWEEN UNIX_TIMESTAMP(‘20110101‘) and UNIX_TIMESTAMP(‘20110102‘)

This is 2.3s.

But you can do this.
5 |

select UNIX_TIMESTAMP(‘20110101‘),UNIX_TIMESTAMP(‘20110102‘);

Get Results 1293811200 and 1293897600

And then

select COUNT(*) from 20130107datewhere c_date_long BETWEEN 1293811200 and 1293897600;

Discovery turned into a 0.61s
The gap between 1 and 2 can also be said to be compared to the difference between int and compare TIMESTAMP, then the gap between 2 and 3? Is there more time for every record to evaluate Unix_timestamp (' 20110102 ')?

Then use the index

select COUNT(*) from 20130107datewhere idx_date_long BETWEEN UNIX_TIMESTAMP(‘20110101‘) and UNIX_TIMESTAMP(‘20110102‘);select COUNT(*) from 20130107datewhere idx_date BETWEEN ‘20110101‘ and ‘20110102‘

No suspense, two basic is instantaneous.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MySQL timestamp and long storage time efficiency comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.