Mycat2作为分库分表为主要功能的中间件,支持物理视图与逻辑视图.
物理视图就是存储节点mysql上的真实视图,而逻辑视图是mycat2中的视图功能,它可以把mycat2中配置的查询语句作为一个视图,这个查询中可以包含mycat2中任意的表,包含分片表.
支持语法
create view db1.testView as select id from db1.normal_table;
create view db1.testView (id2) as select id from db1.normal_table
该语法是alibaba druid支持的语法,使用mysql语法有些视图语法可能会报错
创建物理视图
create database db1;
create table db1.normal_table(id int);//单表
create view db1.testView as select id from db1.normal_table;
//mycat会对视图建立语句进行分析,判断是否是物理视图,如果是物理视图
//则把这个sql转换为单表配置并直接在原型库上建立视图,那么这个视图的配置是对应mycat中的单表
select * from db1.testView;//可以查询了
drop view db1.testview;//删除视图,实际上是删除单表
db1.schema.json
{
"customTables":{},
"globalTables":{},
"normalProcedures":{},
"normalTables":{
"normal_table":{
"createTableSQL":"CREATE TABLE db1.normal_table (\n\tid int\n)",
"locality":{
"schemaName":"db1",
"tableName":"normal_table",
"targetName":"prototype"
}
},
"testView":{
"createTableSQL":"CREATE TABLE db1.testView (\n\tid INT\n)",
"locality":{
"schemaName":"db1",
"tableName":"testView",
"targetName":"prototype"
}
}
},
"schemaName":"db1",
"shardingTables":{},
"views":{}
}
可以看出,物理视图的配置实际上就是单表的配置
创建逻辑视图
create database db1;
create view db1.testView as select * from `information_schema`.`COLUMNS`;
//涉及虚拟表或者分片表,那么这个视图则为逻辑视图
select * from db1.testView;//可以查询了
drop view db1.testview;//删除视图,实际上是删除配置中的视图
db1.schema.json
{
"customTables":{},
"globalTables":{},
"normalProcedures":{},
"normalTables":{},
"schemaName":"db1",
"shardingTables":{},
"views":{
"testView":{
"createViewSQL":"CREATE VIEW db1.testView\nAS\nSELECT *\nFROM `information_schema`.`COLUMNS`"
}
}
}