Should you use DATETIME or TIMESTAMP format in MySQL if you do many ORDER BYs on this field?
-
I have a table with over a 1.1 million rows. I frequently select the first 25 order by timestamp desc. I have an index on this field and it performs pretty reasonably, however, I'm looking for optimum performance... would it help to move to datetime format instead of timestamp?
-
Answer:
This was an interesting question; my theory was that DATETIME would be faster as it's searching a smaller space. It turns out that it might be. http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/ This part is especially interesting. mysql> load data infile â/export/home/ntavares/test_datetime.sqlâ into table test_datetime; Query OK, 10000000 rows affected (41.52 sec) Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 0 mysql> load data infile â/export/home/ntavares/test_datetime.sqlâ into table test_timestamp; Query OK, 10000000 rows affected, 44 warnings (48.32 sec) Records: 10000000 Deleted: 0 Skipped: 0 Warnings: 44 This is not the same thing as querying your data but my guess is that querying TIMESTAMPs would be somewhat smaller than DATETIMEs. Ideally, you should do your own profiling with your own data with your own queries. However, you should make the decision not only based on benchmarks but also your application; if you are updating things a lot and need to find the difference between to timestamps, then maybe TIMESTAMP is the right choice, even with the performance penalty. If you are storing the date for an event, use DATETIME. The reasoning behind is that TIMESTAMPS are UNIX timestamps that start from the UNIX epoch and define how long has passed aka. they define a specific point in time. DATETIME is what it is; it's the current date and time with all the daylight savings and timezones applied.
Can Duruk at Quora Visit the source
Other answers
The important thing is that you're using an index for the sort. The difference between a DATETIME and TIMESTAMP data type is insignificant. It's true a TIMESTAMP is 4 bytes and DATETIME is 8 bytes in MySQL before 5.6.4. So the index on DATETIME is roughly twice as much disk space and memory. But the net difference in query time is not going to be very large, compared to the difference between using an index and not using an index. After MySQL 5.6.4, TIMESTAMP is 4 bytes + fractional second storage, and DATETIME is stored more compactly, 5 bytes + fractional second storage. So they are even closer, and this makes the difference between them even less. Worrying about this is in the realm of micro-optimization. That is, wasting your time and attention on trying to optimize something to such a fine degree, when you should be paying attention to other things that are more important.
Bill Karwin
Related Q & A:
- Does Mathematica use first-order or second-order unification?Best solution by Mathematica
- Does having many databases affect MySQL performance?Best solution by Database Administrators
- How to convert VARCHAR to TIMESTAMP in MySQL?Best solution by Stack Overflow
- How to format RSS pubDate to DateTime?Best solution by Stack Overflow
- How to change HD phillips televsion picture format to a diffrent format when many are non selectable. please?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.