Routines or PHP?

When to Use Routines and When to Keep Logic in PHP

Put a rule in the database when it must hold for every writer (a batch job, a second application, the mysql prompt): constraints first, then triggers for audit trails, definer views and procedures as a narrow interface for a restricted account, and events for housekeeping. Keep fast-changing business logic in PHP, with its tests, debugger and code review. Routines are versioned only if migrations create them (Migrations and Seeders), port poorly to MariaDB 3,681 or PostgreSQL 1,289 , and burn CPU on the hardest server to scale. Summing ROUND(p * 1.08, 2) over a million rows took about 0.5 s here, and through add_vat() about 3 s. place_order saves round trips and guards stock, so it earns its place; a per-row function in a report does not.