Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
DB呼び出し回数を減らしてコア機能を高速化した話
Search
Tasuku Nakagawa
April 09, 2024
0
290
DB呼び出し回数を減らしてコア機能を高速化した話
Tasuku Nakagawa
April 09, 2024
Tweet
Share
More Decks by Tasuku Nakagawa
See All by Tasuku Nakagawa
withContextってスレッド切り替え以外にも使えるって知ってた?
t45k
3
2.5k
R2DBCを導入しようとした話
t45k
0
420
コードレビュー改善のためにJenkinsとIntelliJ IDEAのプラグインを自作してみた話
t45k
0
120
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.2k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
GraphQLとの向き合い方2022年版
quramy
49
14k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Statistics for Hackers
jakevdp
799
220k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
How to Think Like a Performance Engineer
csswizardry
25
1.8k
Testing 201, or: Great Expectations
jmmastey
45
7.6k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
A better future with KSS
kneath
239
17k
Bash Introduction
62gerente
614
210k
[RailsConf 2023] Rails as a piece of cake
palkan
56
5.8k
Transcript
DBݺͼग़͠ճΛݮΒͯ͠ ίΞػೳΛߴԽͨ͠ 2024/04/09 Kyoto Tech Talk Task
Task / த ক GitHub: T45K X (چTwitter): @getupmax ϒϩά:
https://moneyforward-dev.jp/entry/2024/03/28/185934
͜ͷൃදͰ͢͜ͱ • ࢲͨͪͷϓϩμΫτͰߦͬͨߴԽ • ݁Ռͱͯ͠ى͖ͯ͠·ֹͬͨۚෆ߹ • ͔ͦ͜ΒֶΜͩڭ܇ ◦ ૉૣ͘Γ͢ํ๏ ◦
ߦͬͨϙετϞʔςϜͷհ
None
ϓϩμΫτͱ ࿈݁ձܭ
ϚωʔϑΥϫʔυ Ϋϥυ࿈݁ձܭ https://biz.moneyforward.com/consolidated-accounting/
୯ମձܭ ձࣾA ༁ ߴࢼࢉද ୯ମࡒॾද
࿈݁ձܭ ձࣾA ձࣾB ձࣾC
࿈݁άϧʔϓ ࿈݁ձܭ ձࣾA ձࣾB ձࣾC
࿈݁άϧʔϓ ࿈݁ձܭ ձࣾA ձࣾB ձࣾC άϧʔϓձࣾͷߴࢼࢉද + ࿈݁༁ ࿈݁ࡒॾද
ίΞػೳ
Ϋϥυ࿈݁ձܭͷίΞػೳ Ϣʔβ͕࿈݁ࡒॾදͷֹۚΛ ߋ৽͠͏Δૢ࡞Λͨ࣌͠ʹ ͦͷ··࿈݁ࡒॾදΛߋ৽͢Δ
ྫ) ߴࢼࢉදͷऔࠐ ձࣾAͷ ୲ऀ Ϋϥυ࿈݁ձܭ ձࣾAͷ ߴࢼࢉද
ྫ) ߴࢼࢉදͷऔࠐ ձࣾAͷ ୲ऀ 1. ߴࢼࢉදͷऔࠐ 2. ݸผࡒॾදͷ࡞ 3. ଞࣾͷࡒॾදͱ߹Θͤͯ
࿈݁ࡒॾදͷ࡞ Ϋϥυ࿈݁ձܭ ձࣾAͷ ߴࢼࢉද
ྫ) ߴࢼࢉදͷऔࠐ ձࣾAͷ ୲ऀ 1. ߴࢼࢉදͷऔࠐ 2. ݸผࡒॾදͷ࡞ 3. ଞࣾͷࡒॾදͱ߹Θͤͯ
࿈݁ࡒॾදͷ࡞ Ϋϥυ࿈݁ձܭ ձࣾAͷ ߴࢼࢉද
ݸผࡒॾදͷ࡞ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ظ७རӹͷܭࢉ ҝସࠩଛͷܭࢉ ະܭࢉݸผࡒॾද ܭࢉࡁݸผࡒॾද …
class ݸผमਖ਼ద༻αʔϏε( ࡒॾදϦϙδτϦ, ௨՟ࢉαʔϏε, ) { fun ద༻͢Δ(id) { val
ࡒॾද = ࡒॾදϦϙδτϦ.selectById(id) // ݸผमਖ਼ͷద༻Λߦ͏ val ݸผमਖ਼ద༻ࡁࡒॾද = ... ࡒॾදϦϙδτϦ.update(ݸผमਖ਼ద༻ࡁࡒॾද) // ࣍ͷॲཧͷݺͼग़͠ ௨՟ࢉαʔϏε.ࢉ͢Δ(id)
ݸผࡒॾදͷ࡞ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ظ७རӹͷܭࢉ ҝସࠩଛͷܭࢉ … select select select select
update update update update
ݸผࡒॾදͷҰׅߋ৽ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ظ७རӹͷܭࢉ ҝସࠩଛͷܭࢉ … ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ظ७རӹͷܭࢉ ҝସࠩଛͷܭࢉ
… ձࣾA ձࣾB …
ߴԽ
ߴԽલ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ظ७རӹͷܭࢉ ҝସࠩଛͷܭࢉ … select select select select
update update update update
ߴԽલ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ظ७རӹͷܭࢉ ҝସࠩଛͷܭࢉ … select select select select
update update update update ͱ͍ͯ
DBݺͼग़͠ճΛݮΒͤΕ ߴԽͰ͖ͦ͏
ϝϞϦʹࡌͤΔ
ߴԽޙ લॲཧ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ޙॲཧ … select get get set
set update
ߴԽޙ લॲཧ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ޙॲཧ … select get get set
set update
ߴԽޙ લॲཧ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ޙॲཧ … select get get set
set update
ߴԽޙ લॲཧ ݸผमਖ਼ͷద༻ ௨՟ͷࢉ ޙॲཧ … select get get set
set update
تͼͷ
ܭࢉϩδοΫΛ৮ΒͣʹߴԽͰ͖ͨ
ֹۚෆ߹োͱݪҼ
ߴԽϦϦʔε͔Β1िؒ΄Ͳͨͬͨ͋Δ
͓٬༷͔Β͍߹Θͤ
ʮԿֹ͔͓͔͍ۚ͠ΜͰ͚͢Ͳʯ
ٸ͍ͰΓ͠
ܭࢉϩδοΫ৮ͬͯͳ͍ͷʹͲ͏ͯ͠
ʢݩ͔Βʣݸผࡒॾදʹ ಉҰՊͷֹ͕ۚ2ؚͭ·Ε͍ͯͨ
ݸผࡒॾදʮצఆՊʯͱʮֹۚʯͷλϓϧ ͷϦετ ݪҼʢܭࢉॲཧʣ צఆՊA ظ७རӹ צఆՊB 100 300 200
ʮظ७རӹʯͷܭࢉॲཧ ݪҼʢܭࢉॲཧʣ צఆՊA צఆՊB 100 200 300 ظ७རӹ צఆՊA צఆՊB
100 200
ܭࢉલ͔Βظ७རӹΛؚΉ͜ͱ͕͋Δ... ݪҼʢܭࢉॲཧʣ צఆՊA ظ७རӹ 100 300 200 צఆՊB
ܭࢉޙɺظ७རӹͷλϓϧ͕৽نՃ ݪҼʢܭࢉॲཧʣ צఆՊA ظ७རӹ 100 300 200 צఆՊB ظ७རӹ 300
צఆՊA ظ७རӹ 100 300 200 צఆՊB
on duplicate key updateͰinsert ݪҼʢߋ৽ॲཧʣ צఆՊA ظ७རӹ 100 300 200
צఆՊB ظ७རӹ 300 צఆՊA 100 200 צఆՊB ظ७རӹ 300 צఆՊA ظ७རӹ 100 300 200 צఆՊB ظ७རӹ 300 insert select
ҙਤ͠ͳ͍ܭࢉॲཧͱ ҙਤ͠ͳ͍ߋ৽ॲཧͰ ݁Ռతʹ߹͍ͯͨ͠
ڭ܇
Γ͠ ࠓճϑΟʔνϟʔϑϥάΛ͏͜ͱͰ σϓϩΠͳ͠Ͱૉૣ͘Γͤͨ ϑΟʔνϟʔϑϥά࠷ߴ
ʮݸผࡒॾදͰצఆՊϢχʔΫʯͱ͍͏ ϧʔϧΛίʔυ্ͰపఈͰ͖ͯͳ͔ͬͨ υϝΠϯϧʔϧΛίʔυʹམͱ͠ࠐΉ όάΛࠞೖͤ͞ͳ͍
• ࿈݁ձܭۀΛ͍ͯ͠Δͱઈରʹؾͮ͘ • Ϣʔβʔͷૢ࡞Λςετʹམͱ͠ࠐΊͯͳ͔ͬͨ ϢʔβʔઢͰͷγφϦΦςετΛॻ͘ όάΛྲྀग़ͤ͞ͳ͍
None