> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# insert_* セッション設定

> insert_* の生成済みグループに含まれる ClickHouse のセッション設定。

export const SettingsInfoBlock = ({type, default_value, changeable_without_restart}) => {
  return <div className="not-prose" style={{
    display: "flex",
    flexWrap: "wrap",
    alignItems: "baseline",
    columnGap: "0.5rem",
    rowGap: "0.125rem",
    margin: "0.375rem 0",
    fontSize: "0.8125rem",
    lineHeight: "1.125rem"
  }}>
      <div style={{
    fontWeight: 600,
    opacity: 0.72
  }}>型</div>
      <div style={{
    overflowWrap: "anywhere"
  }}>{type}</div>
      <div style={{
    fontWeight: 600,
    opacity: 0.72,
    marginInlineStart: "0.5rem"
  }}>デフォルト値</div>
      <div style={{
    overflowWrap: "anywhere"
  }}>{default_value}</div>
      {changeable_without_restart && <div style={{
    fontWeight: 600,
    opacity: 0.72,
    marginInlineStart: "0.5rem"
  }}>
          再起動せずに変更可能
        </div>}
      {changeable_without_restart && <div style={{
    overflowWrap: "anywhere"
  }}>
          {changeable_without_restart}
        </div>}
    </div>;
};

これらの設定は [system.settings](/docs/ja/reference/system-tables/settings) で利用でき、[ソースコード](https://github.com/ClickHouse/ClickHouse/blob/master/src/Core/Settings.cpp) から自動生成されています。

<div id="insert_allow_materialized_columns">
  ## insert\_allow\_materialized\_columns
</div>

<SettingsInfoBlock type="Bool" default_value="0" />

この設定を有効にすると、INSERT でマテリアライズドカラムを使用できます。

<div id="insert_deduplicate">
  ## insert\_deduplicate
</div>

<SettingsInfoBlock type="Bool" default_value="1" />

`INSERT` のブロックの重複排除 (Replicated\* テーブル向け) を有効または無効にします。

設定可能な値:

* 0 — 無効。
* 1 — 有効。

デフォルトでは、`INSERT` ステートメントによってレプリケートテーブルに挿入されたブロックは重複排除されます ([Data Replication](/docs/ja/reference/engines/table-engines/mergetree-family/replication) を参照) 。
レプリケートテーブルでは、デフォルトで各パーティションにつき直近 100 個のブロックのみが重複排除されます ([replicated\_deduplication\_window](/docs/ja/reference/settings/merge-tree-settings#replicated_deduplication_window)、[replicated\_deduplication\_window\_seconds](/docs/ja/reference/settings/merge-tree-settings#replicated_deduplication_window_seconds) を参照) 。
非レプリケートテーブルについては、[non\_replicated\_deduplication\_window](/docs/ja/reference/settings/merge-tree-settings#non_replicated_deduplication_window) を参照してください。

<div id="insert_deduplication_token">
  ## insert\_deduplication\_token
</div>

この設定を使用すると、MergeTree/ReplicatedMergeTree で独自の重複排除セマンティクスを指定できます。
たとえば、各 INSERT ステートメントでこの設定に一意の値を指定することで、
同じ挿入データが重複排除されるのを防げます。

設定可能な値:

* 任意の文字列

`insert_deduplication_token` は、空でない場合に*のみ*重複排除に使用されます。

レプリケートテーブルでは、デフォルトで各パーティションごとに直近 100 件の insert のみが重複排除されます ([replicated\_deduplication\_window](/docs/ja/reference/settings/merge-tree-settings#replicated_deduplication_window)、[replicated\_deduplication\_window\_seconds](/docs/ja/reference/settings/merge-tree-settings#replicated_deduplication_window_seconds) を参照) 。
レプリケーションされていないテーブルについては、[non\_replicated\_deduplication\_window](/docs/ja/reference/settings/merge-tree-settings#non_replicated_deduplication_window) を参照してください。

<Note>
  `insert_deduplication_token` はパーティション単位で追跡されるため、1 回の insert で書き込まれる複数のパーティションで同じトークンを使用できます。トークンがない場合、デフォルトの内容チェックサムは挿入された block 全体に対して計算されるため、insert が重複排除されるのは、そのデータ全体が以前の insert (再試行) と一致する場合のみです。単一パーティションの行が、たまたま別の insert と一致しただけでは重複排除されません。
</Note>

例:

```sql theme={null}
CREATE TABLE test_table
( A Int64 )
ENGINE = MergeTree
ORDER BY A
SETTINGS non_replicated_deduplication_window = 100;

INSERT INTO test_table SETTINGS insert_deduplication_token = 'test' VALUES (1);

-- the next insert won't be deduplicated because insert_deduplication_token is different
INSERT INTO test_table SETTINGS insert_deduplication_token = 'test1' VALUES (1);

-- the next insert will be deduplicated because insert_deduplication_token
-- is the same as one of the previous
INSERT INTO test_table SETTINGS insert_deduplication_token = 'test' VALUES (2);

SELECT * FROM test_table

┌─A─┐
│ 1 │
└───┘
┌─A─┐
│ 1 │
└───┘
```

<div id="insert_null_as_default">
  ## insert\_null\_as\_default
</div>

<SettingsInfoBlock type="Bool" default_value="1" />

[Nullable](/docs/ja/reference/data-types/nullable) ではないデータ型のカラムに対して、[NULL](/docs/ja/reference/syntax#null) の代わりに [デフォルト値](/docs/ja/reference/statements/create/table#default_values) を挿入するかどうかを切り替えます。
カラム型が Nullable ではなく、この設定が無効な場合、`NULL` を挿入すると例外が発生します。カラム型が Nullable の場合は、この設定に関係なく、`NULL` 値はそのまま挿入されます。

この設定は [INSERT ... SELECT](/docs/ja/reference/statements/insert-into#inserting-the-results-of-select) クエリに適用されます。`SELECT` サブクエリは `UNION ALL` 句で連結できる点に注意してください。

設定可能な値:

* 0 — Nullable ではないカラムに `NULL` を挿入すると例外が発生します。
* 1 — `NULL` の代わりにカラムのデフォルト値が挿入されます。

<div id="insert_shard_id">
  ## insert\_shard\_id
</div>

<SettingsInfoBlock type="UInt64" default_value="0" />

`0` でない場合、データを同期的に挿入する [Distributed](/docs/ja/reference/engines/table-engines/special/distributed) テーブルの分片を指定します。

`insert_shard_id` の値が正しくない場合、サーバーは例外をスローします。

`requested_cluster` の分片数を取得するには、サーバーの設定を確認するか、このクエリを使用します。

```sql theme={null}
SELECT uniq(shard_num) FROM system.clusters WHERE cluster = 'requested_cluster';
```

設定可能な値:

* 0 — 無効。
* 対応する [Distributed](/docs/ja/reference/engines/table-engines/special/distributed) テーブルの `1` から `shards_num` までの任意の数値。

**例**

クエリ:

```sql theme={null}
CREATE TABLE x AS system.numbers ENGINE = MergeTree ORDER BY number;
CREATE TABLE x_dist AS x ENGINE = Distributed('test_cluster_two_shards_localhost', currentDatabase(), x);
INSERT INTO x_dist SELECT * FROM numbers(5) SETTINGS insert_shard_id = 1;
SELECT * FROM x_dist ORDER BY number ASC;
```

結果:

```text theme={null}
┌─number─┐
│      0 │
│      0 │
│      1 │
│      1 │
│      2 │
│      2 │
│      3 │
│      3 │
│      4 │
│      4 │
└────────┘
```
