Slide 1

Slide 1 text

"DUJWF3FDPSEσʔλॲཧ Ξϯνύλʔϯ 3BJMT%FWFMPQFST.FFUVQ

Slide 2

Slide 2 text

ࣗݾ঺հ • ӿຊ හؙʢtoshimaruʣ • Twitter: @toshimaru_e/Github: toshimaru • Rails Engineer ˏגࣜձࣾGunosy • ࠷ۙͷਪ͠gem: • toshimaru/rubocop-rails 2

Slide 3

Slide 3 text

ࠓ೔࿩͢͜ͱ ߦͰ ̏ͭͷࣄྫΛ΋ͱʹ̒ͭͷ"DUJWF3FDPSEσʔλ ॲཧʹ͓͚ΔΞϯνύλʔϯΛίʔυͱͦͷϕ ϯνϚʔΫ݁Ռͱ΋ʹ঺հ͠·͢ɻ 3

Slide 4

Slide 4 text

ຊൃදͷΰʔϧ "DUJWF3FDPSEΛ࢖ͬͨඇޮ཰ͳσʔλॲཧʹΑͬͯύϑΥʔ Ϛϯε͕ܶతʹམͪΔ͜ͱ͕͋Δ͜ͱΛཧղ͢Δ ຊൃදͰ঺հ͢ΔΞϯνύλʔϯΛਖ਼͘͠ཧղ͠Ξϯνύ λʔϯΛ౿ΜͩίʔυΛमਖ਼͢Δ͜ͱ͕Ͱ͖Δɻ΋͘͠͸ ΞϯνύλʔϯΛճආͨ͠ίʔυΛॻ͘͜ͱ͕Ͱ͖Δ ίʔυϨϏϡʔʹ͓͍ͯΞϯνύλʔϯΛ౿ΜͩίʔυΛ ൃݟɾࢦఠ͢Δ͜ͱ͕Ͱ͖Δ 4

Slide 5

Slide 5 text

8IZOPU42- w ʮͦΕ42-ͰͰ͖ΔΑʯ w ύϑΥʔϚϯεϑΝʔετͰٕज़બ୒͢ΔͷͰ͋Ε͹ۃ࿦/P 03. /P3VCZ͕ϕετ w ͳͥ"DUJWF3FDPSEʹͩ͜ΘΔ͔ʁ w %#ͱͷΠϯλϥΫγϣϯΛ"DUJWF3FDPSEϞσϧʹू໿͠ڽू ౓ͷߴ͍ϞσϧΛߏங͢Δ w 'MFYJCJMJUZ ॊೈੑ .FJOUFOBCJMJUZ ϝϯςφϯεੑ 3FBEBCJMJUZ Մಡੑ 5

Slide 6

Slide 6 text

ϕϯνϚʔΫ w ϕϯνϚʔΫ͸Լه63-ͷίʔυΛࢀߟʹॲཧͷ࣮ߦ࣌ؒͱϝϞϦ࢖༻ྔΛ ܭଌ w IUUQTEBMJCPSOBTFWJDDPNQPTUTQSPDFTTJOHMBSHFDTWpMFTXJUISVCZ w ϕϯνϚʔΫ؀ڥ w $JSDMF$* $16.# ্Ͱܭଌ w 3VCZ3BJMT.Z42- w ιʔείʔυɾϕϯνϚʔΫ݁Ռ͸ެ։͞Ε·͢ w IUUQTHJUIVCDPNUPTIJNBSVSENSBJMT 6

Slide 7

Slide 7 text

લఏࣄ߲ w ࠓճίʔυ಺Ͱొ৔͢ΔϞσϧ͸6TFSIBTNBOZ1PTUTͳؔ ܎ͷ6TFSϞσϧ1PTUϞσϧ w ࣄલʹ6TFSສ݅ɺ1PTU໿ສ݅Λσʔλͱͯ͠ొ࿥ w ϢʔβʔͷλΠϜκʔϯ͸શͯ65$ͱͯ͠ѻ͏ w ࠓճߦ͏࠷దԽͷର৅͸ΞϓϦέʔγϣϯίʔυͰ͋Γɺ σʔλϕʔεͷ࠷దԽ͸ߟ͑ͳ͍ w σʔλϕʔεͷτϥϯβΫγϣϯॲཧ͸ߟྀ͠ͳ͍ 7

Slide 8

Slide 8 text

εΩʔϚఆٛ TDIFNBSC 8

Slide 9

Slide 9 text

ࣄྫ̍ શϢʔβʔͷத͔Β೥Ҏ߱ͷొ࿥Ϣʔβʔ΁ϙΠ ϯτΛ෇༩͢Δɻ 9 ˞ొ࿥೔6TFSDSFBUFE@BUͱͯ͠ѻ͍·͢

Slide 10

Slide 10 text

ΦϦδφϧιʔείʔυ 10

Slide 11

Slide 11 text

ΦϦδφϧίʔυ ϕϯνϚʔΫ 11 5JNF .FNPSZ ΦϦδφϧ TFDT .#

Slide 12

Slide 12 text

-PPLT(PPE5P:PV 12

Slide 13

Slide 13 text

All Each Pattern 13 Anti-Pattern

Slide 14

Slide 14 text

"MM&BDI1BUUFSO w .PEFMBMMͰ͋ΔςʔϒϧͷશϨίʔυΛऔಘͨ͠ޙʹɺ ͦͷ݁ՌΛFBDIͰϧʔϓͤ͞Δ͜ͱ 14

Slide 15

Slide 15 text

"MM&BDI1BUUFSO໰୊఺ w શ݅औಘͰϝϞϦ͕ඡഭ w ͦ΋ͦ΋શ݅औಘͰ͋Δඞཁ͕͋Δͷ͔ʁ w ୯७ʹϧʔϓճ਺͕૿͑ΔͷͰ$16Ϧιʔε΋ফඅ 15

Slide 16

Slide 16 text

"MM&BDI1BUUFSOղܾࡦ 6TFSऔಘ݅਺ΛϑΟϧλʔ w BMM➜XIFSF গͣͭ͠6TFSΛऔಘͯ͠ϝϞϦϑϨϯυϦʔͳॲཧʹ w FBDI➜pOE@FBDI 16

Slide 17

Slide 17 text

վળޙιʔείʔυ 17

Slide 18

Slide 18 text

վળޙιʔείʔυ ϕϯνϚʔΫ 18 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ TFDT .#

Slide 19

Slide 19 text

19 -PPLT(PPE5P:PV

Slide 20

Slide 20 text

N+1 Update Queries Pattern 20 Anti-Pattern

Slide 21

Slide 21 text

/6QEBUF2VFSJFT1BUUFSO 21 1 Select N Update

Slide 22

Slide 22 text

/6QEBUF2VFSJFT1BUUFSO ໰୊఺ w ճ4FMFDU/ճ6QEBUFͷΫΤϦ͕૸Δ w /ͷ਺͕ଟ͘ͳΕ͹ͳΔ΄ͲύϑΥʔϚϯε͕ѱԽ w ؔ࿈Ξϯνύλʔϯ w /%FMFUF1BUUFSO 22

Slide 23

Slide 23 text

/6QEBUF2VFSJFT1BUUFSO ղܾࡦ ෳ਺ϨίʔυΛҰׅͰߋ৽͢Δ w VQEBUF➜VQEBUF@BMM 23

Slide 24

Slide 24 text

վળޙιʔείʔυ 24

Slide 25

Slide 25 text

վળޙιʔείʔυ ϕϯνϚʔΫ 25 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ TFDT .# վળ̎ TFDT .#

Slide 26

Slide 26 text

վળޙιʔείʔυ ϕϯνϚʔΫ 26 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ TFDT .# վળ̎ TFDT .# ࣄྫ̍݁Ռ ࣮ߦ࣌ؒ͸ഒҎ্ߴ଎ԽɺϝϞϦফඅ΋෼ͷఔ౓· Ͱ཈͑Δ͜ͱ͕Ͱ͖·ͨ͠ɻ

Slide 27

Slide 27 text

஫ҙ఺ w VQEBUFͱVQEBUF@BMMͷॲཧ͸౳ՁͰ͸ͳ͍ w JUEPFTOPUUSJHHFS"DUJWF3FDPSEDBMMCBDLTPSWBMJEBUJPOT w ݱ৔ʹ͓͍ͯ͸ద੾ͳॲཧΛՃ্͑ͨͰVQEBUF@BMMΛ࢖͏͜ ͱ w ςʔϒϧϩοΫʹ஫ҙʂ w ద੾ͳτϥϯβΫγϣϯઃఆɾϩοΫͷઃఆΛʂ w গͣͭ͠VQEBUF@BMMJO@CBUDIFTVQEBUF@BMM 27

Slide 28

Slide 28 text

ࣄྫ̎ Ϣʔβʔຖͷهࣄͷ͍͍Ͷ਺ʢMJLF@DPVOUʣ߹ܭ͕ଟ͍ॱͰ 501ϢʔβʔͷϢʔβʔ໊Λ߹ܭ͍͍Ͷ਺ͱͱ΋ʹग़ྗ ͢Δɻ 28 ˞࣮ࡍ͸ԿΒ͔ͷσʔλετΞʹอଘ͢Δॲཧ͕૝ఆ͞ΕΔ͕ࠓճ͸γϯϓϧʹඪ ४ग़ྗͱ͢Δ

Slide 29

Slide 29 text

29 ΦϦδφϧιʔείʔυ

Slide 30

Slide 30 text

ΦϦδφϧίʔυ ϕϯνϚʔΫ 30 5JNF .FNPSZ ΦϦδφϧ TFDT .#

Slide 31

Slide 31 text

31 -PPLT(PPE5P:PV

Slide 32

Slide 32 text

Ruby Aggregation Pattern 32 Anti-Pattern

Slide 33

Slide 33 text

3VCZ"HHSFHBUJPO1BUUFSO w Ϩίʔυશऔಘɺ3VCZͷੈքͰूܭɾฒͼସ͑ 33

Slide 34

Slide 34 text

3VCZ"HHSFHBUJPO1BUUFSO ໰୊఺ w طʹ঺հͨ͠ԼهͷΞϯνύλʔϯΛ౿ΜͰ͍Δ w "MM&BDI1BUUFSO w ϝϞϦϦιʔεʹՃ͑ɺ$16Ϧιʔε΋ແବʹফඅ 34

Slide 35

Slide 35 text

3VCZ"HHSFHBUJPO1BUUFSO ղܾࡦ w ૉ௚ʹ3%#ͷू໿ɾूܭؔ਺Λ࢖͍·͠ΐ͏ w <3VCZ>TPSU@CZSFWFSTF➜<42->PSEFS w <3VCZ>UBLF➜<42->MJNJU w <3VCZ>QPTUTTVN MJLF@DPVOU ➜<42-> HSPVQ VTFS@JE TFMFDU l46. MJLF@DPVOU z 35

Slide 36

Slide 36 text

վળޙιʔείʔυ 36

Slide 37

Slide 37 text

վળޙιʔείʔυ ϕϯνϚʔΫ 37 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ TFDT .#

Slide 38

Slide 38 text

38 -PPLT(PPE5P:PV

Slide 39

Slide 39 text

N+1 Queries Pattern 39 Anti-Pattern

Slide 40

Slide 40 text

40 N+1 /2VFSJFT1BUUFSO w ৄ͍͠આ໌͸লུ w 3BJMTॳ৺ऀ͕࠷΋౿Έ΍͍͢ύλʔϯ

Slide 41

Slide 41 text

/2VFSJFT1BUUFSOղܾࡦ w ϞσϧʹJODMVEFTΛ෇༩ w ʲ5JQTʳKPJOT JODMVEFT QSFMPBE FBHFS@MPBEͷҧ͍Λҙࣝ ͯ͠࢖͍෼͚ΒΕΔͱϕετ w SFG"DUJWF3FDPSEͷKPJOTͱQSFMPBEͱJODMVEFTͱ FBHFS@MPBEͷҧ͍ 41

Slide 42

Slide 42 text

վળޙιʔείʔυ 42

Slide 43

Slide 43 text

վળޙιʔείʔυ ϕϯνϚʔΫ 43 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ̍ TFDT .# վળ̎ TFDT .#

Slide 44

Slide 44 text

վળޙιʔείʔυ ϕϯνϚʔΫ 44 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ̍ TFDT .# վળ̎ TFDT .# ࣄྫ̎݁Ռ ࣮ߦ࣌ؒ͸໿ഒߴ଎ԽɺϝϞϦফඅ΋෼ͷҎԼ·Ͱ ཈͑Δ͜ͱ͕Ͱ͖·ͨ͠ɻ

Slide 45

Slide 45 text

ࣄྫ̏ Ϣʔβʔຖͷهࣄͷ͍͍Ͷ਺ʢMJLF@DPVOUʣ߹ܭ͕ଟ͍ॱͰ 501ϢʔβʔͷϢʔβʔ*%ҰཡΛग़ྗ͢Δɻ 45 ˞Ϣʔβʔ*%Ұཡ͸"SSBZͷܗࣜͰඪ४ग़ྗͰ͖ͯΕ͹Α͠ͱ͢Δ

Slide 46

Slide 46 text

46 ΦϦδφϧιʔείʔυ

Slide 47

Slide 47 text

ΦϦδφϧίʔυ ϕϯνϚʔΫ 47 5JNF .FNPSZ ΦϦδφϧ TFDT .#

Slide 48

Slide 48 text

48 -PPLT(PPE5P:PV

Slide 49

Slide 49 text

Unnecessary Query Pattern 49 Anti-Pattern

Slide 50

Slide 50 text

6OOFDFTTBSZ2VFSZ1BUUFSO ໰୊఺ w ຊདྷඞཁͰ͸ͳ͍ϦιʔεΛऔಘ͍ͯ͠Δ͜ͱ w ແବͳ42-ൃߦɾϝϞϦফඅ͕ൃੜ 50

Slide 51

Slide 51 text

6OOFDFTTBSZ2VFSZ1BUUFSO ղܾࡦ w ෆඞཁͳࢀরΛ࡟আ w QPTUVTFSJE➜QPTUVTFS@JE 51

Slide 52

Slide 52 text

վળޙιʔείʔυ 52

Slide 53

Slide 53 text

վળޙιʔείʔυ ϕϯνϚʔΫ 53 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ TFDT .#

Slide 54

Slide 54 text

54 -PPLT(PPE5P:PV

Slide 55

Slide 55 text

Unnecessary Model Initialization Pattern 55 Anti-Pattern

Slide 56

Slide 56 text

w ෆඞཁͳ"DUJWF3FDPSEϞσϧͷੜ੒͕ൃੜ w "DUJWF3FDPSEͷϞσϧੜ੒ίετ͸ۃΊͯߴ͍ w "3ϞσϧΛੜ੒͠ͳͯ͘ࡁΉͷͰ͋Ε͹ɺͦΕʹӽ͠ ͨ͜ͱ͸ͳ͍ 6OOFDFTTBSZ.PEFM*OJU1BUUFSO ໰୊఺ 56

Slide 57

Slide 57 text

6OOFDFTTBSZ.PEFM*OJU1BUUFSO ղܾࡦ w "DUJWF3FDPSEϞσϧΛੜ੒ΛӌճͰ͖ΔϝιουΛ࢖༻ w TFMFDU➜QMVDL 57

Slide 58

Slide 58 text

վળޙιʔείʔυ 58

Slide 59

Slide 59 text

վળޙιʔείʔυ ϕϯνϚʔΫ 59 5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ̍ TFDT .# վળ̎ TFDT .#

Slide 60

Slide 60 text

5JNF .FNPSZ ΦϦδφϧ TFDT .# վળ̍ TFDT .# վળ̎ TFDT .# վળޙιʔείʔυ ϕϯνϚʔΫ 60 ࣮ߦ࣌ؒɺϝϞϦফඅͱ΋ʹఔ౓·Ͱվળ͢Δ͜ͱ͕Ͱ ͖·ͨ͠ɻ ࣄྫ̏݁Ռ 60

Slide 61

Slide 61 text

"OUJ1BUUFSOT "MM&BDI1BUUFSO /6QEBUF2VFSJFT1BUUFSO 3VCZ"HHSFHBUJPO1BUUFSO /2VFSJFT1BUUFSO 6OOFDFTTBSZ2VFSZ1BUUFSO 6OOFDFTTBSZ.PEF*OJUJBMJ[BUJPO1BUUFSO 61

Slide 62

Slide 62 text

·ͱΊ w "DUJWF3FDPSE͸ॾਕͷ݋ w ਖ਼͘͠࢖͑͹ڧྗͳπʔϧͰ͋ΔҰํɺޡͬͨ࢖͍ํΛ ͢ΔͱϘτϧωοΫΛ࡞Γग़͠΍͍͢ w ීஈ͔ΒΫΤϦϩάΛோΊͯσʔλϕʔεͷύϑΥʔϚϯ εΛҙࣝͯ͠ίʔυΛॻ͘श׳Λ͚ͭΔ w ίʔυϨϏϡʔ΋ಉ༷ 62

Slide 63

Slide 63 text

ͱ͸͍͑ w Ұൠతʹσʔλϕʔεͷ࠷దԽͷํ͕γεςϜશମʹޮ͘ ൚༻తͳղܾࡦͱͳΓ΍͍͢ w σʔλϕʔεͷ࠷దԽΛ·ͣ͸ݕ౼ͦ͠ΕΛߦ্ͬͨͰΞ ϓϦέʔγϣϯίʔυͷ࠷దԽΛߦ͏͜ͱ w SFGʰ42-Ξϯνύλʔϯʱ 63

Slide 64

Slide 64 text

"DUJWF3FDPSEΛਖ਼͘͠࢖͍͜ͳͯ͠ ΑΓྑ͍"DUJWF3FDPSEϥΠϑΛʂ 64

Slide 65

Slide 65 text

Thank You 65