- Creates temporary partitions according to the partitions that store the original data.
- Inserts data into the temporary partitions.
- Swaps the original partitions with the temporary partitions.
NOTE If you need to verify the data before overwriting it, instead of using INSERT OVERWRITE, you can follow the above procedures to overwrite your data and verify it before swapping the partitions.From v3.4.0 onwards, the database supports a new semantic - Dynamic Overwrite for INSERT OVERWRITE with partitioned tables. For more information, see Dynamic Overwrite.
Precautions
- You can cancel a synchronous INSERT transaction only by pressing the Ctrl and C keys from your MySQL client.
- You can submit an asynchronous INSERT task using SUBMIT TASK.
- As for the current version of the database, the INSERT transaction fails by default if the data of any rows does not comply with the schema of the table. For example, the INSERT transaction fails if the length of a field in any row exceeds the length limit for the mapping field in the table. You can set the session variable
enable_insert_stricttofalseto allow the transaction to continue by filtering out the rows that mismatch the table. - If you execute the INSERT statement frequently to load small batches of data into the database, excessive data versions are generated. It severely affects query performance. We recommend that, in production, you should not load data with the INSERT command too often or use it as a routine for data loading on a daily basis. If your application or analytic scenario demand solutions to loading streaming data or small data batches separately, we recommend you use Apache KafkaĀ® as your data source and load the data via Routine Load.
- If you execute the INSERT OVERWRITE statement, the database creates temporary partitions for the partitions which store the original data, inserts new data into the temporary partitions, and swaps the original partitions with the temporary partitions. All these operations are executed in the FE Leader node. Hence, if the FE Leader node crashes while executing INSERT OVERWRITE command, the whole load transaction will fail, and the temporary partitions will be truncated.
Preparation
Check privileges
You can load data into tables only as a user who has the INSERT privilege on those tables. If you do not have the INSERT privilege, follow the instructions provided in GRANT to grant the INSERT privilege to the user that you use to connect to your cluster. The syntax is
GRANT INSERT ON TABLE <table_name> IN DATABASE <database_name> TO { ROLE <role_name> | USER <user_identity>}.
Create objects
Create a database namedload_test, and create a table insert_wiki_edit as the destination table and a table source_wiki_edit as the source table.
NOTE Examples demonstrated in this topic are based on the tableinsert_wiki_editand the tablesource_wiki_edit. If you prefer working with your own tables and data, you can skip the preparation and move on to the next step.
NOTICE Since v2.5.7, the database can automatically set the number of buckets (BUCKETS) when you create a table or add a partition. You no longer need to manually set the number of buckets. For detailed information, see set the number of buckets.
Insert data via INSERT INTO VALUES
You can append one or more rows to a specific table by using INSERT INTO VALUES command. Multiple rows are separated by comma (,). For detailed instructions and parameter references, see SQL Reference - INSERT.CAUTION Inserting data via INSERT INTO VALUES merely applies to the situation when you need to verify a DEMO with a small dataset. It is not recommended for a massive testing or production environment. To load mass data into the database, see Loading options for other options that suit your scenarios.The following example inserts two rows into the data source table
source_wiki_edit with the label insert_load_wikipedia. Label is the unique identification label for each data load transaction within the database.
Insert data via INSERT INTO SELECT
You can load the result of a query on a data source table into the target table via INSERT INTO SELECT command. INSERT INTO SELECT command performs ETL operations on the data from the data source table, and loads the data into an internal table in the database. The data source can be one or more internal or external tables, or even data files on cloud storage. The target table MUST be an internal table in the database. For detailed instructions and parameter references, see SQL Reference - INSERT.Insert data from an internal or external table into an internal table
NOTE Inserting data from an external table is identical to inserting data from an internal table. For simplicity, we only demonstrate how to insert data from an internal table in the following examples.
- The following example inserts the data from the source table to the target table
insert_wiki_edit.
- The following example inserts the data from the source table to the
p06andp12partitions of the target tableinsert_wiki_edit. If no partition is specified, the data will be inserted into all partitions. Otherwise, the data will be inserted only into the specified partition(s).
text
p06 and p12 partitions, the data will not be returned in a query.
- The following example inserts the
event_timeandchannelcolumns from the source table to the target tableinsert_wiki_edit. Default values are used in the columns that are not specified here.
Insert data directly from files in an external source using FILES()
From v3.1 onwards, the database supports directly loading data from files on cloud storage using the INSERT command and the FILES() function, thereby you do not need to create an external catalog or file external table first. Besides, FILES() can automatically infer the table schema of the files, greatly simplifying the process of data loading. The following example inserts data rows from the Parquet file parquet/insert_wiki_edit_append.parquet within the AWS S3 bucketinserttest into the table insert_wiki_edit:
Overwrite data via INSERT OVERWRITE VALUES
You can overwrite a specific table with one or more rows by using INSERT OVERWRITE VALUES command. Multiple rows are separated by comma (,). For detailed instructions and parameter references, see SQL Reference - INSERT.CAUTION Overwriting data via INSERT OVERWRITE VALUES merely applies to the situation when you need to verify a DEMO with a small dataset. It is not recommended for a massive testing or production environment. To load mass data into the database, see Loading options for other options that suit your scenarios.Query the source table and the target table to make sure there is data in them.
source_wiki_edit with two new rows.
Overwrite data via INSERT OVERWRITE SELECT
You can overwrite a table with the result of a query on a data source table via INSERT OVERWRITE SELECT command. INSERT OVERWRITE SELECT statement performs ETL operations on the data from one or more internal or external tables, and overwrites an internal table with the data For detailed instructions and parameter references, see SQL Reference - INSERT.NOTE Loading data from an external table is identical to loading data from an internal table. For simplicity, we only demonstrate how to overwrite the target table with the data from an internal table in the following examples.Query the source table and the target table to make sure that they hold different rows of data.
- The following example overwrites the table
insert_wiki_editwith the data from the source table.
- The following example overwrites the
p06andp12partitions of the tableinsert_wiki_editwith the data from the source table.
text
p06 and p12 partitions, the data will not be returned in a query.
PARTITION BY column strategy, INSERT OVERWRITE supports creating new partitions in the destination table by specifying the value of the partition key. Existing partitions are overwritten as usual.
The following example creates the partitioned table activity, and creates a new partition in the table while inserting data into it:
- The following example overwrites the target table
insert_wiki_editwith theevent_timeandchannelcolumns from the source table. The default value is assigned to the columns into which no data is overwritten.
Dynamic Overwrite
From v3.4.0 onwards, the database supports a new semantic - Dynamic Overwrite for INSERT OVERWRITE with partitioned tables. Currently, the default behavior of INSERT OVERWRITE is as follows:- When overwriting a partitioned table as a whole (that is, without specifying the PARTITION clause), new data records will replace the data in their corresponding partitions. If there are partitions that are not involved, they will be truncated while the others are overwritten.
- When overwriting an empty partitioned table (that is, with no partitions in it) and specifying the PARTITION clause, the system returns an error
ERROR 1064 (HY000): Getting analyzing error. Detail message: Unknown partition 'xxx' in table 'yyy'. - When overwriting a partitioned table and specifying a non-existent partition in the PARTITION clause, the system returns an error
ERROR 1064 (HY000): Getting analyzing error. Detail message: Unknown partition 'xxx' in table 'yyy'. - When overwriting a partitioned table with data records that do not match any of the specified partitions in the PARTITION clause, the system either returns an error
ERROR 1064 (HY000): Insert has filtered data in strict mode(if the strict mode is enabled) or filters the unqualified data records (if the strict mode is disabled).
dynamic_overwrite to true.
Enable Dynamic Overwrite in the current session:
Insert data into a table with generated columns
A generated column is a special column whose value is derived from a pre-defined expression or evaluation based on other columns. Generated columns are especially useful when your query requests involve evaluations of expensive expressions, for example, querying a certain field from a JSON value, or calculating ARRAY data. The database evaluates the expression and stores the results in the generated columns while data is being loaded into the table, thereby avoiding the expression evaluation during queries and improving the query performance. You can load data into a table with generated columns using INSERT. The following example creates a tableinsert_generated_columns and inserts a row into it. The table contains two generated columns: avg_array and get_string. avg_array calculates the average value of ARRAY data in data_array, and get_string extracts the strings from the JSON path a in data_json.
NOTE Directly loading data into generated columns is not supported.You can query the table to check the data within it.
INSERT data with PROPERTIES
From v3.4.0 onwards, INSERT statements support configuring PROPERTIES, which can serve a wide variety of purposes. PROPERTIES overrides their corresponding variables.Enable strict mode
From v3.4.0 onwards, you can enable strict mode and setmax_filter_ratio for INSERT from FILES(). Strict mode for INSERT from FILES() has the same behavior as that of other loading methods.
If you want to load a dataset with some unqualified rows, you either filter these unqualified rows or load them and assign NULL values to the unqualified columns. You can achieve them by using the properties strict_mode and max_filter_ratio.
- To filter the unqualified rows: set
strict_modetotrue, andmax_filter_ratioto a desired value. - To load all unqualified rows with NULL values: set
strict_modetofalse.
inserttest into the table insert_wiki_edit, enables strict mode to filter the unqualified data records, and tolerates at most 10% of error data:
strict_mode and max_filter_ratio are supported only for INSERT from FILES(). INSERT from tables does not support these properties.
Set timeout duration
From v3.4.0 onwards, you can set the timeout duration for INSERT statements with properties. The following example inserts the data from the source tablesource_wiki_edit to the target table insert_wiki_edit with the timeout duration set to 2 seconds.
insert_timeout, which applies to operations involving INSERT (for example, UPDATE, DELETE, CTAS, materialized view refresh, statistics collection, and PIPE). In versions earlier than v3.4.0, the corresponding variable is query_timeout.
Match column by name
By default, INSERT matches the columns in the source and the target tables by their positions, that is, the mapping of the columns in the statement. The following example explicitly matches each column in the source and target tables by their positions:channel and user in either the column list or the SELECT statement.
channel in the target table insert_wiki_edit will be filled with data from user in the source table source_wiki_edit.
By adding BY NAME clause in the INSERT statement, the system will detect the column names in the source and the target tables, and match the columns with the same name.
Note
- You cannot specify the column list if
BY NAMEis specified. - If
BY NAMEis not specified, the system matches the columns by the position of the columns in the column list and the SELECT statement.
channel and user will not change the column mapping.
Load data asynchronously using INSERT
Loading data with INSERT submits a synchronous transaction, which may fail because of session interruption or timeout. You can submit an asynchronous INSERT transaction using SUBMIT TASK. This feature is supported since StarRocks v2.5.- The following example asynchronously inserts the data from the source table to the target table
insert_wiki_edit.
- The following example asynchronously overwrites the table
insert_wiki_editwith the data from the source table.
- The following example asynchronously overwrites the table
insert_wiki_editwith the data from the source table, and extends the query timeout to100000seconds using hint.
- The following example asynchronously overwrites the table
insert_wiki_editwith the data from the source table, and specifies the task name asasync.
task_runs in Information Schema.
The following example checks the status of the INSERT task async.
Check the INSERT job status
Check via the result
A synchronous INSERT transaction returns different status in accordance with the result of the transaction.- Transaction succeeds
- Transaction fails
tracking_url.
Check via Information Schema
You can use the SELECT statement to query the results of one or more load jobs from theloads table in the information_schema database. This feature is supported from v3.1 onwards.
Example 1: Query the results of load jobs executed on the load_test database, sort the results by creation time (CREATE_TIME) in descending order, and only return the top result.
insert_load_wikipedia) executed on the load_test database:
Check via curl command
You can check the INSERT transaction status by using curl command. Launch a terminal, and execute the following command:insert_load_wikipedia.
NOTE
If you use an account for which no password is set, you need to input only <username>:.
The return is as follows:
Configuration
You can set the following configuration items for INSERT transaction:- FE configuration
- Session variables