就我而言,我只会测试哪种日期时间格式最适合我的查询持续时间。这是我的查询:
SELECT
max(ColumnA), -- bigint
CreateDate,
ColumnB,
EndTime,
ColumnC,
ColumnD,
ColumnE,
ColumnF, -- int
StartTime, -- timestamp or datetime or int - UNIX_TIMESTAMP(StartTime)
ColumnG,
ColumnH,
ColumnI,
ColumnJ,
UpdateDate,
ColumnK FROM TABLE
条件:
-- with indexes
(Iteration 1) WHERE StartTime BETWEEN "2013-01-18 16:50:00" AND "2013-10-18 18:05:00" AND ColumnF in(5428) GROUP BY ColumnF,StartTime
(Iteration 2) WHERE StartTime BETWEEN "2013-05-18 11:00:00" AND "2013-06-23 22:05:00" AND ColumnF in(5428) GROUP BY ColumnF,StartTime
(Iteration 3) WHERE StartTime BETWEEN "2013-08-18 11:00:00" AND "2013-08-23 22:05:00" AND ColumnF in(7752) GROUP BY ColumnF,StartTime
(Iteration 4) WHERE StartTime BETWEEN "2013-01-18 16:50:00" AND "2013-10-18 18:05:00" AND ColumnF in(5428,5675,444) GROUP BY ColumnF,StartTime
(Iteration 5) WHERE StartTime BETWEEN "2013-09-01 16:50:00" AND "2013-09-15 18:05:00" AND ColumnF in(5428,5675,444) GROUP BY ColumnF,StartTime
-- and same without indexes
测试信息:
Count = 400K
Engine version = 5.6.10
DBEngine = InnoDB
Tests procedure - MySQL restart before any query.
结果:
With index on StartTime, with index on ColumnF
Iteration 1 2 3 4 5
timestamp 0.094 0.094 0.124 0.124 0.125
datetime 0.125 0.109 0.141 0.156 0.156
int 0.125 0.124 0.094 0.156 0.156
Rows 38 8 1 128 8
Without index on StartTime, with index on ColumnF
Iteration 1 2 3 4 5
timestamp 0.078 0.062 0.062 0.109 0.125
datetime 0.078 0.078 0.078 0.140 0.125
int 0.078 0.078 0.078 0.140 0.125
Rows 38 8 1 128 8
所以我决定使用没有索引的时间戳(但没有索引的结果看起来非常相似)。
编辑:我真的不知道为什么我决定只使用一个查询。也许是懒惰,也许我不应该在星期天工作:) 现在测试无关紧要,结论是错误的。当我进行测试时,我会重写所有测试信息......通常:)
EDIT2:固定