Skip to content

Instantly share code, notes, and snippets.

@tinnet
Created June 15, 2011 07:59
Show Gist options
  • Save tinnet/1026668 to your computer and use it in GitHub Desktop.
Save tinnet/1026668 to your computer and use it in GitHub Desktop.
mysql timestamp fun
CREATE TABLE `mails` (
`id` bigint(20) NOT NULL auto_increment,
`email` varchar(250) NOT NULL,
`type` char(6) NOT NULL,
`time` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `time` (`time`),
KEY `type` (`type`)
) ENGINE=MyISAM;
/* old query */
explain SELECT email, type, time FROM mails WHERE DATE(FROM_UNIXTIME(time)) > DATE_SUB(NOW(), INTERVAL 2 DAY);
/* new query */
explain SELECT email, type, time FROM mails WHERE time > UNIX_TIMESTAMP(NOW() - INTERVAL 2 DAY);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment