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
230
DB呼び出し回数を減らしてコア機能を高速化した話
Tasuku Nakagawa
April 09, 2024
Tweet
Share
More Decks by Tasuku Nakagawa
See All by Tasuku Nakagawa
withContextってスレッド切り替え以外にも使えるって知ってた?
t45k
3
1.9k
R2DBCを導入しようとした話
t45k
0
340
コードレビュー改善のためにJenkinsとIntelliJ IDEAのプラグインを自作してみた話
t45k
0
78
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
38
7.1k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
It's Worth the Effort
3n
183
27k
Code Reviewing Like a Champion
maltzj
520
39k
For a Future-Friendly Web
brad_frost
175
9.4k
What's new in Ruby 2.0
geeforr
343
31k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
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