DEV Community

Cover image for N+1 Queries, Batch Loading & Active Model Serializers in Rails

N+1 Queries, Batch Loading & Active Model Serializers in Rails

Usama Ashraf on March 16, 2018

The n+1 query problem is one of the most common scalability bottlenecks. If you’re comfortable with Rails, Active Model Serializers and already h...
Collapse
 
philnash profile image
Phil Nash

That is a great solution! I've never had the situation of loading related resources from a different database before, but if I ever do hopefully I remember this article!

Are there any other uses for the BatchLoader that you've come across?

Collapse
 
usamaashraf profile image
Usama Ashraf

Thanks. BatchLoader was made for essentially similar scenarios. However, I think it has greater utility with GraphQL where the n+1 hydra rears its head more frequently.

Collapse
 
philnash profile image
Phil Nash

Ah, of course!

Collapse
 
bajena profile image
Jan Bajena

Hey, I already mentioned it in Usama's gist, but I've built a gem that tackles the problem of N+1 queries in Active Model Serializers.
You can find it here: github.com/Bajena/ams_lazy_relatio...

Keep on rocking!

Collapse
 
gdm profile image
Gitanjali Mule

β€œA Quickstart Guide for Serializer in Railsβ€Šβ€”β€ŠFast JSON API vs Active Model Serializer” by Gitanjali Mule link.medium.com/yNtj2zNn74