Upgrade to Pro — share decks privately, control downloads, hide ads and more …

気づいた時にリファクタしよう! Laravelのデータベースクエリを最適化するTips_PHPerKaigi2022 LT

気づいた時にリファクタしよう! Laravelのデータベースクエリを最適化するTips_PHPerKaigi2022 LT

みなさんはパフォーマンス改善のためにアプリケーションの最適化に取り組んだことがありますでしょうか?
何もデータベース構造を再構築したりと大規模な改修することだけが最適化ではありません。機能実装のついでに関連処理をリファクタリングしてあげるだけでも十分な最適化になります。

このトークでは気づいた時にちょっとリファクタできる程度のデータベースクエリの最適化Tipsを解説いたします。
5分間で可能な限り詰め込んでお届けします!

こんな方におすすめ

・業務でLaravelを利用されている方
・日頃からパフォーマンスを意識した実装をしたい人

https://fortee.jp/phperkaigi-2022/proposal/3a75fecf-0165-481f-9dfd-0f2959e699ed

AkitoTsukahara

April 11, 2022
Tweet

More Decks by AkitoTsukahara

Other Decks in Technology

Transcript

  1. Copyright© M&A


    Laravel Tips


    PHPerKaigi
    2 0
    2 2
    Akito.Tsukahara

    View full-size slide

  2. Copyright© M&A
    2
    AkitoTsukahara
    M&A
    AkitoTsukahara

    View full-size slide

  3. Copyright© M&A
    3

    View full-size slide

  4. Copyright© M&A
    4






    View full-size slide

  5. Copyright© M&A
    5




    Tips





    View full-size slide

  6. Copyright© M&A
    6

    View full-size slide

  7. Copyright© M&A
    ؾ͍ͮͨ࣌ʹϦϑΝΫλ͠Α͏ʂ


    →খ࢝͘͞ΊΒΕΔTipsूΊ·ͨ͠
    7

    View full-size slide

  8. Copyright© M&A
    8

    View full-size slide

  9. Copyright© M&A
    9
    before

    View full-size slide

  10. Copyright© M&A
    1
    0
    after

    View full-size slide

  11. Copyright© M&A
    1
    1
    EloquentModelͰwhere, findΛ࢖͏ͱ
    ͖Model::query()->where()ʹ͢Δͱ
    PhpStormͰิ׬͕ޮ͘Α

    View full-size slide

  12. Copyright© M&A
    1
    2



    collection id foreach

    View full-size slide

  13. Copyright© M&A
    1
    1
    3

    View full-size slide

  14. Copyright© M&A
    1
    4
    1
    before

    View full-size slide

  15. Copyright© M&A
    1
    5
    1
    after

    View full-size slide

  16. Copyright© M&A
    1
    6
    1




    View full-size slide

  17. Copyright© M&A
    1
    7

    View full-size slide

  18. Copyright© M&A
    1
    8
    before

    View full-size slide

  19. Copyright© M&A
    1
    9
    after

    View full-size slide

  20. Copyright© M&A
    2
    0

    View full-size slide

  21. Copyright© M&A
    paginate


    simplePaginate
    2
    1

    View full-size slide

  22. Copyright© M&A
    2
    2
    paginateͷ୅ΘΓʹsimplePaginateΛ࢖͏

    View full-size slide

  23. Copyright© M&A
    2
    3
    paginateͷ୅ΘΓʹsimplePaginateΛ࢖͏
    paginate simplePaginate

    View full-size slide

  24. Copyright© M&A
    2
    4
    paginateͷ୅ΘΓʹsimplePaginateΛ࢖͏
    DB





    ⾒ paginate



    View full-size slide

  25. Copyright© M&A


    chunk,cursor
    2
    5

    View full-size slide

  26. Copyright© M&A
    2
    6
    chunk,cursor
    before ~Eloquent~

    View full-size slide

  27. Copyright© M&A
    2
    7
    chunk,cursor
    before ~Query Builder~

    View full-size slide

  28. Copyright© M&A
    2
    8
    chunk,cursor
    after ~chunk~

    View full-size slide

  29. Copyright© M&A
    2
    9
    chunk,cursor
    after ~chunk~

    View full-size slide

  30. Copyright© M&A
    3
    0
    chunk,cursor
    after ~cursor~

    View full-size slide

  31. Copyright© M&A
    3
    1
    chunk,cursor
    after ~cursor~
    ಺෦ͰPDOStatement::fetch()Λ࢖༻͢Δ͜
    ͱͰɺ݁ՌηοτΛ1ߦͣͭऔಘ͠·͢ɻऔ
    ಘͨ݁͠Ռ͸δΣωϨʔλͰฦ͞ΕΔɻ


    ● ؾʹͳΔਓ͸cursor()ͷίʔυΛಡΜͰΈΑ
    ͏ʂ


    View full-size slide

  32. Copyright© M&A
    3
    2
    chunkById
    cursor


    chunk


    ...



    View full-size slide

  33. Copyright© M&A


    Tips
    3
    3

    View full-size slide

  34. Copyright© M&A
    count


    3
    4

    View full-size slide

  35. Copyright© M&A
    Eloquent


    3
    5

    View full-size slide

  36. Copyright© M&A
    N+
    1
    ...
    3
    6

    View full-size slide

  37. Copyright© M&A
    3
    7
    N+
    1

    View full-size slide

  38. Copyright© M&A
    3
    8

    View full-size slide