zhannngchen commented on code in PR #29230: URL: https://github.com/apache/doris/pull/29230#discussion_r1437500118
########## docs/zh-CN/docs/advanced/auto-increment.md: ########## @@ -0,0 +1,326 @@ +--- +{ + "title": "自增列", + "language": "zh-CN" +} +--- + +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. +--> + +# 自增列 + +<version since="2.1"> + +</version> + +自增列功能支持了在导入过程中对用户没有在自增列上指定值的数据行分配一个表内唯一的值。 + +## 功能说明 + +对于含有自增列的表,用户在在导入数据时: +- 如果导入的目标列中不包含自增列,则自增列将会被Doris自动生成的值填充。 +- 如果导入的目标列中包含自增列,则导入数据中该列的null值将会被Doris自动生成的值替换,非null值则保持不变。 + +### 唯一性 + +Doris保证了自增列上生成的值具有**表内唯一性**。但需要注意的是,**自增列的唯一性仅保证由Doris自动填充的值具有唯一性,而不考虑由用户提供的值**,如果用户同时对该表通过显示指定自增列的方式插入了用户提供的值,则不能保证这个唯一性。 + +### 单调性 + +Doris保证了在自增列上填充的值**在一个分桶内**是严格单调递增的。但需要注意的是,Doris**不能保证**在物理时间上后一次导入的数据在自增列上填充的值比前一次更大,这是因为处于性能考虑,每个BE上都会缓存一部分预先分配的自增列的值,每个BE上缓存的值互不相交。因此,不能根据自增列分配出的值的大小来判断导入时间上的先后顺序。同时,由于BE上缓存的存在,Doris仅能保证自增列上自动填充的值在一定程度上是稠密的,但**不能保证**在一次导入中自动填充的自增列的值是完全连续的。因此可能会出现一次导入中自增列自动填充的值具有一定的跳跃性的现象。 + + +## 语法 + +要使用自增列,需要在建表[CREATE-TABLE](../../sql-manual/sql-reference/Data-Definition-Statements/Create/CREATE-TABLE)时为对应的列添加`AUTO_INCREMENT`属性。 + +### 示例 + +1. 创建一个Dupliciate模型表,其中一个key列是自增列 + + ```sql + CREATE TABLE `tbl` ( + `id` BIGINT NOT NULL AUTO_INCREMENT, + `value` BIGINT NOT NULL + ) ENGINE=OLAP + DUPLICATE KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 3" + ); + ``` + +2. 创建一个Dupliciate模型表,其中一个value列是自增列 + + ```sql + CREATE TABLE `tbl` ( + `uid` BIGINT NOT NULL, + `name` BIGINT NOT NULL, + `id` BIGINT NOT NULL AUTO_INCREMENT, + `value` BIGINT NOT NULL + ) ENGINE=OLAP + DUPLICATE KEY(`uid`, `name`) + DISTRIBUTED BY HASH(`uid`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 3" + ); + ``` + +3. 创建一个Unique模型表,其中一个key列是自增列 + + ```sql + CREATE TABLE `tbl` ( + `id` BIGINT NOT NULL AUTO_INCREMENT, + `name` varchar(65533) NOT NULL, + `value` int(11) NOT NULL + ) ENGINE=OLAP + UNIQUE KEY(`id`) + DISTRIBUTED BY HASH(`id`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 3", + "enable_unique_key_merge_on_write" = "true" + ); + ``` + +4. 创建一个Unique模型表,其中一个value列是自增列 + + ```sql + CREATE TABLE `tbl` ( + `text` varchar(65533) NOT NULL, + `id` BIGINT NOT NULL AUTO_INCREMENT, + ) ENGINE=OLAP + UNIQUE KEY(`text`) + DISTRIBUTED BY HASH(`text`) BUCKETS 10 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 3", + "enable_unique_key_merge_on_write" = "true" + ); + ``` + +### 约束和限制 + +1. 仅Duplicate模型表和Unique模型表可以包含自增列。 +2. 一张表最多只能包含一个自增列。 +3. 自增列的类型必须是BIGINT类型,且必须为NOT NULL。 Review Comment: Is it support schema change? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org