I am doing two queries
SELECT * FROM datedim WHERE year = YEAR(now());
and
SELECT * FROM datedim WHERE year = YEAR(getNow());
Here getNow() function just returns NOW();
My problem is that the first one is immediate while the second one take much longer. (1.xx sec)
When I execute the explain command I have this for the first one (which is good)
+----+-------------+---------+------+---------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+-------------+---------+-------+------+-------------+
| 1 | SIMPLE | datedim | ref | dd_year_idx | dd_year_idx | 5 | const | 365 | Using where |
+----+-------------+---------+------+---------------+-------------+---------+-------+------+-------------+
But this for the second one (which is pretty bad)
+----+-------------+---------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | datedim | ALL | NULL | NULL | NULL | NULL | 10958 | Using where |
+----+-------------+---------+------+---------------+------+---------+------+-------+-------------+
Can someone explain me what is happening and why doing the same thing, looking for all date with year that is the same to NOW(), takes much longer when using a function than giving it straight.
This example is simple but later my function will return a specific date, I would like just to understand what is happening.
MySQL seems to execute the
getNow()function for each row, NOW() is executed only once per statement:But you can change your query to the following to have the same effect: