# Creating a Materialized View

**Basic Syntax:**

```
CREATE MATERIALIZED VIEW [IF NOT EXISTS] [db.]Materialized_name [TO[db.]name] [ON CLUSTER cluster] 
ENGINE = engine_name()
ORDER BY expr 
[POPULATE] 
AS SELECT ...
```

**Parameter Description:**

<table>
    <thead>
        <tr>
            <th>Parameter</th>
            <th>Description</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <td><code>db</code></td>
        <td>The name of the database, default is the currently selected database. In this context, <i>ck_cluster</i> is used as an example.</td>
    </tr>
    <tr>
        <td><code>Materialized_name</code></td>
        <td>The name of the materialized view.</td>
    </tr>
    <tr>
        <td><code>TO[db.]name</code></td>
        <td>Write the data of the materialized view into a new table.
           If you want to write the data of the materialized view into a new table, the <code>POPULATE</code> keyword cannot be used.
        </td>
    </tr>
    <tr>
        <td><code>[ON CLUSTER cluster]</code></td>
        <td>Create a materialized view on every node, fixed as
            <code>ON CLUSTER ck_cluster</code>.
        </td>
    </tr>
    <tr>
        <td><code>ENGINE = engine_name()</code></td>
        <td>
            Table engine type. For more details, please refer to <span><a href="/uclickhouse/developer/table_engine">Table Engine</a></span>.
        </td>
    </tr>
    <tr>
        <td><code>[POPULATE]</code></td>
        <td>The <code>POPULATE</code> keyword. If the <code>POPULATE</code> keyword is specified when creating the materialized view, the source table data specified by the <code>SELECT</code> clause will be inserted into the materialized view upon creation. If the <code>POPULATE</code> keyword is not specified, the materialized view will only include the new data written to the source table after the view is created.
           Generally, it is not recommended to use the <code>POPULATE</code> keyword because the data written to the source table during the creation of the materialized view will not be included in the view.
        </td>
    </tr>
    <tr>
        <td><code>SELECT ...</code></td>
        <td>
            The <code>SELECT</code> clause. When data is written into the source table specified by the <code>SELECT</code> clause, the inserted data is transformed by the query in the <code>SELECT</code> clause, and the final result is inserted into the materialized view.
            The <code>SELECT</code> query can include
                <code>DISTINCT</code>,
                <code>GROUP BY</code>,
                <code>ORDER BY</code>, and
                <code>LIMIT</code>, etc., but the respective transformations are executed independently on each inserted data block.
        </td>
    </tr>
    </tbody>
</table>

Example:

1. Create the source table specified by the SELECT clause.

   ```sql
   CREATE TABLE ck_test.lineorder_local on cluster ck_cluster
   (
       LO_ORDERKEY             UInt32,
       LO_LINENUMBER           UInt8,
       LO_CUSTKEY              UInt32,
       LO_PARTKEY              UInt32,
       LO_SUPPKEY              UInt32,
       LO_ORDERDATE            Date,
       LO_ORDERPRIORITY        LowCardinality(String),
       LO_SHIPPRIORITY         UInt8,
       LO_QUANTITY             UInt8,
       LO_EXTENDEDPRICE        UInt32,
       LO_ORDTOTALPRICE        UInt32,
       LO_DISCOUNT             UInt8,
       LO_REVENUE              UInt32,
       LO_SUPPLYCOST           UInt32,
       LO_TAX                  UInt8,
       LO_COMMITDATE           Date,
       LO_SHIPMODE             LowCardinality(String)
   )
   ENGINE = ReplicatedMergeTree(
     '/clickhouse/ck_test/tables/{layer}-{shard}/lineorder',
     '{replica}'
   ) PARTITION BY toYear(LO_ORDERDATE) ORDER BY (LO_ORDERDATE, LO_ORDERKEY)
   ```

2. Insert data into the source table.

   Refer to [Quick Start](/docs/uclickhouse/gettingstart)

3. Create a materialized view based on the source table.

   ```sql
   CREATE MATERIALIZED VIEW lineorder_view ON CLUSTER ck_cluster
   ENGINE = MergeTree()
   ORDER BY (id) AS SELECT * FROM lineorder_local;
   ```

4. Query the materialized view to verify whether data written to the source table before the view was created can be queried, when the `POPULATE` keyword is not specified.

   ```sql
   SELECT * FROM lineorder_view;
   ```

   The query returns empty, indicating that without specifying the `POPULATE` keyword, data written to the source table prior to the creation of the materialized view is not accessible.

5. Insert data into the source table.

   Refer to [Quick Start](/docs/uclickhouse/gettingstart)

6. Query the materialized view.

   ```sql
   SELECT * FROM lineorder_view;
   ```

## Official Documentation

For more information on creating a materialized view, please see [Create Materialized View](https://clickhouse.com/docs/zh/sql-reference/statements/create/view/#materialized).