Non-deterministic functions and row-based binary logging (MySQL)
-
03-03-2021 - |
Domanda
Newbie questions. I'm working on a MySQL project that will use only row-level binary logging. There will be no replication. Since logging is row-level, does it even matter if a function is deterministic? From the manual, my understanding is that it does not.
Added 6/9/20:
I've added an example which I think would be non-deterministic; please tell me if I'm wrong. The result will always be the same for a given state of the data, returning the highest date value in a set of rows, but would not always return the same value for the same input of a given patient id, since that changes over time. Is that right?
CREATE FUNCTION `last_enc_date`(patient INT) RETURNS date
BEGIN
# Returns the date of the most recent encounter (of any kind) for patient
DECLARE lastvisit DATE;
SELECT MAX(DATE)
INTO lastvisit
FROM encounters
WHERE personid = patient;
RETURN lastvisit;
END
Soluzione
Your understanding is correct. RBR is safe even with non-deterministic functions.
Altri suggerimenti
The question is, does it matter for what? The fact that non-deterministic functions are considered unsafe for statement-level replication is just one side effect of them being non-deterministic; there are other effects that apply regardless of replication: they are related to the function call optimization. Specifically, a function declared as deterministic may not be called as often during query execution, thus improving query performance. For example, a deterministic function in a where
clause will be called only once (unless it references a table column); a non-deterministic one will be called for every row. There are more potential effects explained in documentation.
In other words, misdeclaring a deterministic function as non-deterministic might hurt query performance; misdeclaring a non-deterministic function as deterministic might lead to incorrect query results.