Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

ࣗݾ঺հ • Piece of Cake, Inc. • দ࡚ ܒ࣏ (·͖ͭ͟ ͚͍͡) • αʔόαΠυ݉ΠϯϑϥΤϯδχΞ • 2019೥1݄͔Β

Slide 3

Slide 3 text

Ruby΍Railsͷਂ෵͸ ೷͖·ͤΜm(_ _)m

Slide 4

Slide 4 text

ϋϚͬͨࣄྫͷ͝঺հ

Slide 5

Slide 5 text

uniq

Slide 6

Slide 6 text

uniq • ͖͔͚ͬRails 4.2 -> 5.1΁ͷΞοϓάϨʔυ͕࠷ॳͷ࢓ࣄ • deprecated 5.0 -> 5.1ΑΓ • distinctʹॻ͖׵͑Δඞཁ͕͋Δ • uniqΛ͋Δఔ౓ݕࡧ͍͕ͯͨ͠ɺͱ͋ΔՕॴͰॻ͖׵͑ ࿙Ε͕͋ͬͨ

Slide 7

Slide 7 text

uniq • ActiveRecord::RelationʹͯuniqΛ࢖͏ͱSELECT DISTINCT… ͱ DISTINCTจʹม͑ͯ͘ΕΔػೳ • Rails 5.1͔ΒɺActiveRecord::Relation͸EnumerableΛ includeͯ͠ରԠ͢ΔΑ͏ʹͳͬͨͷͰɺArrayΛฦ͢

Slide 8

Slide 8 text

uniq • ى͖ͨ໰୊ -> SELECT id FROM… ʹͯऔಘͨ͠IDΛݩʹ ϨίʔυΛݕࡧ͢ΔΫΤϦ͕͋ͬͨ • uniqͰɺDISTINCT͕ޮ͍ͨͱ͖͸ɺidͷ਺͕ߜΓࠐ·Εͯ ͍ͨ

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

uniq • Rails 5.1͔Βɺuniqͦͷ΋ͷͷϝιου͸׆༻Ͱ͖Δͷͩ ͕ɺArrayͱͯ͠ฦͬͯ͘Δ • Rails 5.0 -> 5.1ΞοϓάϨʔυ࣌ʹ͸Ϩίʔυ਺ͷগͳ͍ ؀ڥͰಈ࡞֬ೝΛ͢ΔͨΊɺಈ࡞ʹؔͯ͠໰୊͸ΈΒΕͣ

Slide 11

Slide 11 text

uniq • Production deployޙɺҰ෦Controller#ActionͷϨεϙϯε λΠϜ͕େ͖͘ͳΔ • unicornͷϓϩηε͕൓Ԡ͠ͳ͘ͳΔ • ଞͷunicornϓϩηε͕ଞͷϦΫΤετΛॲཧ͠Α͏ͱ͠ ͯɺಉ͡Controller#ActionͷॲཧΛ࣮ߦͯ͠͠·͏

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Rails Update࣌ʹ Upgrade GuideΛݟΑ͏

Slide 15

Slide 15 text

ଞ (աڈͷܦݧ)

Slide 16

Slide 16 text

ଞ (աڈͷܦݧ) • INDEXΛࢥͬͨΑ͏ʹ࢖ͬͯ͘Ε͍ͯͳ͍͜ͱ͕Θ͔ͬͨ ͷͰɺFORCE INDEXΛ࢖͏Α͏ʹվળ͢Δ • SELECT … FOR UPDATE Λൃߦதɺ͏͔ͬΓtransaction block಺ʹ ଞαʔϏε ΁ͷAPIΞΫηεΛߦ͏ॲཧΛೖΕͯ ͠·͏ • ଞαʔϏεͷϦΫΤετॲཧʹࣦഊ͠ߦLOCKΛѲͬͨ· ·ʹͳΓঃʑʹϦΫΤετʹ٧·͍ͬͯ͘ͳͲ

Slide 17

Slide 17 text

ऴΘΓ

Slide 18

Slide 18 text

ࢀߟ • 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

Slide 19

Slide 19 text

ࢀߟ • ΠϯσοΫεώϯτͷߏจ (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

Slide 20

Slide 20 text

ࢀߟ • Rails Upgrade Guide (೔ຊޠ൛) • https://railsguides.jp/upgrading_ruby_on_rails.html