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
顧客が本当に必要だったもの - パフォーマンス改善編 / Make what is needed
soudai
30
8.4k
仕事を前に進めるためのコツ - 判断と決断と共有 / Aim for the goal
soudai
84
36k
アプリケーションが 正しく動作するということ - 自動テスト編 / Automated Testing
soudai
15
3k
Gitlab本から学んだこと - そーだいなるプレイバック / gitlab-book
soudai
8
1.8k
**強い**エンジニアのなり方 - フィードバックサイクルを勝ち取る / grow one day each day
soudai
119
110k
マルチテナントの実現におけるDB設計とRLS / Utilizing RSL in multi-tenancy
soudai
26
8.2k
キャッシュと向き合う、キャッシュと共に生きる / cache pattern
soudai
38
16k
RDBアンチパターンと戦う - 削除フラグ 完全攻略ガイド / delete flag
soudai
30
18k
コミュニティと共に生きる - キャリアの螺旋 / live with community
soudai
8
5.9k
Other Decks in Technology
See All in Technology
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.3k
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
680
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
170
障害対応指揮の意思決定と情報共有における価値観 / Waroom Meetup #2
arthur1
5
470
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
170
SSMRunbook作成の勘所_20241120
koichiotomo
2
130
The Rise of LLMOps
asei
5
1.3k
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
370
複雑なState管理からの脱却
sansantech
PRO
1
140
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
フルカイテン株式会社 採用資料
fullkaiten
0
40k
個人でもIAM Identity Centerを使おう!(アクセス管理編)
ryder472
3
190
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Music & Morning Musume
bryan
46
6.2k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Visualization
eitanlees
145
15k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Become a Pro
speakerdeck
PRO
25
5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Code Reviewing Like a Champion
maltzj
520
39k
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(ϝϯόʔΛืूͯ͠·͢
·ͱΊ ϙδγϣϯਓΛҭͯΔ
·ͱΊ ࣍ظࢧ෦܅ͩʂʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ