HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

MySQL Deterministic Procedures

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
proceduresmysqldeterministic

Problem

As a general rule, should I be declaring all my stored procedures with the DETERMINISTIC keyword if they are indeed deterministic?

It seems to me that the majority of stored procedures will be deterministic. Am I right in thinking that the only non-deterministic procedures are ones that call non-deterministic functions like RAND() or CURDATE()?

Anyway, the reason I'm asking is because when I use the Data Restore feature within MySQL Workbench I get this error:

ERROR 1418 (HY000) at line 1209: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)


Is it better practice to simply set the 'log_bin_trust_function_creators=1' instead? Bear in mind that I have 50+ stored procedures.

Solution

It makes sense to use the deterministic keyword for procedures that are really deterministic, because:


Declaring a deterministic routine as NONDETERMINISTIC might diminish
performance by causing available optimizations not to be used.

But:


Am I right in thinking that the only non-deterministic procedures are
ones that call non-deterministic functions like RAND() or CURDATE()?

No, a procedure that reads data from the database and bases it's result on that data (in addition to it's inputs) is also non-deterministic (because that data might change between calls).

It is worth pointing out that whether a procedure is DETERMINISTIC or not needs to be considered in a replication context:


When you create a stored function, you must declare either that it is deterministic or that it does not modify data. Otherwise, it may be unsafe for data recovery or replication.

although:


If row-based or mixed binary logging is in use, the statement is accepted and replicated even if the function was defined without the DETERMINISTIC keyword.

Context

StackExchange Database Administrators Q#4079, answer score: 6

Revisions (0)

No revisions yet.