Mycat2作为分库分表为主要功能的中间件,支持物理视图与逻辑视图.
    物理视图就是存储节点mysql上的真实视图,而逻辑视图是mycat2中的视图功能,它可以把mycat2中配置的查询语句作为一个视图,这个查询中可以包含mycat2中任意的表,包含分片表.

    支持语法

    1. create view db1.testView as select id from db1.normal_table;
    2. create view db1.testView (id2) as select id from db1.normal_table

    该语法是alibaba druid支持的语法,使用mysql语法有些视图语法可能会报错

    创建物理视图

    1. create database db1;
    2. create table db1.normal_table(id int);//单表
    3. create view db1.testView as select id from db1.normal_table;
    4. //mycat会对视图建立语句进行分析,判断是否是物理视图,如果是物理视图
    5. //则把这个sql转换为单表配置并直接在原型库上建立视图,那么这个视图的配置是对应mycat中的单表
    6. select * from db1.testView;//可以查询了
    7. drop view db1.testview;//删除视图,实际上是删除单表

    db1.schema.json

    1. {
    2. "customTables":{},
    3. "globalTables":{},
    4. "normalProcedures":{},
    5. "normalTables":{
    6. "normal_table":{
    7. "createTableSQL":"CREATE TABLE db1.normal_table (\n\tid int\n)",
    8. "locality":{
    9. "schemaName":"db1",
    10. "tableName":"normal_table",
    11. "targetName":"prototype"
    12. }
    13. },
    14. "testView":{
    15. "createTableSQL":"CREATE TABLE db1.testView (\n\tid INT\n)",
    16. "locality":{
    17. "schemaName":"db1",
    18. "tableName":"testView",
    19. "targetName":"prototype"
    20. }
    21. }
    22. },
    23. "schemaName":"db1",
    24. "shardingTables":{},
    25. "views":{}
    26. }

    可以看出,物理视图的配置实际上就是单表的配置

    创建逻辑视图

    1. create database db1;
    2. create view db1.testView as select * from `information_schema`.`COLUMNS`;
    3. //涉及虚拟表或者分片表,那么这个视图则为逻辑视图
    4. select * from db1.testView;//可以查询了
    5. drop view db1.testview;//删除视图,实际上是删除配置中的视图

    db1.schema.json

    1. {
    2. "customTables":{},
    3. "globalTables":{},
    4. "normalProcedures":{},
    5. "normalTables":{},
    6. "schemaName":"db1",
    7. "shardingTables":{},
    8. "views":{
    9. "testView":{
    10. "createViewSQL":"CREATE VIEW db1.testView\nAS\nSELECT *\nFROM `information_schema`.`COLUMNS`"
    11. }
    12. }
    13. }