设为首页 收藏本站
查看: 658|回复: 0

[经验分享] MySQL: View

[复制链接]

尚未签到

发表于 2016-9-5 12:29:02 | 显示全部楼层 |阅读模式
  Why use View:
  1) Sometimes the result set of a query is frequently used. That is this result set is usually used as a sub-query for another query. So why don't we transform this result set into a table that can be directly stored and  used?
  

#pseudo-sql
#create table g2 like goods;
#insert into g2 select * from goods;

  
  
  Definition of View:
  1) A view is a vitual table that derieves from the result set of a query.
  

#Normal SQL
select * from stu where stu_score >= 60;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+
#View
create view standard as select * from stu where stu_score > 60;
select * from standard;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+
  
  
  Syntax of View:
  1) create [algorithm=merge/temptable/undefined] view view_name as select_statement
  2) drop view view_name
  3) alter view view_name as select_statement
  
  Benefits of using View:
  1) Simplify SQL: Don't have to use many sub-sqls.
  2) Entitlement Control: Make entitlement control to a more fine grained level as into every column.
  3) Benefits Big Data Tabling.
  Eg>Simplify SQL: Get the highest three student average score group by student name

#Get average score for each students
create view stu_avg as select stu_name, avg(stu_score) as avg_score from stu group by stu_name;
select * from stu_avg;
+----------+-----------+
| stu_name | avg_score |
+----------+-----------+
| lisi     | 50.0000   |
| wangwu   | 30.0000   |
| zhangsan | 60.0000   |
| zhaoliu  | 74.5000   |
+----------+-----------+
#Get highest three average score for students
select * from stu_avg order by avg_score desc limit 3;
+----------+-----------+
| stu_name | avg_score |
+----------+-----------+
| zhaoliu  | 74.5000   |
| zhangsan | 60.0000   |
| lisi     | 50.0000   |
+----------+-----------+
#Bingo!
  Eg>Entitlement Control:
  Grant normal user access to view but not to real table.
  Eg>Big Data Tabling:
  Once the row count of a table is larger than 2,000,000, it would be very slow when SQLing.
  Split the table into serval sub-tables.
  Table news (contains 2,000,000 rows)
  ->news1 + news2 + news3 + news4
  ->(news_id%4 + 1)== 1 into news1 table
  ->(news_id%4 + 1) == 2 into news2 table
  ->...
  use view to combine these three news table.
  create view news_view as select * from news1 union select * from news2 union select * from news3 union select * from news4;
  
  Relationship between View and Table:
  1) View is derieved from table. So once table changed, view will be affected. View is the result set of table.
  2) What if view changed?

update stu_avg set avg_score = 68 where avg_score = 50;
ERROR 1288 : The target table stu_avg of the UPDATE is not updatable
  1) View is not always can CRUD. -> If a column of a view is simply derieved from table, then we can CRUD this column. And table will be affected.
  -> If a column of a view is not simply derieved from table, then we cannot CRUD this column.
  
  Algorithm of View:
  1) Algorithm = merge / temptable / undefined
  2) Merge>When using view, using statement combine with statement that defined this view.
  3) Temptable>When using view, create a temporary table besed on statement that defined this view.
  4) Undefined> Let system to choose algorithm.
  Comments: 
  1) Merge means view is just a rule. When using view, compiler will analyze the view statement and merge it into outer SQL.
  <Simply merge two SQL together. Only ONE SQL executed>
  2) Temptable means view is a temporary table. Outer SQL will based on this temp table.
  <Using real temporary table. TWO SQLs executed>
  3) Different algorithms affect different tables: Merge affects orginal table. Temptable affect temp table.
  Eg> 

#Create view
create view standard as select * from stu where stu_score > 50;
select * from standard;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| lisi     | Literature |        55 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+
#Using view
select * from standard where stu_score < 99;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Math       |        90 |
| lisi     | Literature |        55 |
+----------+------------+-----------+
#Analysis->The real process of execution
select * from stu where stu_score > 50 and stu_score < 99;
##########################################
#Get the profile of the students whose score is highest group by stu_course
#Normal approach
select * from stu order by stu_course asc, stu_score desc;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| zhangsan | Geograph   |        40 |
| lisi     | Literature |        55 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
| lisi     | Politic    |        45 |
| wangwu   | Politic    |        30 |
+----------+------------+-----------+
select * from (select * from stu order by stu_course asc, stu_score desc) as temp group by stu_course;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| lisi     | Literature |        55 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+
#Using view
mysql> create view order_stu as select * from stu order by stu_course asc, stu_score desc;
Query OK, 0 rows affected
mysql> select * from order_stu;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| zhangsan | Geograph   |        40 |
| lisi     | Literature |        55 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
| lisi     | Politic    |        45 |
| wangwu   | Politic    |        30 |
+----------+------------+-----------+
8 rows in set
mysql> select * from order_stu group by stu_course;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Geograph   |        40 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| lisi     | Politic    |        45 |
+----------+------------+-----------+
4 rows in set
#The result is wrong!!!! Why??
#Analysis: Real process of execution>>>>>>>>>>>
select * from stu group by stu_course order by stu_course asc, stu_score desc;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhangsan | Geograph   |        40 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| lisi     | Politic    |        45 |
+----------+------------+-----------+
4 rows in set
#It is the result of merge algorithm. ->Merge outer statement into view definition statement. Sometimes may incur problems!
#So we have to change the algorithm into TEMPTABLE!
#Remedy>
mysql> create algorithm = temptable view order_stu as select * from stu order by stu_course asc, stu_score desc;
Query OK, 0 rows affected
mysql> select * from order_stu;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| zhangsan | Geograph   |        40 |
| lisi     | Literature |        55 |
| zhangsan | Literature |        50 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
| lisi     | Politic    |        45 |
| wangwu   | Politic    |        30 |
+----------+------------+-----------+
8 rows in set
mysql> select * from order_stu group by stu_course;
+----------+------------+-----------+
| stu_name | stu_course | stu_score |
+----------+------------+-----------+
| zhaoliu  | Geograph   |        50 |
| lisi     | Literature |        55 |
| zhangsan | Math       |        90 |
| zhaoliu  | Politic    |        99 |
+----------+------------+-----------+
4 rows in set
  
  Comments:
  1) The moment a view is created, it is regarded as a table. show tables command will show views as well.

show tables;
+-----------------+
| Tables_in_mugua |
+-----------------+
| goods           |
| standard        |
| stu             |
+-----------------+
  2) Knowing the meaning of algorithm for view. And knowing difference between different algorithms.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-267998-1-1.html 上篇帖子: mysql启动不了 下篇帖子: MySQL5.6.11基于GTID的复制问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表