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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
soudai sone
PRO
July 31, 2016
Technology
4
1.8k
知って欲しいSQLなこと
中国地方DB勉強会 in 岡山のLT資料です
soudai sone
PRO
July 31, 2016
Tweet
Share
More Decks by soudai sone
See All by soudai sone
APMの世界から見るOpenTelemetryのTraceの世界 / OpenTelemetry in the Java
soudai
PRO
0
220
失敗できる意思決定とソフトウェアとの正しい歩き方_-_変化と向き合う選択肢/ Designing for Reversible Decisions
soudai
PRO
8
1.7k
外部キー制約の知っておいて欲しいこと - RDBMSを正しく使うために必要なこと / FOREIGN KEY Night
soudai
PRO
15
6k
手を動かしながら学ぶデータモデリング - 論理設計から物理設計まで / Data modeling
soudai
PRO
34
8.7k
これからアウトプットする人たちへ - アウトプットを支える技術 / that support output
soudai
PRO
20
8.3k
コミュニティと計画的偶発性理論 - 出会いが人生を変える / Life-Changing Encounters
soudai
PRO
8
3.8k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
62
51k
変化に強いテーブル設計の勘所 / Table design that is resistant to changes
soudai
PRO
68
20k
抽象化をするということ - 具体と抽象の往復を身につける / Abstraction and concretization
soudai
PRO
51
34k
Other Decks in Technology
See All in Technology
Data Hubグループ 紹介資料
sansan33
PRO
0
2.8k
管理者向けGitHub Enterpriseの運用Tips紹介: 人にもAIにも優しいプラットフォームづくり
yuriemori
0
100
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.1k
白金鉱業Meetup_Vol.22_Orbital Senseを支える衛星画像のマルチモーダルエンベディングと地理空間のあいまい検索技術
brainpadpr
1
110
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
71k
開発組織の課題解決を加速するための権限委譲 -する側、される側としての向き合い方-
daitasu
3
170
類似画像検索モデルの開発ノウハウ
lycorptech_jp
PRO
2
580
Snowflake Night #2 LT
taromatsui_cccmkhd
0
320
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3k
ブラックボックス観測に基づくAI支援のプロトコルのリバースエンジニアリングと再現~AIを用いたリバースエンジニアリング~ @ SECCON 14 電脳会議 / Reverse Engineering and Reproduction of an AI-Assisted Protocol Based on Black-Box Observation @ SECCON 14 DENNO-KAIGI
chibiegg
0
130
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
4k
トラブルの大半は「言ってない」x「言ってない」じゃねーか!!
ichimichi
0
300
Featured
See All Featured
Navigating Team Friction
lara
192
16k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
80
Exploring anti-patterns in Rails
aemeredith
2
280
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.1k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Un-Boring Meetings
codingconduct
0
220
The Curse of the Amulet
leimatthew05
1
9.5k
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
200
Joys of Absence: A Defence of Solitary Play
codingconduct
1
300
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
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(ϝϯόʔΛืूͯ͠·͢
·ͱΊ ϙδγϣϯਓΛҭͯΔ
·ͱΊ ࣍ظࢧ෦܅ͩʂʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ