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

ハマったところ 表参道.rb #44 / into a problem

ハマったところ 表参道.rb #44 / into a problem

Railsアプリ運用時にハマったときの体験です

492da7e022786a647bf0a1efb28db3f8?s=128

Keiji Matsuzaki

March 07, 2019
Tweet

Transcript

  1. ϋϚͬͨͱ͜Ζ දࢀಓ.rb #44 2019/3/7

  2. ࣗݾ঺հ • Piece of Cake, Inc. • দ࡚ ܒ࣏ (·͖ͭ͟

    ͚͍͡) • αʔόαΠυ݉ΠϯϑϥΤϯδχΞ • 2019೥1݄͔Β
  3. Ruby΍Railsͷਂ෵͸ ೷͖·ͤΜm(_ _)m

  4. ϋϚͬͨࣄྫͷ͝঺հ

  5. uniq

  6. uniq • ͖͔͚ͬRails 4.2 -> 5.1΁ͷΞοϓάϨʔυ͕࠷ॳͷ࢓ࣄ • deprecated 5.0 ->

    5.1ΑΓ • distinctʹॻ͖׵͑Δඞཁ͕͋Δ • uniqΛ͋Δఔ౓ݕࡧ͍͕ͯͨ͠ɺͱ͋ΔՕॴͰॻ͖׵͑ ࿙Ε͕͋ͬͨ
  7. uniq • ActiveRecord::RelationʹͯuniqΛ࢖͏ͱSELECT DISTINCT… ͱ DISTINCTจʹม͑ͯ͘ΕΔػೳ • Rails 5.1͔ΒɺActiveRecord::Relation͸EnumerableΛ includeͯ͠ରԠ͢ΔΑ͏ʹͳͬͨͷͰɺArrayΛฦ͢

  8. uniq • ى͖ͨ໰୊ -> SELECT id FROM… ʹͯऔಘͨ͠IDΛݩʹ ϨίʔυΛݕࡧ͢ΔΫΤϦ͕͋ͬͨ •

    uniqͰɺDISTINCT͕ޮ͍ͨͱ͖͸ɺidͷ਺͕ߜΓࠐ·Εͯ ͍ͨ
  9. uniq • طଘͷΫΤϦ͸n✕n✕n…(௚ੵ)ɺDISTINCT͕༗ޮʹͳΒͳ ͍ͱऔಘ͢ΔϨίʔυ਺͕૿͑ΔΑ͏ʹͳ͍ͬͯͨ • EXPLAINͰ࣮ߦͯ͠ɺΫΤϦύϑΥʔϚϯε݁ՌΛݟͯ ͋ͱͰڍಈΛ֬ೝͰ͖ͨ

  10. uniq • Rails 5.1͔Βɺuniqͦͷ΋ͷͷϝιου͸׆༻Ͱ͖Δͷͩ ͕ɺArrayͱͯ͠ฦͬͯ͘Δ • Rails 5.0 -> 5.1ΞοϓάϨʔυ࣌ʹ͸Ϩίʔυ਺ͷগͳ͍

    ؀ڥͰಈ࡞֬ೝΛ͢ΔͨΊɺಈ࡞ʹؔͯ͠໰୊͸ΈΒΕͣ
  11. uniq • Production deployޙɺҰ෦Controller#ActionͷϨεϙϯε λΠϜ͕େ͖͘ͳΔ • unicornͷϓϩηε͕൓Ԡ͠ͳ͘ͳΔ • ଞͷunicornϓϩηε͕ଞͷϦΫΤετΛॲཧ͠Α͏ͱ͠ ͯɺಉ͡Controller#ActionͷॲཧΛ࣮ߦͯ͠͠·͏

  12. uniq • ໰୊ͱͳΔController#ActionΛ௥͍ͬͯ͘ͱɺuniqͱॻ͔ Ε͍ͯͨɻ • uniq͔Βdistinctʹஔ͖׵͑Δͱ໰୊͕ࢭΜͩ

  13. deprecatedͱܯࠂ͞Εͨޙ΋ Ҿ͖ଓ͖࢖͑Δϝιουʹ͸ ஫ҙ͠Α͏

  14. Rails Update࣌ʹ Upgrade GuideΛݟΑ͏

  15. ଞ (աڈͷܦݧ)

  16. ଞ (աڈͷܦݧ) • INDEXΛࢥͬͨΑ͏ʹ࢖ͬͯ͘Ε͍ͯͳ͍͜ͱ͕Θ͔ͬͨ ͷͰɺFORCE INDEXΛ࢖͏Α͏ʹվળ͢Δ • SELECT … FOR

    UPDATE Λൃߦதɺ͏͔ͬΓtransaction block಺ʹ ଞαʔϏε ΁ͷAPIΞΫηεΛߦ͏ॲཧΛೖΕͯ ͠·͏ • ଞαʔϏεͷϦΫΤετॲཧʹࣦഊ͠ߦLOCKΛѲͬͨ· ·ʹͳΓঃʑʹϦΫΤετʹ٧·͍ͬͯ͘ͳͲ
  17. ऴΘΓ

  18. ࢀߟ • ActiveRecord::Relation distinct method (Rails 4.2) • https://github.com/rails/rails/blob/v4.2.11/activerecord/ lib/active_record/relation/query_methods.rb#L776

    • ActiveRecord::Relation (Rails 5.1) -> include Enumerable • https://github.com/rails/rails/blob/v5.1.6.1/activerecord/ lib/active_record/relation.rb
  19. ࢀߟ • ΠϯσοΫεώϯτͷߏจ (FORCE INDEX) • https://dev.mysql.com/doc/refman/5.6/ja/index-hints.html • ϩοΫಡΈऔΓ (SELECT

    … FOR UPDATE) • https://dev.mysql.com/doc/refman/5.6/ja/innodb-locking- reads.html
  20. ࢀߟ • Rails Upgrade Guide (೔ຊޠ൛) • https://railsguides.jp/upgrading_ruby_on_rails.html