20
1 MySQLの限界に挑戦する(?!) MySQL Casual Talks vol.10 2017-02-01 19:10 LT @meijik

MySQLの限界に挑戦する

Embed Size (px)

Citation preview

Page 1: MySQLの限界に挑戦する

1

MySQLの限界に挑戦する(?!)

MySQL Casual Talks vol.10

2017-02-01 19:10 LT

@meijik

Page 2: MySQLの限界に挑戦する

免責事項

• 本プレゼンテーションにおいて示されている見解は、私自身の見解であって、私の所属する会社・団体の見解を必ずしも反映したものではありません。ご了承ください。

2

Page 3: MySQLの限界に挑戦する

3

MySQLの限界

• よいこのみんなはこれを見てるよね!

• 限界までMySQLを使い尽くす!!(漢のコンピュータ道)

– http://nippondanji.blogspot.jp/2009/05/mysql.html

• 今回のLTはそれを順に見ていこうとしましたが、結果は。。。。

Page 4: MySQLの限界に挑戦する

4

SQL文の最大長

• 限界までMySQLを使い尽くす!!(漢のコンピュータ道)によると。。。。

– MySQLサーバーが実行出来るSQL文の最大長は、max_allowed_packetシステム変数で表される。max_allowed_packetの最大値は1GBである。max_allowed_packetの値はセッションごとに

も設定可能なので、デフォルトではそこそこの値(16MBなど)に設定しておいて、必要に応じて大きな対を使うと良いだろう。

–デフォルト値は5.6より前は1MB, 5.6以降4MB

Page 5: MySQLの限界に挑戦する

5

無意味なクエリでトライ!

UNION ALLで一連の値を返すクエリを作成。

↓こんな感じ

select repeat(' union all select "b"',4000) into

@a;

set @a= concat('select "a" ', @a);

prepare stmt1 from @a;

execute stmt1;

deallocate prepare stmt1;

Page 6: MySQLの限界に挑戦する

6

できたクエリ

• select "a" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union allselect "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

Page 7: MySQLの限界に挑戦する

7

できたクエリ(一部拡大)

• select "a" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union allselect "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select

"b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all

select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union

all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b" union all select "b"

select “a” union all select “b” union all select “b” …

Page 8: MySQLの限界に挑戦する

8

さて実行!

mysql> select repeat(' union all select "b"',4000)

into @a;

Query OK, 1 row affected (0.00 sec)

mysql> set @a= concat('select "a" ', @a);

Query OK, 0 rows affected (0.00 sec)

mysql> prepare stmt1 from @a;

ERROR 1436 (HY000): Thread stack overrun:

246184 bytes used of a 262144 byte stack, and

16000 bytes needed. Use 'mysqld --

thread_stack=#' to specify a bigger stack.

Page 9: MySQLの限界に挑戦する

9

スタック不足

• thread_stack

– https://dev.mysql.com/doc/refman/5.6/ja/serve

r-system-variables.html#sysvar_thread_stack

• 各スレッドのスタックサイズ。デフォルトの256KBは、通常の操作では十分な大きさです。スレッドスタックサイズが小さすぎるとサーバで処理できる SQL ステートメントの複雑さ、ストアドプロシージャーの再帰の深さなど、メモリーを大量に消費する処理が制限されます。

Page 10: MySQLの限界に挑戦する

10

じゃ2倍の512KBで

• 成功!

• じゃreapeatも二倍の8000で!

mysql> prepare stmt1 from @a;

ERROR 1064 (42000): memory exhausted

near '"b" union all select "b"' at line 1

→失敗orz

Page 11: MySQLの限界に挑戦する

11

ERROR 1064 (42000)

• 通常クエリが文法的に間違っているときによくでる。

• perror 1064

– MySQL error code 1064

(ER_PARSE_ERROR): %s near ‘%-.80s’ at

line %d

• ただ今回は文法的にはあっている(はず)でキーワード(%s) が “memory exhausted “

Page 12: MySQLの限界に挑戦する

12

ソースをfindstr

(windowsのgrepライクなコマンド) • mysql-5.7.17¥sql>findstr /nsi

/c:"memory exhausted" * • sql_hints.yy.cc:1362: yyoverflow (YY_("memory

exhausted"),

• sql_hints.yy.cc:2099: yyerror (thd, scanner, ret,

YY_("memory exhausted"));

• sql_yacc.cc:19899: yyoverflow (YY_("memory

exhausted"),

• sql_yacc.cc:41019: yyerror (&yylloc, YYTHD,

YY_("memory exhausted"));

Page 13: MySQLの限界に挑戦する

13

構文解析と字句解析

• クエリの解析

–字句解析

–構文解析

• 構文的には正しい(無意味な)複雑なクエリを構文解析していて、内部的に用意しておいたメモリを使い果たしたらしい。。。

• 通常のプログラムでは構文解析にyacc, 字句解析にlexを利用する。

–拡張子.yyはyaccのソースファイル

Page 14: MySQLの限界に挑戦する

14

bisonとflex

• クエリの解析

–字句解析: lex -> GNU版がflex

–構文解析: yacc -> GNU版がbison

Page 15: MySQLの限界に挑戦する

15

MySQLの場合

• クエリの解析

–字句解析: 自前で行っている

–構文解析: BNFで記述したソースをGNU bisonにてコンパイルして行っている。

• BNF(バッカス・ナウア記法)

• バッカス・ナウア記法(英: Backus-Naur

form)とは、文脈自由文法を定義するのに用いられるメタ言語のことで、一般にBNFやBN

記法と略される。(wikipediaより引用)

Page 16: MySQLの限界に挑戦する

16

SQL標準をBNFで

• 初期のSQLは書籍などでBNF(バッカス・ナウア記法)の記述があった。

• 現在奇特な方がSQL-92, 99, 2003のBNFを公開してなさる。(多分読みきれん)

– https://github.com/ronsavage/SQL

• これを使えば多分クエリの整形ツールをつくったり(多分できる)、RDBMSを作ったりすることができると思われます。(多分無理)

Page 17: MySQLの限界に挑戦する

17

まとめ

• MySQLのクエリの最大長(max_allowed_packet)

• スタックに引っかかる場合→スタックサイズを大きくして対応

• 構文解析でメモリを使い果たした場合→簡単な対応なし(まぁほとんど起こらない)

• MySQLのビルドになぜbisonが必要なのか、と@kumagiのツイートの意味が分かった。

Page 18: MySQLの限界に挑戦する

宣伝: 書籍情報

• おうちで学べるデータベースのきほん

– DBの初心者はこちらをどうぞ。

–なぜか韓国語版が発売

• 入手方法

– じゃんけんで勝つ。

– もしくは、Amazonでポチる w。

18

Page 19: MySQLの限界に挑戦する

19

THANK YOU

• ご清聴ありがとうございました。

• [fyi] yacc/lexの参考書籍

–日本語

• lex&yaccプログラミング (オライリージャパン)

• Cコンパイラ設計―yacc/lexの応用

• yacc/lex―プログラムジェネレータon UNIX

–英語

• flex&bison (O‘reilly)

– 比較的新しい(2009/08)本(@meijikは未見)

– SQLの例がある(らしい)

Page 20: MySQLの限界に挑戦する

20

MySQLの限界にチョット挑戦した。

MySQL Casual Talks vol.10

2017-02-01 LT

@meijik