Here are two example queries:
SELECT NOW(), NOW() + INTERVAL 1 HOUR + INTERVAL 1 MINUTE + INTERVAL 1 SECOND;
SELECT NOW(), ADDTIME( NOW(), '1:01:01' );
Is there any difference at all between these two queries? I mean, in performance or best practice uses ?
As far as I tested, the INTERVAL one seems to have slightly longer execution times.
EDIT: The result for 100’000 loops almoust always came out something like this:
Interval: 8.5930590629578 seconds.
Addtime: 8.2951309680939 seconds.
SELECT NOW(), NOW() + 3661 SECOND;
Single interval: 8.3964569568634 seconds.
Interval: 9.1104879379272 seconds.
Addtime: 8.7062540054321 seconds.
EDIT2: New examples:
SELECT NOW(), NOW() + 1 SECOND;
SELECT NOW(), ADDTIME( NOW(), '0:00:01' );
And the time results:
Single interval: 8.4611599445343 seconds.
Addtime: 8.7186510562897 seconds.
Does the ADDTIME parsing always slow it down so much, that the difference can been seen pretty well?
The real question is if this is really a kind of performance gain you’re looking for. Running this 100K times shows a difference of 0.02 sec. So if you run it 5 million times you save one second.
It’s not fair to compare the two since the
intervalhas to do 3 calculations (hour/minute/second). This will slow it down.The
ADDTIME()will have to parse the time which also slow it down.Then if you’re looking for performance use this:
But even then you’re saving milliseconds. If this is really the bottleneck of your application, then you’ve done a wonderful job :).