I have a field in a mysql database where dates are stored as BIGINT
like YYYYMMDDHHMMSS
(PHP: date('YmdHis')
). I'm building a query, where I check the amount of rows per hour and have tried two different things:
SELECT x hour, … FROM table WHERE … GROUP BY x …
- x =
SUBSTR(CAST(time AS CHAR(14)), 9, 2)
- x =
MOD(FLOOR(time / 10000), 100)
I built a quick benchmark (did 1000 querys with each version) and both of them seem to be equally quick so I'm really interested in what database designers would advise me to do?
Best Answer
Perhaps as a third alternative you may want to try
Here is a fourth alternative requiring some project management decisions: Instead of storing
date('YmdHis'))
you should storedate('U'))
. TheU
stands for seconds since Jan 1, 1970. Then, your formula would be simplerIf you must keep what you have, use the BENCHMARK function
I tried it on a random DB Server 1 millions times and then 10 million times
On the particular hardware I ran this on, your first expression is about 2.67 times faster.
Give it a Try !!!