Chris Lehneis Charlotte Tregelles How to Deal with N + 1 Queries Database Performance
SCALABILITY
Scalability Challenge ❖ Database only has a limited amount of connections. ❖ Bottleneck occurs when requests exceed number of connections.
What are N + 1 Queries? ! ! ❖ N + 1 problem: Loading many objects from the database, and for each object 1 or more queries are made.
Models
Controller and View
Queries ❖ N+1 queries = potentially 100s of queries ❖ Average page has 5-6 queries
Bullet ❖ Use eager loading to remove N + 1 queries ❖ Remove eager loading when unnecessary ❖ When to use counter cache
Artists Table
Summary ❖ Optimizing databases performance is essential for scalable web applications! ! ❖ Use eager loading, its awesome, but only when beneficial. ! ❖ Cache when counting objects
“Wat?”

Database Opt