I use following query frequently:
SELECT * FROM table WHERE Timestamp > [SomeTime] AND Timestamp < [SomeOtherTime] and publish = 1 and type = 2 order by Timestamp
I would like to optimize this query, and I am thinking about put timestamp as part of primary key for clustered index, I think if timestamp is part of primary key , data inserted in table has write to disk sequentially by timestamp field.Also I think this improve my query a lot, but am not sure if this would help.
table has 3-4 million+ rows.
timestamp field never changed.
I use mysql 5.6.11
Anothet point is : if this is improve my query , it is better to use timestamp(4 byte in mysql 5.6) or datetime(5 byte in mysql 5.6)?
Best Answer
Consider the following:
KEY (publish, type, Timestamp)
. By your choice of query I get that filtering bypublish
andtype
is common.PRIMARY KEY
is unique. Are you certain you are not allowed to have two identicalTimestamp
values? Remember that resolution is 1 second, so it is very common to have sameTIMESTMAP
values for different rows.AUTO_INCREMENT
columns asPRIMARY KEY
(see Reasons to use AUTO_INCREMENT columns on InnoDB). Just use a normal index on(publish, type, Timestamp)
is my general opinion.