databases
9 minStored Functions, Procedures, and Triggers: Put Logic in the Right Layer
Compare database routines and triggers with application services, focusing on transaction ownership, portability, and operational visibility.
Loading page...
databases
9 minCompare database routines and triggers with application services, focusing on transaction ownership, portability, and operational visibility.
Q&A
Q&AFunctions typically return values; procedures are invoked as operations, with engine-specific transaction rules.
A function is commonly used as an expression or query result and returns a value or table. A procedure is invoked to perform an operation and may accept output parameters. Triggers run implicitly. Transaction control differs by engine, so keep commit ownership explicit and test routines against the target database/version.
Q&A
Q&ADefault expressions are evaluated once, so the same mutable object is reused across calls.
A default list or dictionary is created when the function is defined, not every time it is called. Mutations therefore leak between calls. Use None as a sentinel and construct a fresh object inside, or use default_factory for dataclasses and similar models.
Q&A
Q&AUse the form that makes transformation, laziness, and accumulator state easiest to understand.
map is lazy and can be concise with an existing named function; a comprehension often reads better when transformation and filtering are simple. reduce is appropriate for a well-defined associative fold, but sum, any, all, or an explicit loop often communicates intent better. Avoid hidden side effects because they make ordering and debugging difficult.