N+1 запросов в Laravel: как один foreach превращается в 101 SELECT и как это запретить

habrahabr.ru · world
Full story

A production page listing 100 posts runs slowly, while the profiler shows a hundred identical lightweight queries. That is the N+1 problem, which Eloquent creates through lazy loading. The article explains fixing it with with() and withCount() and blocking it at the framework level with preventLazyLoading().

Страница со списком из 100 постов на проде тормозит, а в профайлере — сотня одинаковых лёгких запросов. Это N+1: разбираем, как Eloquent создаёт его через ленивую загрузку, как лечить через with() и withCount() и как запретить на уровне фреймворка через preventLazyLoading(). Читать далее

Read full article → Compare