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
知って欲しいSQLなこと
Search
soudai sone
July 31, 2016
Technology
4
1.7k
知って欲しいSQLなこと
中国地方DB勉強会 in 岡山のLT資料です
soudai sone
July 31, 2016
Tweet
Share
More Decks by soudai sone
See All by soudai sone
ソフトウェアエンジニアとしてキャリアの螺旋を駆け上がる方法 - 経験と出会いが人生を変える / Career-Anchor-Drive
soudai
14
4k
新婚19年目から学ぶ夫婦円満の正しい歩き方 / Life is beautiful
soudai
11
4.2k
顧客が本当に必要だったもの - パフォーマンス改善編 / Make what is needed
soudai
32
9.3k
仕事を前に進めるためのコツ - 判断と決断と共有 / Aim for the goal
soudai
85
58k
アプリケーションが 正しく動作するということ - 自動テスト編 / Automated Testing
soudai
17
3.3k
Gitlab本から学んだこと - そーだいなるプレイバック / gitlab-book
soudai
8
1.9k
**強い**エンジニアのなり方 - フィードバックサイクルを勝ち取る / grow one day each day
soudai
128
130k
マルチテナントの実現におけるDB設計とRLS / Utilizing RSL in multi-tenancy
soudai
26
8.6k
キャッシュと向き合う、キャッシュと共に生きる / cache pattern
soudai
38
18k
Other Decks in Technology
See All in Technology
When Windows Meets Kubernetes…
pichuang
0
270
20241125 - AI 繪圖實戰魔法工作坊 @ 實踐大學
dpys
1
450
スケールし続ける事業とサービスを支える組織とアーキテクチャの生き残り戦略 / The survival strategy for Money Forward’s engineering.
moneyforward
0
250
rootful・rootless・privilegedコンテナの違い/rootful_rootless_privileged_container_difference
moz_sec_
0
120
Alignment and Autonomy in Cybozu - 300人の開発組織でアラインメントと自律性を両立させるアジャイルな組織運営 / RSGT2025
ama_ch
1
2.1k
AIエージェントに脈アリかどうかを分析させてみた
sonoda_mj
2
140
AI×医用画像の現状と可能性_2024年版/AI×medical_imaging_in_japan_2024
tdys13
1
1.3k
ドメイン駆動設計の実践により事業の成長スピードと保守性を両立するショッピングクーポン
lycorptech_jp
PRO
3
580
Azureの開発で辛いところ
re3turn
0
220
実践! ソフトウェアエンジニアリングの価値の計測 ── Effort、Output、Outcome、Impact
nomuson
0
1.8k
CDKのコードレビューを楽にするパッケージcdk-mentorを作ってみた/cdk-mentor
tomoki10
0
110
Fearsome File Formats
ange
0
580
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
860
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
The Pragmatic Product Professional
lauravandoore
32
6.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Being A Developer After 40
akosma
89
590k
Mobile First: as difficult as doing things right
swwweet
222
9k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Transcript
ͬͯͯཉ͍͠SQLͳ͜ͱ தࠃํ%#ษڧձ
What is it? DB͋Δɺಥવ͘ͳΔ
What is it? ͦΜͳʹཱ͔ͪ͏ ͨͪͷޠͰ͋Δ… ※ϑΟΫγϣϯͰ͢
͋͐͡Μͩ ̍ɹࣗݾհ ̎ɹϧʔϓͰINSERT ̏ɹN+1 ̐ɹςʔϒϧεΩϟϯΛԥΔ ̑ɹ·ͱΊ
͋͐͡Μͩ ̍ɹࣗݾհ ̎ɹϧʔϓͰINSERT ̏ɹN+1 ̐ɹςʔϒϧεΩϟϯΛԥΔ ̑ɹ·ͱΊ
ࣗݾհ ໊લɿીࠜɹେʢͦͶɹ͚ͨͱʣ ྸɿ31ࡀʢࡾਓͷࢠڙ͕͍·͢ʣ ৬ۀɿגࣜձࣾɹΦϛΧϨʢCTOʣ ॴଐɿຊPostgreSQLϢʔβձ ɹɹɹதࠃࢧ෦ ࢧ෦ ɹɹٕज़తʹLLܥݴޠͱ͔RDB͕͖Ͱ͢
͋͐͡Μͩ ̍ɹࣗݾհ ̎ɹϧʔϓͰINSERT ̏ɹN+1 ̐ɹςʔϒϧεΩϟϯΛԥΔ ̑ɹ·ͱΊ
ϧʔϓͰINSERT ͦʔౖ͍ܹͩͨ͠ɻ
ϧʔϓͰINSERT $array = $this->get_hoge(); $this->db->begin(); for ($index = 0; $index
< count($array); $index++) { $this->db->inset($array["{$index}"]) } $this->db->commit();
ϧʔϓͰINSERT $array = $this->get_hoge(); $this->db->begin(); for ($index = 0; $index
< count($array); $index++) { $this->db->inset($array["{$index}"]) } $this->db->commit(); ϧʔϓͷ͚ͩ*/4&35͕ݺΕΔ
ϧʔϓͰINSERT $array = $this->get_hoge(); $this->db->begin(); for ($index = 0; $index
< count($array); $index++) { $this->db->inset($array["{$index}"]) } $this->db->commit(); ϧʔϓͷ͚ͩ*/4&35͕ݺΕΔ ϧʔϓͷ͚ͩDPVOU ͕ݺΕΔ
ϧʔϓͰINSERT ϧʔΫɺόϧΫΠϯαʔτΛ͑
ϧʔϓͰINSERT INSERT INTO hoge (id,name) values (1,’sone1’), (2,’sone2’), (3,’sone3’), (4,’sone4’),
(5,’sone5’), (6,’sone6’) … # ࠷ۙͷFWͳΒྻ͚ͩ͢Ͱ͍͍ this->db->inset($array);
ϧʔϓͰINSERT ͍Ͱ༰ʹΑͬͯ VQEBUFͳΜͰ͢
ϧʔϓͰINSERT $array = $this->get_hoge(); $this->db->begin(); $count = count($array); for ($index
= 0; $index < $count; $index++) { if (empty($array[“{$index}”][“id”])) { $this->db->inset($array["{$index}"]) } else { $this->db->update($array["{$index}"]) } } $this->db->commit();
ϧʔϓͰINSERT ϧʔΫɺ.FSHFจ 614&35 Λ͑ ͔͠͠03.ΫΤϦϏϧμ େ͑ͳ͍
ϧʔϓͰINSERT ——PostgreSQL INSERT INTO ςʔϒϧ໊ VALUES ('̍', '̎', ...ʣ ON
CONFLICT ON CONSTRAINT ੍໊ DO UPDATE SET ΧϥϜ̍='', ΧϥϜ̎='', ...; ——MySQL INSERT INTO ςʔϒϧ໊ VALUES ('̍', '̎', …ʣ ON DUPLICATE KEY ※ࣗͰࢦఆ͢Δ͜ͱ͕ग़དྷͳ͍ UPDATE ΧϥϜ̍='', ΧϥϜ̎='', ...;
͋͐͡Μͩ ̍ɹࣗݾհ ̎ɹϧʔϓͰINSERT ̏ɹN+1 ̐ɹςʔϒϧεΩϟϯΛԥΔ ̑ɹ·ͱΊ
N+1 /
ϧʔϓͰINSERT $array = $this->get_hoge(); $this->db->begin(); for ($index = 0; $index
< count($array); $index++) { $this->db->inset($array["{$index}"]) } $this->db->commit();
N+1 ϧʔϓͷ͚ͩRVFSZ͛Δ
N+1 $users = $this->db->users->get(); $this->db->begin(); foreach ($users as $user_id =>
$user) { if ($user[“pref”] == ‘Ԭࢁ’) { $user[“pref”] = ‘େձ’ } elseif ($user[“pref”] == ‘ౡ’){ $user[“pref”] = ‘ࢁ’ } $this->db->update($user); } $this->db->commit();
N+1 $users = $this->db->users->get(); $this->db->begin(); foreach ($users as $user_id =>
$user) { if ($user[“pref”] == ‘Ԭࢁ’) { $user[“pref”] = ‘େձ’ } elseif ($user[“pref”] == ‘ౡ’){ $user[“pref”] = ‘ࢁ’ } $this->db->update($user); } $this->db->commit(); ͦͦ8)&3&۟ͰQSFGࢦఆͯ͠ 61%"5&ͳΒճ
N+1 ճͷ61%"5&จʹม͑Δ
N+1 UPDATE users SET pref = CASE pref WHEN ‘Ԭࢁ’
THEN ‘େձ’ WHEN ‘ౡ’ THEN ‘ࢁ’ ELSE pref END WHERE pref IN (‘Ԭࢁ’,’ౡ’)
N+1 $"4&ࣜͳͷͰ ৭Μͳͱ͜ΖͰ͑Δ
N+1 $"4&ࣜͳͷͰ ৭Μͳͱ͜ΖͰ͑Δ 03%&3ɹ#:ͱ͔͑Δ
N+1 Ԡ༻ฤ ͭͷ%#Λൺֱͯ͠61%"5&
N+1 JE OBNF BHF TPOF UBLFUPNP
TPVEBJ VTFS@JE CJSUIEBZ QSFG ౡ ࢁ େձ VTFST VTFS@QSPQFSUJFT
N+1 UPDATE users AS u, (SELECT users.id AS id, CASE
WHEN user_properties.birthday > ‘1996-07-30’ THEN ‘ϋλν’ ELSE age END AS age FROM users INNER JOIN user_properties ON user.id = user_properties.user_id AND user_properties.pref IN (‘ࢁ’,’ͳ͝’,’େձ’)) AS tmp SET u.age = tmp.age WHERE u.id = tmp.id
N+1 JE OBNF BHF TPOF UBLFUPNP ϋλν
TPVEBJ VTFS@JE CJSUIEBZ QSFG ౡ ࢁ େձ
͋͐͡Μͩ ̍ɹࣗݾհ ̎ɹϧʔϓͰINSERT ̏ɹN+1 ̐ɹςʔϒϧεΩϟϯΛԥΔ ̑ɹ·ͱΊ
ςʔϒϧεΩϟϯΛԥΔ ϧʔΫɺۚͷؙΛ͑ʢਅإ
ςʔϒϧεΩϟϯΛԥΔ .Z42-ͳΒ44% 1PTUHSF42-ͳΒϝϞϦΛ ·ͣ૿͢
ςʔϒϧεΩϟϯΛԥΔ 1PTUHSF42-͔Β ςʔϒϧεΩϟϯΛ ύϥϨϧΫΤϦʢฒྻॲཧʣग़དྷΔ
ςʔϒϧεΩϟϯΛԥΔ 1PTUHSF42-͔Β ςʔϒϧεΩϟϯΛ ύϥϨϧΫΤϦʢฒྻॲཧʣग़དྷΔ $16ͷΛ૿ͯ͠ԥΕΔΑ͏ʹͳΔ
ςʔϒϧεΩϟϯΛԥΔ ͦΕͰ͍ͳΒ ύʔςʔγϣϯ͔γϟʔσΟϯά
ςʔϒϧεΩϟϯΛԥΔ ͦΕ͕͍࣌͠ ˞ྫ͑σʔλϋεΣΞ ʢ%)8ʣ
ςʔϒϧεΩϟϯΛԥΔ ͦ͏͍͏࣌༻%#Λങ͓͏ ʢ˓˓˓˓ສ͘Β͍͢Δͭʣ
ςʔϒϧεΩϟϯΛԥΔ ςʔϒϧεΩϟϯ͠ͳ͍ઃܭ͕େࣄ
͋͐͡Μͩ ̍ɹࣗݾհ ̎ɹϧʔϓͰINSERT ̏ɹN+1 ̐ɹςʔϒϧεΩϟϯΛԥΔ ̑ɹ·ͱΊ
·ͱΊ ษڧͯ͘͠ΕɺͨͷΉ
·ͱΊ +16(ϝϯόʔΛืूͯ͠·͢
·ͱΊ ϙδγϣϯਓΛҭͯΔ
·ͱΊ ࣍ظࢧ෦܅ͩʂʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ