DETERMINISTIC is an optional attribute that can be added to a CREATE FUNCTION or CREATE PROCEDURE statement to assert that the routine is free of side effects and depends only on its input parameters. When a routine is declared DETERMINISTIC, the optimizer and replication engines can safely cache results, reorder calls, or skip logging the full statement text in favor of a checksum, which improves performance and binary-log efficiency. Incorrectly marking a routine as DETERMINISTIC when it reads tables, uses random numbers, or references dynamic context (such as NOW()) may lead to wrong query results, stale caches, or data drift between primary and replica servers.
NOT DETERMINISTIC, IMMUTABLE (PostgreSQL), PURE functions, SIDE EFFECTS, SQL CREATE FUNCTION
SQL:1999 standard; MySQL 5.0 (2005)
It asserts that the function will always return the same output for the same inputs, allowing the planner to cache or inline results and making statement-based replication safe.
Ensure the body does not read or modify tables, rely on session variables, call non-deterministic functions (RAND, NOW, UUID), or depend on environment state.
You risk stale caches, incorrect query results, or data divergence between primary and replica because the engine assumes repeatability that does not exist.
No. It only permits the optimizer to cache results; whether it actually does so depends on the specific database engine and query plan.