跳到主要内容
翻译页面

本页面由 AI 辅助从英文翻译而来。如两者存在差异,以英文版本为准。

CREATE VIEW

在会话中创建 view 或 materialized view,可用于表示较长或复杂的 stackql 查询。

另请参阅:
[ SELECT ] [ REFRESH ] [ DROP ]


语法​

createViewStatement::=

CREATEORREPLACEMATERIALIZEDVIEWviewNameASselectStatementOUTERJOINselectStatementUNIONselectStatement';'

 
 

CREATE [ OR REPLACE ] [ MATERIALIZED ] VIEW <viewName> AS
<selectStatement> [ UNION | JOIN <selectStatement> ];

示例​

aws 与 google 之间的跨云 UNION​

CREATE VIEW dual_cloud_block_storage AS
SELECT
'google' AS vendor,
name,
split_part(split_part(type, '/', 11), '-', 2) AS type,
status,
sizeGb AS size
FROM google.compute.disks
WHERE project = '<YOUR_GCP_PROJECT>'
AND zone = 'australia-southeast1-a'
UNION
SELECT
'aws' AS vendor,
volumeId AS name,
volumeType AS type,
status,
size
FROM aws.ec2.volumes
WHERE region = 'ap-southeast-2';

创建并使用物化视图​

/* -------------------------- */
/* create a materialized view */
/* -------------------------- */
CREATE MATERIALIZED VIEW vw_ec2_instance_types AS
SELECT
memoryInfo,
hypervisor,
autoRecoverySupported,
instanceType,
SPLIT_PART(processorInfo, '\n', 3) as processorArch,
currentGeneration,
freeTierEligible,
hibernationSupported,
SPLIT_PART(vCpuInfo, '\n', 2) as vCPUs,
bareMetal,
burstablePerformanceSupported,
dedicatedHostsSupported
FROM aws.ec2.instance_types
WHERE region = 'us-east-1';
/* ------------------------- */
/* use the materialized view */
/* ------------------------- */
SELECT
i.instanceId,
i.instanceType,
it.vCPUs,
it.memoryInfo
FROM aws.ec2.instances i
INNER JOIN vw_ec2_instance_types it
ON i.instanceType = it.instanceType
WHERE i.region = 'us-east-1';