no-analytical-queries-in-transactional-db
Keep DISTINCT, GROUP BY, and aggregate queries in analytical databases.
Why
Project architecture reserves SQL deduplication, grouping, and aggregates for ClickHouse or another analytical database, preventing reporting work from competing with transactional reads. This is an explicit datastore policy, not an execution-cost estimate.
Fix
Move reporting queries to ClickHouse or another analytical database with an explicit freshness contract. Use EXISTS or indexed row lookups for operational reads. Preserve atomicity; a reviewed transactional invariant needs an exact, explained SARJ122 exception.
Examples
SELECT DISTINCT team_idFROM event;-- dialect: clickhouseSELECT team_id, COUNT(*)FROM eventGROUP BY team_id;SELECT COUNT(*)FROM eventWHERE id = $1;SELECT EXISTS ( SELECT 1 FROM event WHERE id = $1 );CREATE TEMP TABLE report ONCOMMITDROP ASWITH rows AS ( SELECT DISTINCT team_id FROM event )SELECT team_idFROM rows;CREATE TEMP TABLE report ONCOMMITDROP ASWITH rows AS ( SELECT team_id FROM event WHERE id = $1 )SELECT team_idFROM rows;