Yang, Tong 6182f91ba8 MRS component operation guide_normal 2.0.38.SP20 version
Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com>
Co-authored-by: Yang, Tong <yangtong2@huawei.com>
Co-committed-by: Yang, Tong <yangtong2@huawei.com>
2022-12-09 14:55:21 +00:00

238 lines
23 KiB
HTML

<a name="mrs_01_24105"></a><a name="mrs_01_24105"></a>
<h1 class="topictitle1">ClickHouse Table Engine Overview</h1>
<div id="body0000001106279954"><div class="section" id="mrs_01_24105__section2024214115335"><h4 class="sectiontitle">Background</h4><p id="mrs_01_24105__p1073812512346">Table engines play a key role in ClickHouse to determine:</p>
<ul id="mrs_01_24105__ul182181558411"><li id="mrs_01_24105__li192189510418">Where to write and read data</li><li id="mrs_01_24105__li110714794113">Supported query modes</li><li id="mrs_01_24105__li1689498164119">Whether concurrent data access is supported</li><li id="mrs_01_24105__li7899410194114">Whether indexes can be used</li><li id="mrs_01_24105__li12648412174120">Whether multi-thread requests can be executed</li><li id="mrs_01_24105__li277344164710">Parameters used for data replication</li></ul>
<p id="mrs_01_24105__p1318438195119">This section describes MergeTree and Distributed engines, which are the most important and frequently used ClickHouse table engines.</p>
</div>
<div class="section" id="mrs_01_24105__section3291324175410"><h4 class="sectiontitle">MergeTree Family</h4><p id="mrs_01_24105__p1649552920548">Engines of the MergeTree family are the most universal and functional table engines for high-load tasks. They have the following key features:</p>
<ul id="mrs_01_24105__ul579062791417"><li id="mrs_01_24105__li47901127161411">Data is stored by partition and block based on partitioning keys.</li><li id="mrs_01_24105__li0433829131412">Data index is sorted based on primary keys and the <strong id="mrs_01_24105__b1078212221171">ORDER BY</strong> sorting keys.</li><li id="mrs_01_24105__li14910203011415">Data replication is supported by table engines prefixed with Replicated.</li><li id="mrs_01_24105__li110533318147">Data sampling is supported.</li></ul>
<p id="mrs_01_24105__p133441194144">When data is written, a table with this type of engine divides data into different folders based on the partitioning key. Each column of data in the folder is an independent file. A file that records serialized index sorting is created. This structure reduces the volume of data to be retrieved during data reading, greatly improving query efficiency.</p>
<ul id="mrs_01_24105__ul750073312167"><li id="mrs_01_24105__li4501633171620">MergeTree<div class="p" id="mrs_01_24105__p1812885162115"><a name="mrs_01_24105__li4501633171620"></a><a name="li4501633171620"></a><strong id="mrs_01_24105__b3455344123610">Syntax for creating a table</strong>:<pre class="screen" id="mrs_01_24105__screen13755115922114">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1] [TTL expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2] [TTL expr2],
...
INDEX index_name1 expr1 TYPE type1(...) GRANULARITY value1,
INDEX index_name2 expr2 TYPE type2(...) GRANULARITY value2
) <strong id="mrs_01_24105__b1745917632214">ENGINE = MergeTree</strong>()
ORDER BY expr
[PARTITION BY expr]
[PRIMARY KEY expr]
[SAMPLE BY expr]
[TTL expr [DELETE|TO DISK 'xxx'|TO VOLUME 'xxx'], ...]
[SETTINGS name=value, ...]</pre>
</div>
<p id="mrs_01_24105__p18988104416214"><strong id="mrs_01_24105__b12758265222">Example</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen898817443211">CREATE TABLE default.test (
name1 DateTime,
name2 String,
name3 String,
name4 String,
name5 Date,
...
) <strong id="mrs_01_24105__b14988194432117">ENGINE = MergeTree</strong>()
<strong id="mrs_01_24105__b1498817449219">PARTITION BY</strong> toYYYYMM(name5)
<strong id="mrs_01_24105__b3988144172115">ORDER BY</strong> (name1, name2)
<strong id="mrs_01_24105__b1598814410219">SETTINGS </strong><strong id="mrs_01_24105__b15988044152120">index_granularity</strong> = 8192</pre>
<div class="p" id="mrs_01_24105__p1198820444216">Parameters in the example are described as follows:<ul id="mrs_01_24105__ul1198819440219"><li id="mrs_01_24105__li16988154416213"><strong id="mrs_01_24105__b3988174411211">ENGINE = MergeTree()</strong>: specifies the MergeTree engine.</li><li id="mrs_01_24105__li18988144415211"><strong id="mrs_01_24105__b2098844482119">PARTITION BY</strong> <strong id="mrs_01_24105__b1332724153418">toYYYYMM(name4)</strong>: specifies the partition. The sample data is partitioned by month, and a folder is created for each month.</li><li id="mrs_01_24105__li4988164412214"><strong id="mrs_01_24105__b1998814418219">ORDER BY</strong>: specifies the sorting field. A multi-field index can be sorted. If the first field is the same, the second field is used for sorting, and so on.</li><li id="mrs_01_24105__li12988174462110"><strong id="mrs_01_24105__b2988134442117">index_granularity = 8192</strong>: specifies the index granularity. One index value is recorded for every 8,192 data records.</li></ul>
</div>
<p id="mrs_01_24105__p898814422111">If the data to be queried exists in a partition or sorting field, the data query time can be greatly reduced.</p>
</li><li id="mrs_01_24105__li35011733131618">ReplacingMergeTree<p id="mrs_01_24105__p12256051104411"><a name="mrs_01_24105__li35011733131618"></a><a name="li35011733131618"></a>Different from MergeTree, ReplacingMergeTree deletes duplicate entries with the same sorting key. ReplacingMergeTree is suitable for clearing duplicate data to save space, but it does not guarantee the absence of duplicate data. Generally, it is not recommended.</p>
<div class="p" id="mrs_01_24105__p0360122614521"><strong id="mrs_01_24105__b116131451114918">Syntax for creating a table</strong>:<pre class="screen" id="mrs_01_24105__screen18831436125212">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) <strong id="mrs_01_24105__b141858135314">ENGINE = ReplacingMergeTree</strong>([ver])
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...]</pre>
</div>
</li><li id="mrs_01_24105__li45011633121610">SummingMergeTree<p id="mrs_01_24105__p135222013142"><a name="mrs_01_24105__li45011633121610"></a><a name="li45011633121610"></a>When merging data parts in SummingMergeTree tables, ClickHouse merges all rows with the same primary key into one row that contains summed values for the columns with the numeric data type. If the primary key is composed in a way that a single key value corresponds to large number of rows, storage volume can be significantly reduced and the data query speed can be accelerated.</p>
<p id="mrs_01_24105__p1697716173168"><strong id="mrs_01_24105__b9941257124918">Syntax for creating a table</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen1023917276161">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) <strong id="mrs_01_24105__b1214513011710">ENGINE = SummingMergeTree</strong>([columns])
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...]</pre>
<p id="mrs_01_24105__p171103912174"><strong id="mrs_01_24105__b42991237191718">Example</strong>:</p>
<p id="mrs_01_24105__p17770152911173">Create a SummingMergeTree table named <strong id="mrs_01_24105__b10669171716359">testTable</strong>.</p>
<pre class="screen" id="mrs_01_24105__screen2325164454311">CREATE TABLE testTable
(
id UInt32,
value UInt32
)
<strong id="mrs_01_24105__b665425184417">ENGINE = SummingMergeTree()</strong>
ORDER BY id</pre>
<p id="mrs_01_24105__p16302131410448">Insert data into the table.</p>
<pre class="screen" id="mrs_01_24105__screen1650212618448">INSERT INTO testTable Values(5,9),(5,3),(4,6),(1,2),(2,5),(1,4),(3,8);
INSERT INTO testTable Values(88,5),(5,5),(3,7),(3,5),(1,6),(2,6),(4,7),(4,6),(43,5),(5,9),(3,6);</pre>
<p id="mrs_01_24105__p1749552410451">Query all data in unmerged parts.</p>
<pre class="screen" id="mrs_01_24105__screen184318279461">SELECT * FROM testTable
┌─id─┬─value─┐
│ 1 │ 6 │
│ 2 │ 5 │
│ 3 │ 8 │
│ 4 │ 6 │
│ 5 │ 12 │
└───┴──── ┘
┌─id─┬─value─┐
│ 1 │ 6 │
│ 2 │ 6 │
│ 3 │ 18 │
│ 4 │ 13 │
│ 5 │ 14 │
│ 43 │ 5 │
│ 88 │ 5 │
└───┴──── ┘</pre>
<p id="mrs_01_24105__p572352561">If ClickHouse has not summed up all rows and you need to aggregate data by ID, use the <strong id="mrs_01_24105__b5291201714445">sum</strong> function and <strong id="mrs_01_24105__b1413195014209">GROUP BY</strong> statement.</p>
<pre class="screen" id="mrs_01_24105__screen5558105210562">SELECT id, sum(value) FROM testTable GROUP BY id
┌─id─┬─sum(value)─┐
│ 4 │ 19 │
│ 3 │ 26 │
│ 88 │ 5 │
│ 2 │ 11 │
│ 5 │ 26 │
│ 1 │ 12 │
│ 43 │ 5 │
└───┴───────┘</pre>
<p id="mrs_01_24105__p157972482571">Merge rows manually.</p>
<pre class="screen" id="mrs_01_24105__screen2052716221582">OPTIMIZE TABLE testTable</pre>
<p id="mrs_01_24105__p196221938145820">Query data in the <strong id="mrs_01_24105__b1638942717451">testTable</strong> table again.</p>
<pre class="screen" id="mrs_01_24105__screen2094811145911">SELECT * FROM testTable
┌─id─┬─value─┐
│ 1 │ 12 │
│ 2 │ 11 │
│ 3 │ 26 │
│ 4 │ 19 │
│ 5 │ 26 │
│ 43 │ 5 │
│ 88 │ 5 │
└───┴──── ┘</pre>
<p id="mrs_01_24105__p328592817103">SummingMergeTree uses the <strong id="mrs_01_24105__b17602103564819">ORDER BY</strong> sorting keys as the condition keys to aggregate data. That is, if sorting keys are the same, data records are merged into one and the specified merged fields are aggregated.</p>
<p id="mrs_01_24105__p14211122263310">Data is pre-aggregated only when merging is executed in the background, and the merging execution time cannot be predicted. Therefore, it is possible that some data has been pre-aggregated and some data has not been aggregated. Therefore, the <strong id="mrs_01_24105__b721120573012">GROUP BY</strong> statement must be used during aggregation.</p>
</li><li id="mrs_01_24105__li7501163316160">AggregatingMergeTree<p id="mrs_01_24105__p145961112135415"><a name="mrs_01_24105__li7501163316160"></a><a name="li7501163316160"></a>AggregatingMergeTree is a pre-aggregation engine used to improve aggregation performance. When merging partitions, the AggregatingMergeTree engine aggregates data based on predefined conditions, calculates data based on predefined aggregate functions, and saves the data in binary format to tables.</p>
<p id="mrs_01_24105__p6500171014544"><strong id="mrs_01_24105__b1050961195020">Syntax for creating a table</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen20500131035414">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
)<strong id="mrs_01_24105__b135001810195410"> ENGINE = AggregatingMergeTree</strong>()
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[TTL expr]
[SETTINGS name=value, ...]</pre>
<p id="mrs_01_24105__p10724182011578"><strong id="mrs_01_24105__b115201832175814">Example</strong>:</p>
<p id="mrs_01_24105__p174101925205712">You do not need to set the AggregatingMergeTree parameter separately. When partitions are merged, data in each partition is aggregated based on the <strong id="mrs_01_24105__b1666385119340">ORDER BY</strong> sorting key. You can set the aggregate functions to be used and column fields to be calculated by defining the AggregateFunction type, as shown in the following example:</p>
<pre class="screen" id="mrs_01_24105__screen341324114213">create table test_table (
name1 String,
name2 String,
name3 <strong id="mrs_01_24105__b66917535210">AggregateFunction(uniq,String)</strong>,
name4 <strong id="mrs_01_24105__b8709185613210">AggregateFunction(sum,Int)</strong>,
name5 DateTime
) <strong id="mrs_01_24105__b16625012317">ENGINE = AggregatingMergeTree()</strong>
PARTITION BY toYYYYMM(name5)
ORDER BY (name1,name2)
PRIMARY KEY name1;</pre>
<p id="mrs_01_24105__p78411035792">When data of the AggregateFunction type is written or queried, the <strong id="mrs_01_24105__b6811114919161">*state</strong> and <strong id="mrs_01_24105__b1412575661616">*merge</strong> functions need to be called. The asterisk (*) indicates the aggregate functions used for defining the field type. For example, the <strong id="mrs_01_24105__b52981154205512">uniq</strong> and <strong id="mrs_01_24105__b229810546556">sum</strong> functions are specified for the <strong id="mrs_01_24105__b182979542558">name3</strong> and <strong id="mrs_01_24105__b112981054175516">name4</strong> fields defined in the <strong id="mrs_01_24105__b52981554115516">test_table</strong>, respectively. Therefore, you need to call the <strong id="mrs_01_24105__b529905455514">uniqState</strong> and <strong id="mrs_01_24105__b12299854115514">sumState</strong> functions and run the <strong id="mrs_01_24105__b135553119413">INSERT</strong> and <strong id="mrs_01_24105__b13501815049">SELECT</strong> statements when writing data into the table.</p>
<pre class="screen" id="mrs_01_24105__screen11585433818">insert into test_table select '8','test1',<strong id="mrs_01_24105__b156918331139">uniqState('name1'),sumState(toInt32(100))</strong>,'2021-04-30 17:18:00';
insert into test_table select '8','test1',<strong id="mrs_01_24105__b11548173661315">uniqState('name1'),sumState(toInt32(200))</strong>,'2021-04-30 17:18:00';</pre>
<p id="mrs_01_24105__p15665153521611">When querying data, you need to call the corresponding functions <strong id="mrs_01_24105__b10208173822316">uniqMerge</strong> and <strong id="mrs_01_24105__b187974282312">sumMerge</strong>.</p>
<pre class="screen" id="mrs_01_24105__screen19282174113162">select name1,name2,uniqMerge(name3),sumMerge(name4) from test_table group by name1,name2;
┌─name1─┬─name2─┬─uniqMerge(name3)─┬─sumMerge(name4)─┐
│ 8 │ test1 │ 1 │ 300 │
└──── ┴──── ┴──────────┴───────── ┘</pre>
<p id="mrs_01_24105__p11765165631815">AggregatingMergeTree is more commonly used with materialized views, which are query views of other data tables at the upper layer.</p>
</li><li id="mrs_01_24105__li105011233141610">CollapsingMergeTree<p id="mrs_01_24105__p12400620175612"><a name="mrs_01_24105__li105011233141610"></a><a name="li105011233141610"></a>CollapsingMergeTree defines a <strong id="mrs_01_24105__b2097046134411">Sign</strong> field to record status of data rows. If <strong id="mrs_01_24105__b19645111315466">Sign</strong> is <strong id="mrs_01_24105__b5844102464612">1</strong>, the data in this row is valid. If <strong id="mrs_01_24105__b11900119145019">Sign</strong> is <strong id="mrs_01_24105__b112720618486">-1</strong>, the data in this row needs to be deleted.</p>
<div class="p" id="mrs_01_24105__p134944533591"><strong id="mrs_01_24105__b1633818108016">Syntax for creating a table</strong>:<pre class="screen" id="mrs_01_24105__screen15756136333">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
)<strong id="mrs_01_24105__b4122544135"> ENGINE = CollapsingMergeTree(sign)</strong>
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...]</pre>
</div>
</li><li id="mrs_01_24105__li25011033121618">VersionedCollapsingMergeTree<p id="mrs_01_24105__p69761481571"><a name="mrs_01_24105__li25011033121618"></a><a name="li25011033121618"></a>The VersionedCollapsingMergeTree engine adds <strong id="mrs_01_24105__b760010810548">Version</strong> to the table creation statement to record the mapping between a <strong id="mrs_01_24105__b1143993815718">state</strong> row and a <strong id="mrs_01_24105__b1401654115720">cancel</strong> row in case that rows are out of order. The rows with the same primary key, same <strong id="mrs_01_24105__b48021334296">Version</strong>, and opposite <strong id="mrs_01_24105__b11121181317291">Sign</strong> will be deleted during compaction.</p>
<p id="mrs_01_24105__p825911561177"><strong id="mrs_01_24105__b124101013102416">Syntax for creating a table</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen1832333913817">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
name1 [type1] [DEFAULT|MATERIALIZED|ALIAS expr1],
name2 [type2] [DEFAULT|MATERIALIZED|ALIAS expr2],
...
) <strong id="mrs_01_24105__b017217451986">ENGINE = VersionedCollapsingMergeTree(sign, version)</strong>
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...]</pre>
</li><li id="mrs_01_24105__li17501143316166">GraphiteMergeTree<p id="mrs_01_24105__p1848481416184"><a name="mrs_01_24105__li17501143316166"></a><a name="li17501143316166"></a>The GraphiteMergeTree engine is used to store data in the time series database Graphite.</p>
<p id="mrs_01_24105__p1049563091813"><strong id="mrs_01_24105__b4428165113217">Syntax for creating a table</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen1944814391194">CREATE TABLE [IF NOT EXISTS] [db.]table_name [ON CLUSTER cluster]
(
Path String,
Time DateTime,
Value &lt;Numeric_type&gt;,
Version &lt;Numeric_type&gt;
...
) ENGINE = GraphiteMergeTree(config_section)
[PARTITION BY expr]
[ORDER BY expr]
[SAMPLE BY expr]
[SETTINGS name=value, ...]</pre>
</li></ul>
</div>
<div class="section" id="mrs_01_24105__section132251834154017"><h4 class="sectiontitle">Replicated*MergeTree Engines</h4><p id="mrs_01_24105__p19448184704017">All engines of the MergeTree family in ClickHouse prefixed with Replicated become MergeTree engines that support replicas.</p>
<p id="mrs_01_24105__p159732194411"><span><img id="mrs_01_24105__image1197371934117" src="en-us_image_0000001295770752.png"></span></p>
<p id="mrs_01_24105__p1989414264410">Replicated series engines use ZooKeeper to synchronize data. When a replicated table is created, all replicas of the same shard are synchronized based on the information registered with ZooKeeper.</p>
<div class="p" id="mrs_01_24105__p1717511341531"><strong id="mrs_01_24105__b5790748485">Template for creating a Replicated engine</strong>:<pre class="screen" id="mrs_01_24105__screen1539115174814">ENGINE = Replicated*MergeTree('<em id="mrs_01_24105__i17145211528">Storage path in ZooKeeper</em>','<em id="mrs_01_24105__i117761229155212">Replica name</em>', ...)</pre>
</div>
<p id="mrs_01_24105__p8602647134716">Two parameters need to be specified for a Replicated engine:</p>
<ul id="mrs_01_24105__ul18781618184413"><li id="mrs_01_24105__li11465131915617"><em id="mrs_01_24105__i111553945212">Storage path in ZooKeeper</em>: specifies the path for storing table data in ZooKeeper. The path format is <strong id="mrs_01_24105__b9445114512546"><em id="mrs_01_24105__i1514119314103">/clickhouse/tables/{shard}</em></strong><strong id="mrs_01_24105__b154452454545">/</strong><strong id="mrs_01_24105__b1445745195416"><em id="mrs_01_24105__i514133171015">Database name/Table name</em></strong>.</li><li id="mrs_01_24105__li1378191811446"><em id="mrs_01_24105__i151213373555">Replica name</em>: Generally, <strong id="mrs_01_24105__b17303228205516">{replica}</strong> is used.</li></ul>
<p id="mrs_01_24105__p20344204314548">For details about the example, see <a href="mrs_01_2398.html">Creating a ClickHouse Table</a>.</p>
</div>
<div class="section" id="mrs_01_24105__section65781422145918"><h4 class="sectiontitle">Distributed Engine</h4><div class="p" id="mrs_01_24105__p6274527714">The Distributed engine does not store any data. It serves as a transparent proxy for data shards and can automatically transmit data to each node in the cluster. Distributed tables need to work with other local data tables. Distributed tables distribute received read and write tasks to each local table where data is stored.<div class="fignone" id="mrs_01_24105__fig756320311253"><span class="figcap"><b>Figure 1 </b>Working principle of the Distributed engine</span><br><span><img id="mrs_01_24105__image456316316250" src="en-us_image_0000001349289865.png"></span></div>
</div>
<p id="mrs_01_24105__p1699188143415"><strong id="mrs_01_24105__b2570626202414">Template for creating a Distributed engine</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen1052617123413">ENGINE = Distributed(cluster_name, database_name, table_name, [sharding_key])</pre>
<p id="mrs_01_24105__p8570133316568">Parameters of a distributed table are described as follows:</p>
<ul id="mrs_01_24105__ul1159131917386"><li id="mrs_01_24105__li155912019183819"><strong id="mrs_01_24105__b15530132215268">cluster_name</strong>: specifies the cluster name. When a distributed table is read or written, the cluster configuration information is used to search for the corresponding ClickHouse instance node.</li><li id="mrs_01_24105__li915215011381"><strong id="mrs_01_24105__b184521113281">database_name</strong>: specifies the database name.</li><li id="mrs_01_24105__li2011652283812"><strong id="mrs_01_24105__b112118132815">table_name</strong>: specifies the name of a local table in the database. It is used to map a distributed table to a local table.</li><li id="mrs_01_24105__li15872573817"><strong id="mrs_01_24105__b143183416282">sharding_key</strong> (optional): specifies the sharding key, based on which a distributed table distributes data to each local table.</li></ul>
<p id="mrs_01_24105__p43073552347"><strong id="mrs_01_24105__b910635743817">Example</strong>:</p>
<pre class="screen" id="mrs_01_24105__screen9875122115489">-- Create a ReplicatedMergeTree local table named <strong id="mrs_01_24105__b878620142394">test</strong>.
CREATE TABLE default.test ON CLUSTER default_cluster_1
(
`EventDate` DateTime,
`id` UInt64
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/default/test', '{replica}')
PARTITION BY toYYYYMM(EventDate)
ORDER BY id
-- Create a distributed table named <strong id="mrs_01_24105__b116729814013">test_all</strong> based on the local table <strong id="mrs_01_24105__b31911615114017">test</strong>.
CREATE TABLE default.test_all ON CLUSTER default_cluster_1
(
`EventDate` DateTime,
`id` UInt64
)
ENGINE = Distributed(default_cluster_1, default, test, rand())</pre>
<p id="mrs_01_24105__p1976018534526"><strong id="mrs_01_24105__b9291918414">Rules for creating a distributed table</strong>:</p>
<ul id="mrs_01_24105__ul108591657125214"><li id="mrs_01_24105__li14181206538">When creating a distributed table, add <strong id="mrs_01_24105__b143311478452">ON CLUSTER</strong> <em id="mrs_01_24105__i35182052134520">cluster_name</em> to the table creation statement so that the statement can be executed once on a ClickHouse instance and then distributed to all instances in the cluster for execution.</li><li id="mrs_01_24105__li14124194012554">Generally, a distributed table is named in the following format: <em id="mrs_01_24105__i1691413337547">Local table name</em>_all. It forms a one-to-many mapping with local tables. Then, multiple local tables can be operated using the distributed table proxy.</li><li id="mrs_01_24105__li429019596552">Ensure that the structure of a distributed table is the same as that of local tables. If they are inconsistent, no error is reported during table creation, but an exception may be reported during data query or insertion.</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_01_2344.html">Using ClickHouse</a></div>
</div>
</div>