views:

147

answers:

1

It stumbled upon me while I was reading the query in another post.

Take the following query for example (ignore the non-practical use of the ordering):

SELECT 
*
FROM Members 
ORDER BY (TIMESTAMPDIFF(FRAC_SECOND, DateCreated , SYSDATE()))

Say "Members" table has a huge row count (or the query is complex enough for it to be executed over at least dozen of milliseconds). How does mySQL or other mainstream DB engines evaluate the "SYSDATE()" in the "ORDER BY"?

Say the query takes half a second, the microsecond (FRAC_SECOND) of "SYSDATE" changes 1000 X 1000 X 0.5 = 500 000 times.

My questions are:

  1. Does the "SYSDATE" get fixed on the start of the query execution or it gets evaluated and changes as the execution progresses?
  2. If it's the latter, can I assume the ordering might be jumbled?

UPDATE:
My original post uses NOW as an example of dynamic value, it's SYSDATE now

+3  A: 

NOW() returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.) This differs from the behavior for SYSDATE(), which returns the exact time at which it executes as of MySQL 5.0.12.

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_now

In other words, it is executed only once when the statement is executed. However, if you want to obtain the time at each execution you should use SYSDATE

As of MySQL 5.0.12, SYSDATE() returns the time at which it executes. This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_sysdate

Update:
Well, from what I know Order by will be executed or better said "used" only once. Since the value of TIMESTAMPDIFF(FRAC_SECOND, DateCreated , SYSDATE()) will be different every time you execute the SELECT statement. So, I think (once again I think) ORDER BY will consider either the first evaluated value of the timestampdiff or the last one. Anyway, I think by executing this - you will get a random order every time. Maybe there are better experts than me here who can answer better.

Alexandru Luchian
Ah I see, ok so let me modify the question a bit. That's great info by the way. +1
o.k.w
@Heavy Bytes: I've modified my post with a new sub-question. Care to give it another shot? :)
o.k.w
Well, from what I know Order by will be executed or better said "used" only once. Since the value of TIMESTAMPDIFF(FRAC_SECOND, DateCreated , SYSDATE()) will be different every time you execute the SELECT statement. So, I think (once again I think) ORDER BY will consider either the first evaluated value of the timestampdiff or the last one.Anyway, I think by executing this - you will get a random order every time. Maybe there are better experts than me here who can answer better.
Alexandru Luchian
Hope you don't mind I appended your above comment to your answer, I think it looks more complete for readers :P Too bad I can't upvote you twice :P
o.k.w
Sure, are you satisfied with the answer?I guess the best way to find out how it works is to actually test it :)
Alexandru Luchian
@Heavy Bytes: I appreciate your effort for sure, I'm still waiting for the experts/gurus (as you said) to come along. I might do some testing myself though :)
o.k.w
Sounds like a plan
Alexandru Luchian