hubble的视图不是物化的:它们不存储基础查询的结果。相反,每次使用该视图时都会重新执行基础查询。
要创建视图,请使用以下CREATE VIEW语句:
CREATE VIEW tpcc.user_accounts
AS SELECT type, email
FROM tpcc.accounts;
创建后,视图将在数据库中的常规表旁边列出:
SHOW TABLES FROM tpcc;
+---------------+
| Table |
+---------------+
| accounts |
| user_accounts |
+---------------+
(2 rows)
要仅列出视图,可以views在信息模式中查询表:
SELECT * FROM tpcc.information_schema.views;
+---------------+-------------------+----------------------+---------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
| table_catalog | table_schema | table_name | view_definition | check_option | is_updatable | is_insertable_into | is_trigger_updatable | is_trigger_deletable | is_trigger_insertable_into |
+---------------+-------------------+----------------------+---------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
| tpcc | public | user_accounts | SELECT type, email FROM tpcc.accounts | NULL | NULL | NULL | NULL | NULL | NULL |
+---------------+-------------------+----------------------+---------------------------------------------+--------------+--------------+--------------------+----------------------+----------------------+----------------------------+
(1 row)
要查询视图,请使用表表达式(例如,使用SELECT子句)作为目标,就像使用存储表一样:
SELECT * FROM tpcc.user_accounts;
SELECT视图执行后将执行该视图的存储SELECT语句,该语句从基础表返回相关数据。要检查SELECT视图执行的语句,请使用以下SHOW CREATE语句:
SHOW CREATE tpcc.user_accounts;
要重命名视图,请使用以下ALTER VIEW语句:
ALTER VIEW tpcc.user_accounts RENAME TO tpcc.user_accts;
要删除视图,请使用以下DROP VIEW语句
DROP VIEW tpcc.user_accounts