I got confused with a seemingly simple concept. MySQL defines deterministic function as a function that:
always produces the same result for the same input parameters
So in my understanding, functions like:
CREATE FUNCTION foo (val INT) READS SQL DATA
BEGIN
DECLARE retval INT;
SET retval = (SELECT COUNT(*) FROM table_1 WHERE field_1 = val);
RETURN retval;
END;
are not deterministic (there is no guarantee that delete/update/insert does not happen between 2 calls to the function). At the same time, I saw many functions which do pretty much the same, i.e. return value based on result of queries, and declared as DETERMINISTIC
. It looks like I'm missing something very basic.
Could anyone clarify this issue?
Thanks.
Update
Thanks for those who answered(+1); so far it looks like there is a widespread misuse of DETERMINISTIC
keyword. It is still hard to believe for me that so many people do it, so I'll wait a bit for other answers.
From the MySQL 5.0 Reference:
So there you have it, you can tag a stored routine as
DETERMINISTIC
even if it is not, but it might lead to unexpected results or performance problems.