Submit Search
Upload
Lt・dbチューニング
•
0 likes
•
202 views
P
pollseed
Follow
初心者用MySQLのインデクスの付け方講座。ハンズオン含める
Read less
Read more
Technology
Report
Share
Report
Share
1 of 22
Download now
Download to read offline
Recommended
Cocoa勉強会#38-UITableViewテーブル内のナビゲーション
Cocoa勉強会#38-UITableViewテーブル内のナビゲーション
Masayuki Nii
Ruby入門者用文法
Ruby入門者用文法
pollseed
Javaによる基礎デザインパターン
Javaによる基礎デザインパターン
pollseed
サービスについて
サービスについて
pollseed
リファクタリングについてのお話
リファクタリングについてのお話
pollseed
DQH 初回特典のダウンロード方法と装備の見た目
DQH 初回特典のダウンロード方法と装備の見た目
pollseed
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
Recommended
Cocoa勉強会#38-UITableViewテーブル内のナビゲーション
Cocoa勉強会#38-UITableViewテーブル内のナビゲーション
Masayuki Nii
Ruby入門者用文法
Ruby入門者用文法
pollseed
Javaによる基礎デザインパターン
Javaによる基礎デザインパターン
pollseed
サービスについて
サービスについて
pollseed
リファクタリングについてのお話
リファクタリングについてのお話
pollseed
DQH 初回特典のダウンロード方法と装備の見た目
DQH 初回特典のダウンロード方法と装備の見た目
pollseed
2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
UEHARA, Tetsutaro
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
博三 太田
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
FumieNakayama
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
FumieNakayama
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
iPride Co., Ltd.
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
sugiuralab
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
Yuki Kikuchi
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
Hiroki Ichikura
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
akihisamiyanaga1
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
More Related Content
Recently uploaded
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
UEHARA, Tetsutaro
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
博三 太田
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
FumieNakayama
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
FumieNakayama
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
iPride Co., Ltd.
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
sugiuralab
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
Yuki Kikuchi
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
Hiroki Ichikura
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
akihisamiyanaga1
Recently uploaded
(9)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
デジタル・フォレンジックの最新動向(2024年4月27日情洛会総会特別講演スライド)
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
モーダル間の変換後の一致性とジャンル表を用いた解釈可能性の考察 ~Text-to-MusicとText-To-ImageかつImage-to-Music...
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
クラウドネイティブなサーバー仮想化基盤 - OpenShift Virtualization.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
AWS の OpenShift サービス (ROSA) を使った OpenShift Virtualizationの始め方.pdf
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
自分史上一番早い2024振り返り〜コロナ後、仕事は通常ペースに戻ったか〜 by IoT fullstack engineer
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
CTO, VPoE, テックリードなどリーダーポジションに登用したくなるのはどんな人材か?
Featured
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
Skeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
Vit Horky
The six step guide to practical project management
The six step guide to practical project management
MindGenius
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
Featured
(20)
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
Skeleton Culture Code
Skeleton Culture Code
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Getting into the tech field. what next
Getting into the tech field. what next
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
How to have difficult conversations
How to have difficult conversations
Introduction to Data Science
Introduction to Data Science
Time Management & Productivity - Best Practices
Time Management & Productivity - Best Practices
The six step guide to practical project management
The six step guide to practical project management
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
Lt・dbチューニング
1.
DBチューニング ∼MySQL編∼ by poll seed
2.
まずは、テーブルを作る 商品テーブル 都道府県 商品 分類
3.
インデックスなし 1. 商品全体テーブルを作成 CREATE TABLE
`item` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `pref_code` int(11) NOT NULL, `items_code` int(11) NOT NULL, `classify_code` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 2. 各々のcodeに対して正規化しテーブルを生成→比較のためキーなどは設けない
4.
クエリの実行計画 1. Explainにより実行計画を調べる explain SELECT * FROM
item i inner join pref p on i.pref_code = p.pref_code WHERE p.pref_code = 1 ; 2. 実行計画結果 # id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra 1, SIMPLE, p, const, PRIMARY, PRIMARY, 4, const, 1, 1, SIMPLE, i, ALL, , , , , 21, Using where ※「i」テーブルのtypeが「ALL」となっている
5.
ALLとは そもそもtypeとはなんだろうか 結論から言えば、「結合型」 さらに言えば、ALLと言うのは「フルテーブルス キャン」のALLである。ただの爆遅結合というこ と 要は、ALLなんて出してはいけない
6.
改善しよう typeで爆速なのがsystem, constである。 systemというのは1レコードのみなので「const」, 次に早い「eq_ref」を目指すというのがエンジニ アの必須事項
7.
手を加える1 1. indexの追加 ALTER TABLE
`test`.`item` ADD INDEX `idx_item1` USING BTREE (`pref_code` ASC); 2. 実行計画 # id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra 1, SIMPLE, p, const, PRIMARY, PRIMARY, 4, const, 1, 1, SIMPLE, i, ALL, idx_item1, , , , 21, Using where ※idx_item1がキーの候補として出てきた。ただし、まだALLである。
8.
pref_codeにインデックスをつ けることについて考える1 通常、B木インデックスをつける時は、カーディナ リティの高い列を選ぶことが前提としてある なぜならば、B木をより平坦にする必要があるから である。平坦というのはリーフが多い状態 具体的にはレコード全体の5%以下に抑えることがで きる場合、インデックスを張るのは有効と言われる
9.
pref_codeにインデックスをつ けることについて考える2 pref_codeというのは都道府県である。よって、47 件のデータが存在する。1件につき全体のデータを 大体2.1%程度に絞る事が可能 先の前提を元にすれば充分インデックスを張るの が有効だと言えよう
10.
手を加える2 1. 外部キーを付ける ALTER TABLE
`test`.`item` ADD CONSTRAINT `fk_item1` FOREIGN KEY (`pref_code`) REFERENCES `test`.`pref` (`pref_code`) ON DELETE NO ACTION ON UPDATE NO ACTION; 2. 実行計画 # id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra 1, SIMPLE, p, const, PRIMARY, PRIMARY, 4, const, 1, 1, SIMPLE, i, ALL, idx_item1, , , , 21, Using where
11.
変わっていない?
12.
もう一度先ほどの話を見て みよう
13.
pref_codeにインデックスをつ けることについて考える1 通常、B木インデックスをつける時は、カーディナ リティの高い列を選ぶことが前提としてある なぜならば、B木をより平坦にする必要があるから である。平坦というのはリーフが多い状態 具体的にはレコード全体の5%以下に抑えることがで きる場合、インデックスを張るのは有効と言われる
14.
どういうことか 言うなれば、概算としてレコードを2.1%におさえ れるという想定で先のインデックスを張り、外部 キーまで加えたのにALLのまま 現場では得てしてこういうことが起こる。 それでは、このカラクリを紐解いてみよう。
15.
件数ベースで確認 1. 件数を見てみよう SELECT COUNT(*), COUNT(IF(pref_code
= 1, 1, NULL)) FROM test.item; 2. 件数の結果 # count(*), count(if(pref_code=1,1,null)) 21, 16 ※全体の76%以上がpref_code=1となっている。 想定では高いと思っていたが、データ的にはカーディナリティが低いのである。
16.
データ確認1 select * from
item where pref_code <> 1; # id, pref_code, items_code, classify_code 1, 3, 1, 1 18, 2, 1, 1 19, 2, 2, 1 20, 2, 3, 1 21, 2, 4, 1 ※3のカーディナリティは4.7%と高いため、こいつで条件を見てみる
17.
データ確認2 1. 実行計画をpref_code=3で見てみる explain SELECT * FROM
item i inner join pref p on i.pref_code = p.pref_code where p.pref_code = 3; 2. 結果 # id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra 1, SIMPLE, p, const, PRIMARY, PRIMARY, 4, const, 1, 1, SIMPLE, i, ref, idx_item1, idx_item1, 4, const, 1, ※問題なくインデックスが使われている
18.
まとめ B木インデックスをつける時は、カーディナリ ティの高い列を選ぶ レコード全体の5%以下に抑えることができる場 合、インデックスを張るのは有効 ただし、データ件数によっては、インデックスが 有効でない場合も実際の現場では多い
19.
発展として 複合インデックスについてはどうか? カーディナリティの高い順に貼っていくのが妥 当と思われる 現場でデータ件数を元に決めるのがよい 検索するときも、インデックスが効くように Where文で検索する順序に注意
20.
余力があればハンズオン 以下のURLにアクセス https://raw.githubusercontent.com/pollseed/ linux_setup/master/src/hackathon/db/ tuning_before.sql Q1∼Q3をできるところまで
21.
資料 以下のドキュメントも参考にして下さい https://raw.githubusercontent.com/pollseed/ linux_setup/master/src/hackathon/db/doc.md
22.
Fin
Download now