⚖ Compare news

Put up to three stories side by side. Pick IDs from any article URL: /compare?id=123&id=456

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

habrahabr.ru ·Russian Federation · world
Words
43
Chars
288
AI summary
Yes
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(). Читать далее

Open article