Search code examples
mysqlperformancedatetimecachingquery-cache

Round `now()` for better caching?


How do I best round down (i.e. "floor") the value of now() to the nearest hour?

This is in order to make the query cache work efficiently with statements such as:

SELECT SQL_CACHE * FROM table WHERE appointment_date >= NOW()

Solution

  • Unless you do the rounding in your application layer, do not expect MySQL to be smart enough to use the query cache. Any query that includes NOW() (and many other functions) will never be cached.

    That said, if you do compute the nearest hour in the application layer, it should work fine.