sql / intermediate
Snippet
Read-Only Isolation Levels for Consistent Reporting
Executing read-only analytical queries within an explicit read-only transaction ensures data consistency without placing unnecessary lock contention on write operations in high-concurrency database systems.
snippet.sql
sql
1
2
3
4
5
START TRANSACTION READ ONLY;SELECT department_id, SUM(salary) AS total_budgetFROM department_expensesGROUP BY department_id;COMMIT;
Breakdown
1
START TRANSACTION READ ONLY;
Initiates a transaction scoped exclusively for reading data, preventing accidental data modification.
2
SELECT department_id, SUM(salary) AS total_budget
Aggregates salary figures per department to produce total budget metrics.
3
FROM department_expenses
Specifies the target database table containing financial records.
4
GROUP BY department_id;
Groups individual rows by department identifier prior to applying the aggregation function.
5
COMMIT;
Concludes the read-only transaction cleanly, releasing any acquired system locks.