1. Database optimisation
- Kill N+1 queries: loading associations in a loop fires one query per row. Use
includes,preloadoreager_load, and confirm with the Bullet gem. - Add the right indexes: index columns used in WHERE, JOIN, ORDER BY and foreign keys. Check query plans with
EXPLAIN. Avoid over-indexing, which slows writes. - Fetch less data: use
selectorpluckfor only the columns you need, and paginate large lists. - Use counter caches for frequent counts, and avoid expensive
COUNT(*)on big tables in every request. - Batch heavy work: use
find_eachfor large iterations.
