マテリアライズドテーブルの作成¶
CREATE MATERIALIZED TABLE
ステートメントの構文です。
CREATE [ OR REPLACE ] MATERIALIZED TABLE <name:identifier>
(
<VDP type field> [, <VDP type field> ]*
| <SQL type field> [, <SQL type field> ]*
)
[ FOLDER = <literal> ]
[ <primary key> ]
CREATE [ OR REPLACE ] MATERIALIZED TABLE <name:identifier>
AS <SELECT statement>
<VDP type field> ::=
<name:identifier> : <Virtual DataPort data type>
[ ( <property> [, <property> ]+ ) ]
<SQL type field> ::=
<name:identifier> <SQL type> [ ( <property> [, <property> ]+ ) ]
<SQL type> ::=
BIT ( <integer> )
| BIT VARYING ( <integer> )
| BLOB
| BOOLEAN
| CHAR ( <integer> )
| CHARACTER ( <integer> )
| CHARACTER VARYING ( <integer> )
| DATE
| DECIMAL
| DECIMAL ( <integer>, <integer> )
| DOUBLE
| DOUBLE PRECISION
| FLOAT
| INT
| INTEGER
| LONG
| NCHAR ( <integer> )
| NUMERIC
| NUMERIC ( <integer> , <integer> )
| NVARCHAR ( <integer> )
| REAL
| SMALLINT
| TEXT
| TIMESTAMP
| TIMESTAMP WITH TIME ZONE
| VARCHAR ( <integer> )
<property> ::=
<name:identifier> [ = <value:identifier> ]
<primary key> ::=
[ CONSTRAINT <name:literal> ]
PRIMARY KEY ( <field name:literal> [, <field name:literal> ]* )
<Virtual DataPort data type> ::= (「 Virtual DataPort のデータ型 」を参照)
マテリアライズドビューの作成方法は 3 つあります。
構文 1: CREATE MATERIALIZED TABLE
CREATE OR REPLACE MATERIALIZED TABLE usa_state (
name : text
, abbreviation : text
, capital : text
)
CONSTRAINT 'primary_key_usa_state' PRIMARY KEY ('name');
;
This statement creates a materialized table without data in it. The next section explains how to insert data in it.
このテーブルのプライマリキーフィールドは「name」です。Virtual DataPort では、ビューのプライマリキーは強制されません。このフィールドには重複する値を挿入しないようにする必要があります。
構文 2: CREATE MATERIALIZED TABLE ... AS SELECT ...
CREATE MATERIALIZED TABLE order_current_year
AS SELECT *
FROM order
WHERE TRUNC(date, 'YEAR') = TRUNC(NOW(), 'YEAR');
このステートメントは、SELECT ステートメントのスキーマでマテリアライズドビューを作成し、クエリの結果をそのビューに挿入します。
構文 3: SELECT ... INTO ...
SELECT <expression> [, <expression>]*
INTO <table name:identifier>
FROM <FROM clause>
[ <WHERE clause> ]
[ <GROUP BY clause> ]
[ <HAVING clause> ]
<table name> ビューがすでに存在する場合、このステートメントは失敗します。
以下に例を示します。
SELECT order_date, total_amount
INTO order_current_year
FROM order
WHERE TRUNC(date, 'YEAR') = TRUNC(NOW(), 'YEAR');
このステートメントは、「order_date」フィールドおよび「total_amount」フィールドを持つマテリアライズドテーブル「order_current_year」を作成し、クエリの結果をそのテーブルに挿入します。
マテリアライズドテーブルを作成する場合、Virtual DataPort サーバーは操作が終了するまでデータベースをシングルユーザーモードに切り替えます。最初の構文 (CREATE MATERIALIZED TABLE ...
) では、操作は即座に完了します。
With the second syntax (CREATE MATERIALIZED TABLE ... AS SELECT ...
)
and the third one (SELECT ... INTO ...
), the database stays in single
user mode until the query finishes and its result is stored. If the
query is long, it is better to execute CREATE MATERIALIZED TABLE
,
which finishes instantly; and then, insert the data into the
materialized table with the command
INSERT INTO <materialized table> ( <SELECT clause> )
(see the
next section). As the INSERT does not switch the database to single user mode, inserting the data with the INSERT allows for a higher level of concurrency.