ãã®ã¨ã³ããªã¯MySQL Casual Advent Calendar 2015ã®8æ¥ç®ã§ãã
MySQL 5.7.6ããoptimizer_switch
ã«derived_merge
ã追å ããããã©ã«ãã§æå¹ã«ãªã£ã¦ãããåºæ¬çã«ããã¯ã»ã£ã¨ãããã ããããµãã¯ã¨ãªãéããªããã¤ãªã®ã§æ°ã«ããªãã¦ããããã§ãããã¡ãã£ã¨éäºæãããã®ã§ããã£ã¨èª¬æãã¾ãã
root@localhost [mysqlcasual] > CREATE TABLE t1 (a int); Query OK, 0 rows affected (0.03 sec) root@localhost [mysqlcasual] > CREATE TABLE t2 (b int); Query OK, 0 rows affected (0.03 sec) root@localhost [mysqlcasual] > INSERT INTO t1 VALUES (1),(2),(3),(4),(5); Query OK, 5 rows affected (0.02 sec) Records: 5 Duplicates: 0 Warnings: 0 root@localhost [mysqlcasual] > INSERT INTO t2 VALUES (1),(2),(3),(4),(5); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0
ããã¾ã§FROMå¥ã®ãµãã¯ã¨ãªã¯ä¸æ¦ãããªã¢ã©ã¤ãºããã¦ããå¤å´ã®ã¯ã¨ãªã¨ãWHEREå¥ã¨ãã¨çµåããã¦ããã
root@localhost [mysqlcasual] > SET optimizer_switch = 'derived_merge=off'; Query OK, 0 rows affected (0.00 sec) root@localhost [mysqlcasual] > EXPLAIN SELECT * FROM t1 JOIN (SELECT * FROM t2) dt; +----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------------+ | 1 | PRIMARY | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | NULL | | 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | Using join buffer (Block Nested Loop) | | 2 | DERIVED | t2 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | NULL | +----+-------------+------------+------------+------+---------------+------+---------+------+------+----------+---------------------------------------+ 3 rows in set, 1 warning (0.00 sec)
ãããderived_merge=on
ã ã¨ãããªã¢ã©ã¤ãºããã«å¤å´ã®æ¡ä»¶ã¨ãã¼ã¸ã§ããããªã¨ãã¯ãã¼ã¸ãããããã«ãªãã®ã ï¼
root@localhost [mysqlcasual] > SET optimizer_switch = 'derived_merge=on'; Query OK, 0 rows affected (0.00 sec) root@localhost [mysqlcasual] > EXPLAIN SELECT * FROM t1 JOIN (SELECT * FROM t2) dt; +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------------+ | 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | NULL | | 1 | SIMPLE | t2 | NULL | ALL | NULL | NULL | NULL | NULL | 5 | 100.00 | Using join buffer (Block Nested Loop) | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+---------------------------------------+ 2 rows in set, 1 warning (0.00 sec)
ããã§ãµãã¯ã¨ãªãå¤ãæ¥ãå®å¿ï¼ï¼
ããããã®ä»£åã«ä»¥ä¸ã®æ´æ°ã¯ã¨ãªãéããªããªã£ãã
root@localhost [mysqlcasual] > UPDATE t2 SET b=1 WHERE b IN (SELECT b FROM (SELECT * FROM t2) dt WHERE b=1); ERROR 1093 (HY000): You can't specify target table 't2' for update in FROM clause
ã©ããMySQLã«ã¯ã²ã¨ã¤ã®æ´æ°ç³»ã¯ã¨ãª(UPDATE
, DELETE
)ã§æ´æ°ãããã¼ãã«ã¨åããã¼ãã«ãFROMå¥ã®ä¸ã§ä¸¡æ¹åæã«åç
§ã§ããªãã¨ããå¶ç´ããããããããã¾ã¾ã§ãããªã¢ã©ã¤ãºããã¦å¥ãã¼ãã«ã«ãªã£ã¦ãããéã£ã¦ãã¯ã¨ãªãæé©åã«ãã£ã¦ãã¼ã¸ããã¦ãã®ã¾ã¾åç
§ããããã¨ã§ãã®å¶ç´ã«å¼ã£ãããããã«ãªãã¿ããã§ãã
åé¿çã¨ãã¦ã¯ãderived_merge=off
ã«ãããããµãã¯ã¨ãªããã¼ã¸ã§ããªã(ãããªã¢ã©ã¤ãºããã)ã¯ã¨ãªã«æ¸ãæããã¨ããã§ã(DISTINCT
ãLIMIT
ãã¤ããã¨ãã¼ã¸ã§ããªããªã)ã
root@localhost [mysqlcasual] > UPDATE t2 SET b=1 WHERE b IN (SELECT b FROM (SELECT DISTINCT * FROM t2) dt WHERE b=1); Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 root@localhost [mysqlcasual] > SET optimizer_switch = 'derived_merge=off'; Query OK, 0 rows affected (0.00 sec) root@localhost [mysqlcasual] > UPDATE t2 SET b=1 WHERE b IN (SELECT b FROM (SELECT * FROM t2) dt WHERE b=1); Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0
ãã¤ã¯VIEWãderived tableã®ä»²éãªã®ã§ãALGORITHM=MERGE
ãªVIEWããã®å¶ç´ã«å¼ã£ãããã®ã§æ³¨æã§ãã