创建月份和年份列以加快时间戳查询

时间:2016-09-06 21:29:27

标签: mysql mariadb

我在mariadb / mysql中有一个大的日志表:

CREATE TABLE `logs` (
    `id` CHAR(36) NOT NULL,
    `user` CHAR(4) NOT, NULL,
    `dateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `dateUpdated` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB

我正在尝试根据按月和年创建的用户和日期查询日志:

select * from logs where month(dateCreated) = '9' and year(dateCreated) = '2016' and user = '1234'

问题: 我应该创建两个名为month和year的列,并为月,年和用户编制索引以加快查询速度吗?

1 个答案:

答案 0 :(得分:3)

您最好只重构查询的标准,以便更好地利用该领域的可能索引:

WHERE dateCreated >= '2016-09-01 00:00:00' 
   AND dateCreated < '2016-10-01 00:00:00' 
   AND user = '1234'