7. ざっくりMySQL 5.7
2013/02 MySQL 5.6.10 GA
2013/04 MySQL 5.7.1 DMR11
2015/03 MySQL 5.7.6 DMR16
2015/04 MySQL 5.7.7 RC
2015/08 MySQL 5.7.8 RC
2015/10 MySQL 5.7.9 GA
2015/12 MySQL 5.7.10 GA
2016/02 MySQL 5.7.11 GA
6/69
8. MySQL 5.7
2015/04 MySQL 5.7.7 RC
ここまではまあいい
2015/08 MySQL 5.7.8 RC
JSON型, virtual generated columnの拡張, InnoDB Page
Compression
2015/10 MySQL 5.7.9 GA
innodb_default_row_format, JSON -> operator,
innodb_numa_interleave
2015/12 MySQL 5.7.10 GA
Bug Fix
2016/02 MySQL 5.7.11 GA
InnoDB Tablespace Encryption 7/69
9. MySQL 5.7
2015/04 MySQL 5.7.7 RC
ここまではまあいい
2015/08 MySQL 5.7.8 RC
新機能!!
2015/10 MySQL 5.7.9 GA
新機能!!
2015/12 MySQL 5.7.10 GA
Bug Fix
2016/02 MySQL 5.7.11 GA
新機能!!
8/69
35. 範囲検索でORDER BYまでキーが使えないやつも
mysql> ALTER TABLE Country ADD KEY (population, gnp);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country WHERE Population > 100000000 ORD
ER BY GNP DESC;
+----+-------------+---------+------------+-------+---------------+-----
-------+---------+------+------+----------+-----------------------------
----------+
| id | select_type | table | partitions | type | possible_keys | ke
y | key_len | ref | rows | filtered | Extr
a |
+----+-------------+---------+------------+-------+---------------+-----
-------+---------+------+------+----------+-----------------------------
----------+
| 1 | SIMPLE | Country | NULL | range | Population | Popu
lation | 4 | NULL | 10 | 100.00 | Using index condition; Usin
g filesort |
+----+-------------+---------+------------+-------+---------------+-----
-------+---------+------+------+----------+-----------------------------
----------+
1 row in set, 1 warning (0.00 sec)
34/69
36. generated columnならこの通り
mysql> ALTER TABLE Country ADD is_over_100mil tinyint AS (IF(Population > 100000
00, 1, 0)), ADD KEY (is_over_100mil, GNP);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country WHERE is_over_100mil = 1 ORDER BY GNP DE
SC;
+----+-------------+---------+------------+------+----------------+-------------
---+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | ke
y | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+----------------+-------------
---+---------+-------+------+----------+-------------+
| 1 | SIMPLE | Country | NULL | ref | is_over_100mil | is_over_100m
il | 2 | const | 78 | 100.00 | Using where |
+----+-------------+---------+------------+------+----------------+-------------
---+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
35/69
37. 昇順と降順が混じったソートも
mysql> ALTER TABLE Country ADD KEY (is_over_100mil, GNP, GNPOld);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country USE INDEX(is_over_100mil_2) WHERE is_ove
r_100mil = 1 ORDER BY GNP ASC, GNPOld DESC LIMIT 1;
+----+-------------+---------+------------+------+------------------+-----------
-------+---------+-------+------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | ke
y | key_len | ref | rows | filtered | Extr
a |
+----+-------------+---------+------------+------+------------------+-----------
-------+---------+-------+------+----------+-----------------------------+
| 1 | SIMPLE | Country | NULL | ref | is_over_100mil_2 | is_over_10
0mil_2 | 2 | const | 78 | 100.00 | Using where; Using filesort |
+----+-------------+---------+------------+------+------------------+-----------
-------+---------+-------+------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)
36/69
38. generated columnならこの通り
mysql> ALTER TABLE Country ADD invert_gnpold float(10, 2) AS (0 - GNPOld), ADD K
EY (is_over_100mil, GNP, invert_gnpold);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain SELECT Name FROM Country WHERE is_over_100mil = 1 ORDER BY GNP AS
C, invert_gnpold ASC LIMIT 1;
+----+-------------+---------+------------+------+------------------------------
--------------------+------------------+---------+-------+------+----------+----
---------+
| id | select_type | table | partitions | type | possible_key
s | key | key_len | ref | row
s | filtered | Extra |
+----+-------------+---------+------------+------+------------------------------
--------------------+------------------+---------+-------+------+----------+----
---------+
| 1 | SIMPLE | Country | NULL | ref | is_over_100mil,is_over_100mil
_2,is_over_100mil_3 | is_over_100mil_3 | 2 | const | 78 | 100.00 | Usi
ng where |
+----+-------------+---------+------------+------+------------------------------
--------------------+------------------+---------+-------+------+----------+----
---------+
1 row in set, 1 warning (0.00 sec)
37/69
39. MySQLにないCHECK制約もgenerated columnならこ
の通り
mysql> ALTER TABLE Country ADD v_check tinyint AS (IF(GNPOld < 10
00, NULL, 1)) NOT NULL;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO Country SET Name= 'Dummy', GNPOld= 0.01;
ERROR 1048 (23000): Column 'v_check' cannot be null
mysql> INSERT INTO Country SET Name= 'Dummy', GNPOld= 1000.01;
Query OK, 1 row affected (0.00 sec)
38/69
62. WHERE狙いのキー, ORDER BY狙いのキー
WHEREとORDER BY .. LIMITを同時にカバーできるインデック
スが作成できない時に
WHEREをストレージエンジンに任せてフェッチする⾏を減
らしてファイルソートをExecutorにやらせるか
-
ORDER BY .. LIMITをストレージエンジンに任せて
ExecutorにフィルターさせつつLIMITでループを抜けるの
を期待するか
-
61/69
64. ⼈間がAIに今のところ勝てること
⼈間は得てして 正しい 統計情報を知っているケースがある
サービスの特性(⼥性は少ないからWHERE gender= 'F'
ORDER BY birthdayはWHEREで狙った⽅がいいだろうし、
WHERE gender= 'M' ORDER BY birthdayはORDER BY狙いの
⽅が良いだろう、とか)
-
サンプリングでない実際の分布(InnoDBの統計情報は飽
くまでサンプリングなのでたまに踏み外す)
-
LIMITの考慮(5.6とそれ以前のMySQLはORDER BY ..
LIMITの時でも積極的にWHEREを狙う)
-
63/69
65. オプティマイザーの強化
MySQL 5.6
Multi-Range Read, Batched Key Access(デフォルトOFF),
Semi-JOIN, Materialized
MySQL 5.7
ORDER BY .. LIMIT Optimization, Configurable cost
Model, JOIN optimization
ホントに賢くなってて、「ほらMySQLバカでしょ︖」って
デモがしづらくなった
64/69