前回の記事で、unionの条件判定が10.2から変更になっていると記載しましたが、
どうやら、10.2から追加されたoptimizer_switchの動作によるものらしい。
具体的には、
condition_pushdown_for_derived=on
というパラメータに依存するようです。
実際に、上記パラメータを切り替えてテストしてみた。
・offのとき
MariaDB [test]> set optimizer_switch='condition_pushdown_for_derived=off';
Query OK, 0 rows affected (0.000 sec)
MariaDB [test]> explain select * from (select * from users union all select * from users) as t where id = 7542;
+------+-------------+------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+------------+------+---------------+------+---------+------+---------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 1993522 | Using where |
| 2 | DERIVED | users | ALL | NULL | NULL | NULL | NULL | 996761 | |
| 3 | UNION | users | ALL | NULL | NULL | NULL | NULL | 996761 | |
+------+-------------+------------+------+---------------+------+---------+------+---------+-------------+
3 rows in set (0.001 sec)
・onのとき
MariaDB [test]> set optimizer_switch='condition_pushdown_for_derived=on';
Query OK, 0 rows affected (0.000 sec)
MariaDB [test]> explain select * from (select * from users union all select * from users) as t where id = 7542;
+------+-------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+------------+-------+---------------+---------+---------+-------+------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
| 2 | DERIVED | users | const | PRIMARY | PRIMARY | 4 | const | 1 | |
| 3 | UNION | users | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+------+-------------+------------+-------+---------------+---------+---------+-------+------+-------------+
3 rows in set (0.000 sec)
というように、
condition_pushdown_for_derived
のパラメータによって上記の差異がでる。
で、さらに気になったのが、
10.2から、
mrr=off
mrr_cost_based=off
に変更されている。
mrrはセカンダリーインデックスのIO負荷軽減のものであったかと思うが、
このあたりの絡みでパフォーマンスにどう影響があるのか、再度調べてみようと思う。
なお、MySQL8ではonのままであった。
以上