LoginSignup
0
0

More than 3 years have passed since last update.

LaravelのwithCountとwithは挙動が異なるので併用する

Posted at

N+1問題を解決する際に利用する with

withでリレーション先を指定することで基本的に N+1 は解決することが多い
withCountも同様に N+1 問題を解決することができると思っていたが違うようだ。

self::withCount('hoge')

image.png

  • SQL発行数は54個
  • 処理時間は54ms

withCountとwithは併用する

self::withCount('hoge')->with('hoge')

image.png

  • SQL発行数は14個
  • 処理時間は18ms

Nが47(都道府県分)だと約3倍高速化が可能である。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0