I have queries that are similar to:
-- MySQL Query
SELECT * FROM table1 WHERE start_date >= CURRENT_TIMESTAMP - INTERVAL 7 DAY
-- MSSQL Query
SELECT * FROM table1 WHERE start_date >= GETDATE() - 7
Since the expression on the right hand side does not depend on the column values, should I expect the query optimizer to optimize it into a constant value such as:
SELECT * FROM table1 WHERE start_date >= '2012-04-28 14:54:31'
Or should I calculate the constants and build a query using code.
According to the documentation for MySQL’s
NOW()function (for whichCURRENT_TIMESTAMPis a synonym):As such, the query optimiser will treat it as a constant, as desired. One can see this in the
EXPLAINoutput.I can’t speak for MSSQL, although perhaps this blog is worth a read?