AUTO_INCREMENT column attribute, which can simplify data management. This topic introduces the application scenarios, usage and features of the AUTO_INCREMENT column attribute.
Introduction
When a new data row is loaded into a table and values are not specified for theAUTO_INCREMENT column, the database automatically assigns an integer value for the row’s AUTO_INCREMENT column as its unique ID across the table. The subsequent values for the AUTO_INCREMENT column automatically increase at a specific step starting from the ID of the row. An AUTO_INCREMENT column can be used to simplify data management and speed up some queries. Here are some application scenarios of an AUTO_INCREMENT column:
- Serve as primary keys: An
AUTO_INCREMENTcolumn can be used as the primary key to ensure that each row has a unique ID and make it easy to query and manage data. - Join tables: When multiple tables are joined, an
AUTO_INCREMENTcolumn can be used as the Join Key, which can expedite queries compared to using a column whose data type is STRING, for example, UUID. - Count the number of distinct values in a high-cardinality column: An
AUTO_INCREMENTcolumn can be used to represent the unique value column in a dictionary. Compared to directly counting distinct STRING values, counting distinct integer values of theAUTO_INCREMENTcolumn can sometimes improve the query speed by several times or even tens of times.
AUTO_INCREMENT column in the CREATE TABLE statement. The data types of an AUTO_INCREMENT column must be BIGINT. The value for an AUTO_INCREMENT column can be implicitly assigned or explicitly specified. It starts from 1, and increments by 1 for each new row.
Basic operations
Specify AUTO_INCREMENT column at table creation
Create a table named test_tbl1 with two columns, id and number. Specify the column number as the AUTO_INCREMENT column.
Assign values for AUTO_INCREMENT column
Assign values implicitly
When you load data into a table, you do not need to specify the values for theAUTO_INCREMENT column. The database automatically assigns unique integer values for that column and inserts them into the table.
DEFAULT for the AUTO_INCREMENT column. The database automatically assigns unique integer values for that column and inserts them into the table.
AUTO_INCREMENT column are strictly monotonic. But the database can guarantee that the values roughly increase in chronological order. For more information, see Monotonicity.
Specify values explicitly
You can also explicitly specify the values for theAUTO_INCREMENT column and insert them into the table.
AUTO_INCREMENT column at the same time. Because the specified values may be the same as the values generated by the database, breaking the global uniqueness of auto-incremented IDs.
Basic features
Uniqueness
In general, the database guarantees that the values for aAUTO_INCREMENT column are globally unique across a table. We recommend that you do not implicitly assign and explicitly specify the values for the AUTO_INCREMENT column at the same time. If you do so, it may break the global uniqueness of auto-incremented IDs. Here is a simple example: Create a table named test_tbl2 with two columns, id and number. Specify the column number as the AUTO_INCREMENT column.
AUTO_INCREMENT column number in the table test_tbl2.
test_tbl2.
Monotonicity
In order to improve the performance of allocating auto-incremented IDs, BEs cache some auto-incremented IDs locally. In this situation, the database cannot guarantee that the values for theAUTO_INCREMENT column are strictly monotonic. It can only be ensured that the values roughly increase in chronological order.
NOTE The number of auto-incremented IDs cached by the BEs is determined by the FE dynamic parameterFor example, a cluster has one FE node and two BE nodes. Create a table namedauto_increment_cache_size, which defaults to100,000. You can modify the value by usingADMIN SET FRONTEND CONFIG ("auto_increment_cache_size" = "xxx");
test_tbl3 and insert five rows of data as follows:
test_tbl3 do not monotonically increase, because the two BE nodes cache auto-incremented IDs, [1, 100000] and [100001, 200000], respectively. When data is loaded by using multiple INSERT statements, the data is sent to different BE nodes which allocate auto-incremented IDs independently. Therefore, it cannot be guaranteed that auto-incremented IDs are strictly monotonic.
Partial updates and AUTO_INCREMENT column
This section explains how to update only a few specified columns in a table that contains an AUTO_INCREMENT column.
NOTE Currently, only Primary Key tables support partial updates.
AUTO_INCREMENT column is primary key
You need to specify the primary key during partial updates. Therefore, if the AUTO_INCREMENT column is the primary key or part of the primary key, the user behavior for partial updates is exactly the same as when the AUTO_INCREMENT column is not defined.
-
Create a table
test_tbl4in the databaseexample_dband insert one data row. -
Prepare the CSV file my_data4.csv to update table
test_tbl4. The CSV file includes values for theAUTO_INCREMENTcolumn and does not include values for the columnjob1. The primary key of the first row already exists in tabletest_tbl4, while the primary key of the second row does not exist in the table. -
Run a Stream Load job and use the CSV file to update table
test_tbl4. -
Query the updated table. The first row of data already exists in table
test_tbl4, and the value for the columnjob1remains unchanged. The second row of data is newly inserted, and because the default value for the columnjob1is not specified, the partial update framework directly sets the value for this column to0.
AUTO_INCREMENT column is not primary key
If the AUTO_INCREMENT column is not a primary key or a part of the primary key, and auto-incremented IDs are not provided in a Stream Load job, the following situations occur:
- If the row already exists in the table, the database does not update the auto-incremented ID.
- If the row is newly loaded into the table, the database generates a new auto-incremented ID.
-
In the database
example_db, create a tabletest_tbl5and specify the columnjob1as theAUTO_INCREMENTcolumn and insert a data row into the tabletest_tbl5. -
Prepare a CSV file my_data5.csv to update table
test_tbl5. The CSV file does not contain values for theAUTO_INCREMENTcolumnjob1. The primary key of the first row already exists in the table while the primary keys of the second and third rows do not. -
Run a Stream Load job to load data from the CSV file into table
test_tbl5. -
Query the updated table. The first row of data already exists in table
test_tbl5, so theAUTO_INCREMENTcolumnjob1retains its original value. The second and third rows of data are newly inserted, so the database generate new values for theAUTO_INCREMENTcolumnjob1.
Usage Notes
By default, the system will allow the optimizer to choose theCOUNT(DISTINCT) implementation.
When deduplicating low- and medium-cardinality columns, you can set count_distinct_implementation to multi_count_distinct for a query via query hint to verify the multi_distinct_count implementation for precise counting. However, do not use the multi_distinct_count implementation when deduplicating high-cardinality columns, because its HashSet status and final merging can cause excessive memory consumption and even OOM.
Limits
-
When a table with an
AUTO_INCREMENTcolumn is created,'replicated_storage' = 'true'must be set to ensure that all replicas have the same auto-incremented IDs. -
Each table can have only one
AUTO_INCREMENTcolumn. -
The data type of the
AUTO_INCREMENTcolumn must be BIGINT. -
The
AUTO_INCREMENTcolumn must beNOT NULLand does not have a default value. -
You can delete data from a Primary Key table with an
AUTO_INCREMENTcolumn. However, if theAUTO_INCREMENTcolumn is not the primary key or part of the primary key, you need to note the following limits when you delete data in the following scenarios:- During the DELETE operation, there is also a load job for partial updates, which only contains UPSERT operations. If both the UPSERT and DELETE operations hit the same data row and the UPSERT operation is executed after the DELETE operation, the UPSERT operation may not take effect.
- There is a load job for partial updates, which includes multiple UPSERT and DELETE operations on the same data row. If a certain UPSERT operation is executed after the DELETE operation, the UPSERT operation may not take effect.
-
Adding the
AUTO_INCREMENTattribute by using ALTER TABLE is not supported. -
Since version 3.1, the database’s shared-data mode supports the
AUTO_INCREMENTattribute. -
The database does not support specifying the starting value and step size for the
AUTO_INCREMENTcolumn.