AUTO_INCREMENT
Since version 3.0, StarRocks supports the 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 the AUTO_INCREMENT column, StarRocks 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.
You need to specify an 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.
CREATE TABLE test_tbl1
(
id BIGINT NOT NULL,
number BIGINT NOT NULL AUTO_INCREMENT
)
PRIMARY KEY (id)
DISTRIBUTED BY HASH(id)
PROPERTIES("replicated_storage" = "true");
Assign values for AUTO_INCREMENT columnβ
Assign values implicitlyβ
When you load data into a StarRocks table, you do not need to specify the values for the AUTO_INCREMENT column. StarRocks automatically assigns unique integer values for that column and inserts them into the table.
INSERT INTO test_tbl1 (id) VALUES (1);
INSERT INTO test_tbl1 (id) VALUES (2);
INSERT INTO test_tbl1 (id) VALUES (3),(4),(5);
View data in the table.
mysql > SELECT * FROM test_tbl1 ORDER BY id;
+------+--------+
| id | number |
+------+--------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
+------+--------+
5 rows in set (0.02 sec)
When you load data into a StarRocks table, you can also specify the values as DEFAULT for the AUTO_INCREMENT column. StarRocks automatically assigns unique integer values for that column and inserts them into the table.
INSERT INTO test_tbl1 (id, number) VALUES (6, DEFAULT);
View data in the table.
mysql > SELECT * FROM test_tbl1 ORDER BY id;
+------+--------+
| id | number |
+------+--------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 6 |
+------+--------+
6 rows in set (0.02 sec)
In actual usage, the following result may be returned when you view the data in the table. This is because StarRocks cannot guarantee that the values for the AUTO_INCREMENT column are strictly monotonic. But StarRocks can guarantee that the values roughly increase in chronological order. For more information, see Monotonicity.
mysql > SELECT * FROM test_tbl1 ORDER BY id;
+------+--------+
| id | number |
+------+--------+
| 1 | 1 |
| 2 | 100001 |
| 3 | 200001 |
| 4 | 200002 |
| 5 | 200003 |
| 6 | 200004 |
+------+--------+
6 rows in set (0.01 sec)