Skip to main content
Documents
Clauses StatementsKeywordsExpressionsFunctionsIndexesPerformance Optimization

MySQL Temporary Tables and Performance Optimization

Performance optimization in MySQL involves strategies to improve query execution speed and resource management. Using temporary tables is one such strategy that allows you to store intermediate results temporarily and process complex queries more efficiently.

Usage

Temporary tables are used when executing complex joins or subqueries to store intermediate results, thus improving the performance of the main query. They are particularly useful for breaking down complicated operations into simpler parts. Temporary tables are session-specific and are automatically dropped at the end of the session if not explicitly dropped earlier.


CREATE TEMPORARY TABLE temp_table AS
SELECT column1, column2
FROM original_table
WHERE condition;

In this syntax, `CREATE TEMPORARY TABLE temp_table AS` creates a temporary table named `temp_table` that stores the results of the `SELECT` query.

Examples

1. Basic Temporary Table Creation


CREATE TEMPORARY TABLE temp_customers AS
SELECT customer_id, customer_name
FROM customers
WHERE active = 1;

This example creates a temporary table `temp_customers` containing only active customers from the `customers` table.

2. Using Temporary Tables for Complex Joins


CREATE TEMPORARY TABLE temp_sales AS
SELECT order_id, total_amount
FROM sales
WHERE order_date >= '2023-01-01';

SELECT s.order_id, c.customer_name, s.total_amount
FROM temp_sales s
JOIN customers c ON s.customer_id = c.customer_id;

Here, `temp_sales` is used to store sales data from 2023 onwards, simplifying a subsequent join with the `customers` table to retrieve customer names and sales amounts.

3. Temporary Tables in Stored Procedures


DELIMITER //
CREATE PROCEDURE get_high_value_orders()
BEGIN
    CREATE TEMPORARY TABLE temp_orders AS
    SELECT order_id, customer_id, total_amount
    FROM orders
    WHERE total_amount > 1000;

    SELECT customer_id, SUM(total_amount) AS total_spent
    FROM temp_orders
    GROUP BY customer_id;
END //
DELIMITER ;

A stored procedure creates a temporary table `temp_orders` to aggregate high-value orders by customer.

Tips and Best Practices

  • Limit temporary table size. Keep temporary tables small by filtering data to prevent excessive memory usage, which can impact disk space and I/O performance.
  • Index temporary tables. Indexes should be defined at the time of creation, as temporary tables cannot be indexed after creation.
  • Drop temporary tables explicitly. Use `DROP TEMPORARY TABLE` to free resources if tables are no longer needed before the session ends.
  • Avoid overuse. Use temporary tables judiciously to prevent unnecessary complexity and resource consumption.
  • Session-specific scope. Temporary tables are specific to a session and cannot be referenced by different connections or sessions, which is crucial to understand to avoid confusion about their scope and lifecycle.

SQL Upskilling for Beginners

Gain the SQL skills to interact with and query your data.
Start Learning for Free