How To Avoid N + 1 problem in Railsby@kalavhan

How To Avoid N + 1 problem in Rails

tldt arrow
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

The problem occurs when we have to make a query to get each of the associations, "1" being the first query and "N" the number of queries required to get all the associations. Preload, Includes and eager_load are three ways to avoid this problem. The problem is because the queries are not handled in a proper manner this can slow down the loading, and that is because of the "N + 1" problem. If we use Preload we can't use a "where" clause in the associated table and with Includes we can fix the problem by making only two queries.

Company Mentioned

Mention Thumbnail
featured image - How To Avoid N + 1 problem in Rails
Josué HackerNoon profile picture

@kalavhan

Josué

Full stack web developer Ruby | JavaScript | PHP | CSS | RoR


Receive Stories from @kalavhan

react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!