Indexing datetime in MySQL

孤人 提交于 2020-01-24 12:35:09

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2812843/indexing-datetime-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!