随笔 - 576  文章 - 0  评论 - 62  阅读 - 219万

mysql 视图

1、视图是一个虚拟表,可以认为对原表封装了一下,一般情况下,可以把视图当做表来对待。
2、视图的实现由两种策略:临时表算法与合并算法。临时表算法:把视图对原表的查询结果放在一个临时表中,以后对视图的操作就是对临时表的操作。合并算法:把对试图的操作转化为对原表的操作。
3、举例来说,mysql> create view bigAgeStuView as select * from stu where age>25;
对于查询 mysql> select * from bigAgeStuView where age<28;
如果采用临时表算法,Mysql会把select * from stu where age>25的结果放入一张临时表,再操作临时表,查找age>28
如果采用合并算法,Mysql会把对视图的查询转化为select * from bigAgeStuView where 25<age and age<28;
显然,临时表算法会存在严重的性能问题。
4、对于create view bigAgeStuView as select * from stu where age>25; 可以指定视图的算法,格式如下: 
复制代码
mysql> create algorithm=temptable 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 | 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,子查询,不能使用合并算法,因为视图记录无法与原表记录建立一一映射的关系。如下:

复制代码
mysql> create algorithm=merge view bigAgeStuView as select max(age) from stu;
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、使用临时表算法,没有办法更新原表的记录,这个很好理解,因为临时表算法,操作的是临时表。 

 

posted on   Andy Niu  阅读(316)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示