Query OK, 0 rows affected
mysql> desc extended select * from bigAgeStuView;
+----+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 6 | 100 | |
| 2 | DERIVED | stu | ALL | NULL | NULL | NULL | NULL | 1138 | 100 | Using where |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+-------------+
2 rows in set
temptable 执行计划显示,使用了派生表(临时表)
mysql> create algorithm=merge view bigAgeStuView as select * from stu where age>25;
Query OK, 0 rows affected
mysql> desc extended select * from bigAgeStuView;
+----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | stu | ALL | NULL | NULL | NULL | NULL | 1138 | 100 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set
merge 执行计划显示,没有使用派生表
mysql> create algorithm=undefined view bigAgeStuView as select * from stu where age>25;
Query OK, 0 rows affected
mysql> desc extended select * from bigAgeStuView;
+----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | stu | ALL | NULL | NULL | NULL | NULL | 1138 | 100 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set
undefined(由Mysql决定使用合适的算法)执行计划显示,没有使用派生表
5、需要注意的是,临时表算法存在性能问题,因此我们倾向于使用合并算法。但是,并不是每个视图都能使用合并算法,比如建立视图使用了group by, distinct,聚合函数,组合查询union,子查询,不能使用合并算法,因为视图记录无法与原表记录建立一一映射的关系。如下:
Query OK, 0 rows affected
mysql> show warnings;
+---------+------+-------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------------------+
| Warning | 1354 | View merge algorithm can't be used here for now (assumed undefined algorithm) |
+---------+------+-------------------------------------------------------------------------------+
1 row in set
mysql> desc extended select * from bigAgeStuView;
+----+-------------+------------+--------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+--------+---------------+------+---------+------+------+----------+-------+
| 1 | PRIMARY | <derived2> | system | NULL | NULL | NULL | NULL | 1 | 100 | |
| 2 | DERIVED | stu | ALL | NULL | NULL | NULL | NULL | 1138 | 100 | |
+----+-------------+------------+--------+---------------+------+---------+------+------+----------+-------+
6、使用临时表算法,没有办法更新原表的记录,这个很好理解,因为临时表算法,操作的是临时表。