When to Avoid INSERT INTO SELECT: Alternative Methods for Efficient Data Insertion with Discounts in MariaDB


The Issue:

In SQL, combining an INSERT and SELECT statement into a single INSERT INTO SELECT can sometimes be inefficient. This happens because the database engine performs these operations differently compared to running them separately:

  1. Read and Hold Data: During INSERT INTO SELECT, the SELECT portion retrieves data first. However, unlike a regular SELECT where the results are displayed, the database holds onto this data.
  2. Insert One by One: For each row retrieved, the database then performs a separate INSERT operation to insert that single row into the target table.

Reasons for Slowness:

  • Locking: Each INSERT within the combined statement might acquire locks on the target table, causing delays if other operations need to access the same table.
  • Overhead: There's extra processing involved in managing the temporary result set from the SELECT before inserting each row.

Separate Statements are Faster:

When you run INSERT and SELECT separately, the database engine can potentially optimize them independently. The SELECT might leverage indexes for faster retrieval, and the INSERT might insert data in larger batches, improving efficiency.


  • Check for Implicit Conversions: If the data types between the source and target tables differ, implicit conversions can slow things down. Ensure compatible data types.
  • Analyze Table Statistics: Outdated table statistics can lead to suboptimal execution plans. Use ANALYZE TABLE to update them.
  • Consider Alternatives: In some cases, using temporary tables or bulk loading techniques might be faster than INSERT INTO SELECT.

When to Separate:

If you're facing performance issues with INSERT INTO SELECT, consider separating the statements and analyzing the execution plans of both the original statement and the separated versions. This can help identify bottlenecks and optimize the process.

Scenario: We have two tables, Products and DiscountedProducts. Products has columns product_id, name, and price. We want to create a new table, DiscountedProducts, with a 10% discount on the price from the Products table.


INSERT INTO DiscountedProducts (product_id, name, discounted_price)
SELECT product_id, name, price * 0.9 AS discounted_price
FROM Products;

This statement retrieves all products from Products, calculates a discounted price for each, and then inserts them one by one into DiscountedProducts.

Faster Separate Statements:

-- Step 1: Select data with discount calculation
SELECT product_id, name, price * 0.9 AS discounted_price
INTO OUTFILE '/tmp/discounted_products.csv'
FROM Products;

-- Step 2: Bulk insert from temporary file
LOAD DATA LOCAL INFILE '/tmp/discounted_products.csv'
INTO TABLE DiscountedProducts

This approach is faster because:

  1. Faster Data Retrieval: The SELECT writes the results with discounts directly to a temporary file, potentially using optimizations for data retrieval.
  2. Bulk Loading: The LOAD DATA statement bulk inserts data from the file into the table, which can be more efficient than single-row inserts.

Note: This example uses a temporary file for illustration. You might need to adjust the file path based on your environment.

Temporary Tables:

  • Create a temporary table with the desired structure to hold the discounted product data.
  • Use INSERT ... SELECT to populate the temporary table with the discounted prices from the Products table.
  • Perform a single INSERT from the temporary table into the DiscountedProducts table.

This approach avoids the overhead of single-row inserts and leverages a single INSERT for the final transfer.


CREATE TEMPORARY TABLE TempDiscountedProducts (
  product_id INT,
  name VARCHAR(255),
  discounted_price DECIMAL(10,2)

INSERT INTO TempDiscountedProducts (product_id, name, discounted_price)
SELECT product_id, name, price * 0.9 AS discounted_price
FROM Products;

INSERT INTO DiscountedProducts (product_id, name, discounted_price)
SELECT * FROM TempDiscountedProducts;

DROP TEMPORARY TABLE TempDiscountedProducts;

Stored Procedures:

  • Create a stored procedure that encapsulates the logic for calculating discounts and inserting data.
  • Within the procedure, you can use separate SELECT and INSERT statements potentially with optimizations for reusability.

This approach allows for modularity and potentially better performance by pre-compiling the logic.

Example (Basic Structure):

CREATE PROCEDURE CreateDiscountedProducts()
  DECLARE discount DECIMAL(5,2) DEFAULT 0.1;

  INSERT INTO DiscountedProducts (product_id, name, discounted_price)
  SELECT p.product_id, p.name, p.price * discount
  FROM Products AS p;
END //

CALL CreateDiscountedProducts();


  • Create a trigger on the Products table that fires on INSERT or UPDATE events.
  • Within the trigger, calculate the discounted price and insert a new row into the DiscountedProducts table.

This approach can be useful for maintaining consistency between the tables, but be cautious of potential performance overhead for frequent inserts/updates in Products.

sql database mariadb

Understanding SQL Cursors: When to Use Them (and When Not To)

SQL Cursors Explained:Purpose: Cursors in SQL (Structured Query Language) act as iterators, enabling you to fetch and process data from a database one row at a time...

Can Table Columns with a Foreign Key Be NULL in SQL (MySQL)?

I'd be glad to explain whether table columns with foreign keys can be NULL in SQL, specifically MySQL:Foreign Keys and Data Integrity...

Randomness at Your Fingertips: How to Select Random Rows in SQLite

Methods for Selecting Random Rows:ORDER BY RANDOM() with LIMIT:This method leverages the RANDOM() function that generates a random number between 0 and 1.We use ORDER BY RANDOM() to sort the table rows randomly...

Effectively Deleting Fields in MongoDB Collections

Concepts:MongoDB: A NoSQL database that stores data in flexible, document-like structures.MongoDB Query: A specific command used to interact with MongoDB data...

Extracting Data Based on Text Content: Understanding SQL SELECT WHERE Field Contains Words

Breakdown:SELECT: This keyword initiates the process of retrieving data from a database table. It's followed by a list of columns (fields) you want to extract...

sql database mariadb