Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
知って欲しいSQLなこと
Search
soudai sone
PRO
July 31, 2016
Technology
1.9k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
知って欲しいSQLなこと
中国地方DB勉強会 in 岡山のLT資料です
soudai sone
PRO
July 31, 2016
More Decks by soudai sone
See All by soudai sone
ビジネスを止めない技術的負債の返済のための戦略とその手法 - 技術的負債と向き合う / Complexity and Simplicity
soudai
PRO
3
750
AIを活用するために決めた "やらないこと" - 価値に注目する / Not betting on AI
soudai
PRO
3
720
目の前の楽しいが人生を変える - コミュニティの螺旋の歩き方と楽しむコツ / change your life
soudai
PRO
5
820
医療の現場を変革に挑戦した半年間の軌跡 - PythonとAIで現場を変える / From Code to Care
soudai
PRO
1
760
形骸化しない社内勉強会の取り組み - オーナーシップの作り方 / In-house study session
soudai
PRO
1
440
Djangoユーザが知っ得なPostgreSQL機能 - 設計の選択肢を増やす / Djang-use-PostgreSQL
soudai
PRO
1
350
AI時代における具体と抽象の往復 - 日常にチャンスがある / Moving Between the Concrete
soudai
PRO
9
4.6k
制約を設計する - 非決定性との境界線 / Designing constraints
soudai
PRO
6
4.8k
APMの世界から見るOpenTelemetryのTraceの世界 / OpenTelemetry in the Java
soudai
PRO
2
940
Other Decks in Technology
See All in Technology
How we choose session proposals
kawaguti
PRO
1
180
1週間、Opus封印でお願いします
kintotechdev
0
310
AIに任せられるFlutterコードベースを作る 〜仕様をコードで表し、ハーネスで守る〜
myohei
1
320
Let's Change Our World—Again
snoozer05
PRO
6
700
なぜAI時代に技術記事を書くのか?どう書くのか? 〜bunの場合〜
kworkdev
PRO
0
150
Vue.jsのGitHubから学ぶ意思決定
yut0naga1_fa
1
320
AIコーディングエージェント開発_全体像
happysamurai294
0
110
渡米前の準備を制す!re:Invent 2026徹底準備術 / The Ultimate Preparation Guide for re:Invent 2026
yuj1osm
1
340
cdk deployとcdkd deploy、実際にデプロイして5倍速近くの差を実測した
yama3133
1
100
ビジネス部門からPOを迎えての半年間の進歩と課題
zozotech
PRO
0
150
書き手が人でもAIでも正しくなる道、SREを埋め込んだDeveloper Platform / tech drivers day8
chmikata
4
790
Filter What the LLM Reads: RAG Governance with AgentCore Gateway
yakumo
1
110
Featured
See All Featured
Bash Introduction
62gerente
615
220k
Navigating Team Friction
lara
192
16k
Designing Powerful Visuals for Engaging Learning
tmiket
1
620
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
エンジニアに許された特別な時間の終わり
watany
109
250k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
580
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
550
The Invisible Side of Design
smashingmag
301
52k
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(ϝϯόʔΛืूͯ͠·͢
·ͱΊ ϙδγϣϯਓΛҭͯΔ
·ͱΊ ࣍ظࢧ෦܅ͩʂʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ