3

What is the best way to index a datetime in MySQL? Which method is faster:

  1. Store the datetime as a double (via unix timestamp)
  2. Store the datetime as a datetime

The application generating the timestamp data can output either format. Unfortunately, datetime will be a key for this particular data structure so speed will matter.

Also, is it possible to make an index on an expression? For example, index on UNIX_TIMESTAMP(mydate) where mydate is a field in a table and UNIX_TIMESTAMP is a mysql function. I know that Postgres can do it. I'm thinking there must be a way in mysql as well.

2 Answers 2

4

I don't think either method will be much faster than the other, but if you choose a 'datetime' it will be a lot easier to operate with the standard date time functions.

MySQL doesn't support functional indexes.

Sign up to request clarification or add additional context in comments.

Comments

-3

I think using an integer will be faster because where datetime >= '2010-09-30 01:31:41' will never hit an index. However where intTimestamp >= 237492347 will.

2 Comments

Why would that not use an index?
It does hit an index (provided the index is there). You can dig into MySQL way of performing queries by using EXPLAIN SELECT (...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.