MySQL HeatWave User Guide

5.4.6 INSERT ... SELECT Statements

This topic describes how to run a SELECT query and insert the results into a target table using the INSERT ... SELECT statement. The target table can be an InnoDB table and does not need to use a secondary engine.

When the query is offloaded to MySQL HeatWave, it is processed faster and improves the overall performance of the operation.

Before You Begin

Execute INSERT ... SELECT Statement

When you execute an INSERT ... SELECT statement, the SELECT query is offloaded to MySQL HeatWave for execution, and result is copied into the target table in the DB System. This offloading reduces execution time, especially for long-running and complex queries. However, it may not be beneficial for SELECT queries that produce large result sets, as the large volume of DML operations on the DB System can limit performance improvements.

For example, the following statement inserts data into the flight_from_US table on the DB System, by selecting rows from the flight table on MySQL HeatWave. It filters the flights that depart from airports located in the United States, based on matching airport IDs present in table airport_geo.

mysql>  CREATE TABLE flight_from_US LIKE flight; 
Query OK, 0 rows affected (0.2368 sec) 

INSERT INTO flight_from_US SELECT * FROM flight WHERE `from` IN (SELECT airport_id FROM airport_geo WHERE country='UNITED STATES')
Query OK, 99431 rows affected (4.2551 sec)

Records: 99431  Duplicates: 0  Warnings: 0

EXPLAIN INSERT INTO flight_from_US SELECT * FROM flight WHERE `from` IN (SELECT airport_id FROM airport_geo WHERE country='UNITED STATES');
*************************** 1. row ***************************
           id: 1
  select_type: NONE
        table: NULL
   partitions: NULL
         type: NULL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: NULL
     filtered: NULL
        Extra: Using secondary engine RAPID. Use EXPLAIN FORMAT=TREE to show the plan.
1 row in set, 1 warning (0.2304 sec)

Note the following:

What's Next

Learn how to query views.