Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

insert_* 会话设置

这些设置可在 system.settings 中查看,并根据 源代码 自动生成。

insert_allow_materialized_columns

类型
Bool
默认值
0

如果启用此设置,则允许在 INSERT 中插入 materialized 列。

insert_deduplicate

类型
Bool
默认值
1

启用或禁用 INSERT 的块去重 (适用于复制表) 。

仅当 deduplicate_insert 设置为 backward_compatible_choice 时,此设置才会生效。

可能的值:

  • 0 — 禁用。
  • 1 — 启用。

默认情况下,通过 INSERT 语句插入到复制表中的块会进行去重 (请参见 Data Replication) 。 对于复制表,默认情况下每个分区仅对最近的 100 个块进行去重 (请参见 replicated_deduplication_windowreplicated_deduplication_window_seconds) 。 对于非复制表,请参见 non_replicated_deduplication_window

insert_deduplication_token

该设置允许用户在 MergeTree/ReplicatedMergeTree 中自定义去重语义。 例如,在每条 INSERT 语句中为该设置提供一个唯一值, 即可避免相同的插入数据被去重。

可能的值:

  • 任意字符串

只有当 insert_deduplication_token 非空时,才会将其用于去重。

对于复制表,默认每个分区仅会对最近 100 次插入进行去重 (参见 replicated_deduplication_windowreplicated_deduplication_window_seconds) 。 对于非复制表,请参见 non_replicated_deduplication_window

示例:

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

insert_null_as_default

类型
Bool
默认值
1

启用或禁用:向非 Nullable 数据类型的列中插入数据时,使用 默认值 替代 NULL。 如果列类型不是 Nullable 且此设置被禁用,则插入 NULL 会导致异常。如果列类型是 Nullable,则无论此设置如何,NULL 值都会按原样插入。

此设置适用于 INSERT … SELECT 查询。请注意,SELECT 子查询可以使用 UNION ALL 子句进行拼接。

可能的值:

  • 0 — 向非 Nullable 列插入 NULL 会导致异常。
  • 1 — 插入列的默认值而不是 NULL

insert_shard_id

类型
UInt64
默认值
0

如果不为 0,则指定将数据同步插入到 Distributed 表中的哪个分片。

如果 insert_shard_id 的值不正确,服务器将抛出异常。

要获取 requested_cluster 中的分片数量,可以检查服务器配置,或使用以下查询:

SELECT uniq(shard_num) FROM system.clusters WHERE cluster = 'requested_cluster';

可能的值:

  • 0 — 已禁用。
  • 对应的 Distributed 表中,从 1shards_num 之间的任意数字。

示例

查询:

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;

结果:

┌─number─┐
│      0 │
│      0 │
│      1 │
│      1 │
│      2 │
│      2 │
│      3 │
│      3 │
│      4 │
│      4 │
└────────┘
Navigation