MySQL Temporary Tables: How They Work, Benefits, and Best Practices

目次

1. Introduction

When working with MySQL, a “temporary table” is a useful way to store and process data temporarily. By using temporary tables, you can temporarily hold data to reduce the load of complex queries and improve the efficiency of batch processing.

In this article, we will explain what MySQL temporary tables are, along with their use cases and benefits in detail.

What Is a Temporary Table?

A temporary table is a table that is valid only within a session.
Unlike regular tables, it is not stored permanently in the database, and it is automatically dropped when the session ends.

The key characteristics of temporary tables are as follows:

  • They exist per session (not accessible from other connections)
  • They are automatically dropped when the session ends
  • They can be used without interfering even if a regular table with the same name exists
  • They are often used to improve performance

Temporary tables are well-suited for data analysis and temporary data processing, and they are commonly used as support for batch processing and aggregation tasks.

Benefits of Using Temporary Tables

Using temporary tables can make data processing more efficient. Here are three major benefits.

1. Improve query performance

When handling large amounts of data, using multiple JOINs and subqueries can make processing complex and increase database load. With temporary tables, you can filter and store data in advance, speeding up query execution.

2. Ideal for temporary data storage

In batch processing or data transformation, you may need to store data temporarily and perform necessary operations. Temporary tables let you store data temporarily and enable fast in-memory processing.

3. Keep existing data safe

Directly manipulating production data is risky. By using temporary tables, you can process data without changing production data and reduce the risk of errors.

Summary

MySQL temporary tables are a convenient tool for temporary data storage and processing.

  • They are session-scoped and dropped when the session ends
  • They are useful for performance improvements and batch processing
  • They allow safe operations without changing production data

2. Temporary Table Basics

MySQL temporary tables are used to store data temporarily, unlike regular tables. In this section, we will explain the basic concepts of temporary tables in detail, including “differences from regular tables” and “differences from internal temporary tables.”

Differences Between Temporary Tables and Regular Tables

Temporary tables and regular tables differ significantly in data retention and access behavior. The table below summarizes the main differences.

ItemTemporary TableRegular Table
LifetimeDropped when the session endsExists until explicitly dropped
AccessAvailable only within the session (not visible to other connections)Shareable across all sessions
ConflictsCan be used even if a regular table with the same name existsCannot create another table with the same name
Storage locationMEMORY (default) or an InnoDB temporary areaStored in the database storage
PersistenceNone (dropped when the session ends)Yes (retained by the database)

Key points

  • Temporary tables are isolated per session and are not visible to other users.
  • You can create them without error even if a regular table with the same name exists.
  • They are created explicitly using CREATE TEMPORARY TABLE and are automatically dropped when the session ends.

Differences Between Temporary Tables and Internal Temporary Tables

In addition to user-created temporary tables, MySQL also creates internal temporary tables automatically. They may sound similar, but their purposes and management differ.

ItemTemporary TableInternal Temporary Table
Creation methodExplicitly created using CREATE TEMPORARY TABLEAutomatically created by MySQL
PurposeCreated by the user for specific processingCreated by MySQL to process complex queries (GROUP BY, ORDER BY)
ScopeAvailable only within the sessionValid only while the query is executing
DeletionDropped when the session endsAutomatically dropped after the query completes

What is an internal temporary table?

  • MySQL may internally create temporary tables to optimize certain queries (such as GROUP BY, ORDER BY, DISTINCT).
  • End users cannot manage them directly (you cannot explicitly create them like CREATE TEMPORARY TABLE).
  • They are created as needed during query execution and are automatically dropped when the query completes.

Example that may trigger internal temporary tables

When you run a query like the following, MySQL may create an internal temporary table to process it.

SELECT category, COUNT(*) 
FROM products 
GROUP BY category
ORDER BY COUNT(*) DESC;

In this case, MySQL may create an internal temporary table to temporarily store the GROUP BY results,
and then use it to compute the final output.

Summary

  • A temporary table is a user-created temporary table that is automatically dropped when the session ends.
  • Unlike regular tables, it cannot be accessed from other sessions.
  • An internal temporary table is created and dropped automatically by MySQL, and users cannot control it directly.

3. How to Create Temporary Tables

You can create a MySQL temporary table using the CREATE TEMPORARY TABLE statement. In this section, we explain everything from basic creation to creating one based on an existing table.

Basic way to create a temporary table

In MySQL, you use CREATE TEMPORARY TABLE to create a temporary table.

Basic syntax

CREATE TEMPORARY TABLE table_name (
    column_name data_type constraints,
    column_name data_type constraints,
    ...
);

Sample code

The following SQL creates a temporary table named users_temp with three columns: id (integer), name (string), and email (string).

CREATE TEMPORARY TABLE users_temp (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100)
);

Because this table is automatically dropped when the session ends, it does not affect the persistent database.

Create a temporary table based on an existing table

Instead of creating a temporary table from scratch, you can also copy an existing table structure to create one.

Using CREATE TEMPORARY TABLE ... SELECT

In MySQL, you can create a temporary table based on the result of a SELECT statement.

Basic syntax

CREATE TEMPORARY TABLE temp_table_name
SELECT * FROM existing_table_name;

Sample code

For example, to copy the data structure of the users table and create a new temporary table users_temp, you can write:

CREATE TEMPORARY TABLE users_temp
SELECT * FROM users;

With this method, the column structure of users is carried over to users_temp, but constraints such as PRIMARY KEY and INDEX are not copied.

If you want to copy only the table structure without including data, add WHERE 1=0.

CREATE TEMPORARY TABLE users_temp
SELECT * FROM users WHERE 1=0;

With this SQL, the column definitions of users are copied, but no data is included.

Notes when creating temporary tables

1. Temporary tables are session-scoped

  • A temporary table is valid only within the session in which it was created.
  • It cannot be accessed from other connections or by other users.

2. You can create it even if a regular table with the same name exists

  • For example, even if there is a regular table named users in the database, you can create a temporary table named users.
  • In that session, the temporary table takes precedence and the regular table becomes hidden.

3. Storage engine impact

  • By default, temporary tables use the MEMORY engine, but if the data size is large, they may be stored in an InnoDB temporary area.
  • If you want to explicitly specify the MEMORY engine, write it like this:
    CREATE TEMPORARY TABLE users_temp ( id INT PRIMARY KEY, name VARCHAR(50), email VARCHAR(100) ) ENGINE=MEMORY;
  • The MEMORY engine is fast but has data size limits. For large datasets, consider using InnoDB.

Summary

  • Create temporary tables using CREATE TEMPORARY TABLE.
  • You can also create one by copying an existing table (SELECT * FROM).
  • The MEMORY engine can be fast, but InnoDB is often better for large datasets.
  • Temporary tables are managed per session and are automatically dropped when the session ends.

4. How to Use Temporary Tables

MySQL temporary tables can be operated like regular tables, including INSERT, UPDATE, DELETE, and SELECT. In this section, we explain each operation in detail.

Inserting data

To add data to a temporary table, use the regular INSERT INTO statement.

Basic syntax

INSERT INTO temp_table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Sample code

The following SQL inserts data into a temporary table named users_temp.

INSERT INTO users_temp (id, name, email)
VALUES (1, 'Taro Yamada', 'taro@example.com');

You can also copy and insert data from an existing table.

INSERT INTO users_temp (id, name, email)
SELECT id, name, email FROM users WHERE age >= 18;

This SQL inserts data for users aged 18 or older from the users table into the temporary table.

Updating data

To modify data in a temporary table, use the regular UPDATE statement.

Basic syntax

UPDATE temp_table_name
SET column_name = new_value
WHERE condition;

Sample code

For example, to change the name of the user with id=1 in the users_temp table:

UPDATE users_temp
SET name = 'Ichiro Sato'
WHERE id = 1;

Deleting data

To delete unnecessary data, use the DELETE statement.

Basic syntax

DELETE FROM temp_table_name WHERE condition;

Sample code

For example, to delete the row with id=1 from users_temp:

DELETE FROM users_temp WHERE id = 1;

To delete all data in the table, omit the WHERE clause.

DELETE FROM users_temp;

Note that using DELETE does not drop the table itself; it deletes only the data.

Selecting data

To retrieve data stored in a temporary table, use the SELECT statement.

Basic syntax

SELECT column_name FROM temp_table_name WHERE condition;

Sample code

For example, to retrieve all data from users_temp:

SELECT * FROM users_temp;

To retrieve data matching a specific condition, use the WHERE clause.

SELECT * FROM users_temp WHERE email LIKE '%@example.com';

This SQL retrieves only rows where the email address contains @example.com.

Notes when using temporary tables

1. Data is removed when the session ends

  • Temporary tables are managed per session, and their data is also removed when the session ends.
  • For long-running processing, it is recommended to back up data periodically.

2. Creating a temporary table with the same name causes an error

  • If you try to create a temporary table with the same name using CREATE TEMPORARY TABLE, an error occurs.
  • As an error avoidance approach, run DROP TEMPORARY TABLE IF EXISTS beforehand.
    DROP TEMPORARY TABLE IF EXISTS users_temp; CREATE TEMPORARY TABLE users_temp (...);

3. Storage engine constraints

  • Temporary tables default to the MEMORY engine, but large datasets may be automatically stored in an InnoDB temporary area.
  • For large datasets, using an InnoDB temporary table is recommended.

Summary

  • Temporary tables can perform INSERT, UPDATE, DELETE, and SELECT just like regular tables.
  • When the session ends, data in the temporary table is also automatically removed.
  • Running DROP TEMPORARY TABLE IF EXISTS beforehand helps avoid name-conflict errors.
  • For large datasets, using an InnoDB temporary table is recommended.

5. Managing and Dropping Temporary Tables

MySQL temporary tables are automatically dropped when the session ends. However, in some cases you may need to drop them explicitly. In this section, we explain how to manage and drop temporary tables.

How to drop a temporary table

To explicitly drop a temporary table, use the DROP TEMPORARY TABLE statement.

Basic syntax

DROP TEMPORARY TABLE table_name;

Sample code

For example, to drop a temporary table named users_temp, run:

DROP TEMPORARY TABLE users_temp;

After running this SQL, the users_temp table is removed and can no longer be used in the session.

Automatic drop when the session ends

A temporary table is automatically dropped when the session ends.

How automatic drop works

  1. Create a temporary table with CREATE TEMPORARY TABLE
  2. Operate on its data while the session is active
  3. When the session (connection) is closed, the temporary table is automatically dropped

However, be careful in the following cases:

  • When sessions remain open for a long time
    → Unnecessary temporary tables may consume memory, so it is recommended to run DROP TEMPORARY TABLE as needed.
  • When handling large amounts of data
    → To avoid storage pressure, it is important to drop tables appropriately.

Using DROP TEMPORARY TABLE IF EXISTS

To avoid errors when dropping a table that may not exist, you can use IF EXISTS.

Basic syntax

DROP TEMPORARY TABLE IF EXISTS table_name;

Sample code

DROP TEMPORARY TABLE IF EXISTS users_temp;

This SQL drops users_temp if it exists; if it does not exist, it will not raise an error.

Common errors and fixes

Error 1: “Table not found”

When it happens:

  • When you try to drop a table that does not exist using DROP TEMPORARY TABLE
  • Because temporary tables are session-scoped, you cannot drop them from a different session

Fix:

  • Add IF EXISTS to avoid the error
  DROP TEMPORARY TABLE IF EXISTS users_temp;
  • Drop it within the correct session

Error 2: “Table already exists”

When it happens:

  • When you try to create a temporary table with a name that already exists

Fix:

  • Run DROP TEMPORARY TABLE IF EXISTS beforehand
  DROP TEMPORARY TABLE IF EXISTS users_temp;
  CREATE TEMPORARY TABLE users_temp (
      id INT PRIMARY KEY,
      name VARCHAR(50),
      email VARCHAR(100)
  );

Best practices for managing temporary tables

  1. Drop them explicitly when you no longer need them
  • Run DROP TEMPORARY TABLE as needed to free unnecessary tables.
  1. Use IF EXISTS to avoid errors
  • DROP TEMPORARY TABLE IF EXISTS prevents errors when dropping a table that does not exist.
  1. Be mindful of session management
  • Long-lived sessions can cause temporary tables to consume memory, so drop them as appropriate.
  1. Understand storage engine impact
  • The MEMORY engine is fast but has data size limits.
  • If you use InnoDB, you need to consider disk space usage.

Summary

  • You can explicitly drop temporary tables using DROP TEMPORARY TABLE.
  • They are automatically dropped when the session ends, but for long-lived sessions, manual cleanup is recommended.
  • DROP TEMPORARY TABLE IF EXISTS helps prevent errors when dropping.
  • It is useful to know how to handle “Table not found” and “Table already exists” errors.

6. Practical Use Cases for Temporary Tables

MySQL temporary tables are used to make temporary data storage and processing more efficient. In this section, we introduce common scenarios where temporary tables are useful and explain implementation details.

1. Using as an intermediate table for aggregation

In data analysis and report generation, processing large datasets directly can slow down query execution. By using a temporary table, you can organize data first and then process it, improving performance.

Scenario

  • The sales table contains one year of sales data.
  • You want to calculate monthly total sales and perform further analysis.

Example implementation

CREATE TEMPORARY TABLE monthly_sales (
    month_year DATE,
    total_sales DECIMAL(10,2)
);

INSERT INTO monthly_sales (month_year, total_sales)
SELECT DATE_FORMAT(sale_date, '%Y-%m-01') AS month_year, SUM(amount) 
FROM sales
GROUP BY month_year;

SELECT * FROM monthly_sales;

2. Keeping temporary data for batch processing

Temporary tables are also useful for batch processing (bulk operations). For example, you can filter data by certain conditions and store only the target data in a temporary table to operate efficiently.

Scenario

  • From the users table, you want to email only users who have logged in within the last year.
  • You store the target data in a temporary table first, then process it sequentially.

Example implementation

CREATE TEMPORARY TABLE active_users (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(255)
);

INSERT INTO active_users
SELECT id, name, email FROM users WHERE last_login >= NOW() - INTERVAL 1 YEAR;

SELECT * FROM active_users;

3. Simplifying complex queries

Running complex queries directly can reduce performance and hurt readability. By using temporary tables, you can reduce subqueries and keep SQL simpler.

Scenario

  • You want to get the top 10 best-selling products from the orders table.
  • You want to avoid using subqueries by leveraging a temporary table.

Example implementation

CREATE TEMPORARY TABLE top_products AS
SELECT product_id, SUM(amount) AS total_sales
FROM orders
GROUP BY product_id
ORDER BY total_sales DESC
LIMIT 10;

SELECT * FROM top_products;

4. Temporary operations without needing rollback

Temporary tables are managed per session and are not affected by transactions. This makes them suitable for managing temporary data where rollback is not desired.

Scenario

  • During a transaction, you want to keep temporary calculation results.
  • But you want to avoid temporary data being rolled back on errors.

Example implementation

START TRANSACTION;

CREATE TEMPORARY TABLE temp_results (
    user_id INT,
    score INT
);

INSERT INTO temp_results
SELECT user_id, SUM(points) FROM game_scores GROUP BY user_id;

-- Commit the transaction
COMMIT;

SELECT * FROM temp_results;

Summary

  • Temporary tables can be used for aggregation, batch processing, and simplifying queries across many scenarios.
  • Using them as an intermediate table can improve performance and help organize data.
  • For batch processing, pre-extracting only target data helps avoid unnecessary work.
  • For simplifying complex queries, reducing subqueries improves readability.
  • Because they are not affected by transactions, they can be used for temporary data where rollback is not needed.

7. Alternatives and Limitations of Temporary Tables

MySQL temporary tables are useful, but they have some limitations. In some cases, using alternatives such as views or subqueries can provide more efficient data processing. In this section, we explain the main limitations of temporary tables and alternative approaches to work around them.

Main limitations of temporary tables

Temporary tables have several limitations that regular tables do not. Understanding these helps you choose appropriate use cases.

1. Session-scoped

  • A temporary table is valid only within the session where it was created, and cannot be accessed by other connections or users.
  • Even if a regular table with the same name exists, the temporary table takes precedence within the session (the regular table is not accessible).

2. The schema is not retained

  • Regular tables can be inspected with SHOW CREATE TABLE, but a temporary table disappears when the session ends, so its schema is not retained.

3. Index limitations

  • If you do not specify PRIMARY KEY or INDEX in CREATE TEMPORARY TABLE, they are not created automatically.
  • If you need indexes on a temporary table, you must create them manually.

4. Default storage engine is MEMORY

  • With the MEMORY engine, large data sizes may cause swapping to disk and reduce performance.
  • If you specify InnoDB, it can handle larger data, but disk usage increases.

5. Not affected by transactions

  • Temporary tables are not affected by ROLLBACK.
  • Therefore, they are not suitable for processing that requires strict transactional consistency.

Alternatives to temporary tables

To avoid these limitations, you can use views or subqueries instead of temporary tables for more flexible data handling.

1. Use a view

A view can be used similarly to temporary tables for temporary data referencing. A view acts as a virtual table and does not require temporary data storage, which helps avoid storage constraints.

Create a view

CREATE VIEW active_users AS
SELECT id, name, email FROM users WHERE last_login >= NOW() - INTERVAL 1 YEAR;

Use the view

SELECT * FROM active_users;
Pros of using a view

No storage usage (data is referenced directly, no need to store it temporarily)
Not session-dependent (available to other users and connections)
Schema can be retained (you can review the definition with SHOW CREATE VIEW)

Cons of using a view

Hard to update (direct INSERT or UPDATE on a view is restricted)
Performance may degrade for large datasets

2. Use a subquery

You can also use a subquery to process temporary data without creating a temporary table.

Using a temporary table

CREATE TEMPORARY TABLE top_products AS
SELECT product_id, SUM(amount) AS total_sales
FROM orders
GROUP BY product_id
ORDER BY total_sales DESC
LIMIT 10;

SELECT * FROM top_products;

Using a subquery

SELECT product_id, SUM(amount) AS total_sales
FROM orders
GROUP BY product_id
ORDER BY total_sales DESC
LIMIT 10;
Pros of using a subquery

Better performance because you do not create a temporary table
No storage usage
Not session-dependent and can be executed anytime

Cons of using a subquery

Readability can suffer for complex queries
Hard to reuse data (you may need to reference the same data repeatedly)

3. Use a CTE (WITH clause)

In MySQL 8.0 and later, you can use a CTE (Common Table Expression) to handle data temporarily without creating a temporary table.

CTE example

WITH top_products AS (
    SELECT product_id, SUM(amount) AS total_sales
    FROM orders
    GROUP BY product_id
    ORDER BY total_sales DESC
    LIMIT 10
)
SELECT * FROM top_products;
Pros of using a CTE

Improves readability (often easier to read than subqueries)
Can optimize performance (temporary-style processing without creating a temporary table)

Cons of using a CTE

Not available in MySQL 5.x (supported only in MySQL 8.0 and later)

Summary

MethodProsCons
Temporary tableGood for session-scoped data processingConsumes storage and disappears when the session ends
ViewNo storage usage, not session-dependentHard to update, possible performance degradation
SubqueryNo storage usage, simpleHard to reuse, reduced readability
CTE (WITH)Better readability, performance optimizationAvailable only in MySQL 8.0+

8. FAQ

Here are frequently asked questions about MySQL temporary tables. We hope this helps clarify how they behave and what limitations they have.

1. Can temporary tables be referenced from other sessions?

No, they cannot.
A temporary table is available only within the session where it was created. Other sessions cannot access it. Even if another user creates a temporary table with the same name, each session treats it as an independent table.

2. What privileges are required to create temporary tables?

To create temporary tables, you need the CREATE TEMPORARY TABLES privilege on the database.
To grant the privilege to a user, run the following SQL:

GRANT CREATE TEMPORARY TABLES ON database_name.* TO 'user_name'@'host';

You can also check the current privileges using SHOW GRANTS.

SHOW GRANTS FOR 'user_name'@'host';

3. Do temporary tables affect disk usage?

Yes, they can.
By default, MySQL temporary tables use the MEMORY engine, but when the data size exceeds a certain threshold, they are stored in an InnoDB temporary area.

When dealing with large datasets, temporary tables may consume disk space. Therefore, it is recommended to drop them explicitly when they are no longer needed.

DROP TEMPORARY TABLE IF EXISTS table_name;

To minimize disk impact, if you expect large data volumes, consider creating the temporary table with InnoDB instead of MEMORY.

CREATE TEMPORARY TABLE table_name (
    column1 data_type,
    column2 data_type
) ENGINE=InnoDB;

4. What is the difference between a temporary table and an internal temporary table?

ItemTemporary tableInternal temporary table
Creation methodCreated by the user with CREATE TEMPORARY TABLEAutomatically created by MySQL during processing such as GROUP BY
ScopeOnly within the creating sessionOnly during query execution
DeletionExplicitly dropped with DROP TEMPORARY TABLEAutomatically dropped when the query completes

5. Can temporary tables be shared across threads?

No, they cannot.
A temporary table is valid only within the thread (session) where it was created, and it cannot be accessed from other threads or processes.

If you need to share data across sessions/threads, you must create a regular table instead.

CREATE TABLE shared_temp_table (
    id INT PRIMARY KEY,
    data VARCHAR(255)
);

6. Can temporary tables reduce performance?

Yes, in some cases.
In particular, be careful in the following situations:

  • When the data volume is too large
  • The MEMORY engine has size limits; beyond that, data may swap to InnoDB, which can reduce performance.
  • Mitigation: If you expect to exceed MEMORY limits, create the table with InnoDB from the start.
  • When appropriate indexes are not set
  • Tables created with CREATE TEMPORARY TABLE ... SELECT do not copy indexes, so searches can become slower.
  • Mitigation: Add indexes as needed using ALTER TABLE.
  ALTER TABLE temp_table_name ADD INDEX (column_name);

7. How can I improve temporary table performance?

To improve temporary table performance, the following approaches are effective:

Use the MEMORY engine (fast for small datasets)

CREATE TEMPORARY TABLE table_name (
    id INT PRIMARY KEY,
    name VARCHAR(50)
) ENGINE=MEMORY;

Select only required columns (omit unnecessary columns)

CREATE TEMPORARY TABLE users_temp AS
SELECT id, name FROM users;

Add appropriate indexes (speed up searches)

ALTER TABLE users_temp ADD INDEX (name);

Drop it as soon as you no longer need it (free memory)

DROP TEMPORARY TABLE IF EXISTS users_temp;

Summary

  • Temporary tables cannot be referenced from other sessions or threads
  • You need the CREATE TEMPORARY TABLES privilege to create them
  • If the data becomes too large, MySQL may switch from MEMORY to InnoDB, which can reduce performance
  • Adding proper indexes can speed up queries
  • Dropping temporary tables with DROP TEMPORARY TABLE is recommended when they are no longer needed

This completes a detailed explanation of MySQL temporary tables, from basic concepts to use cases, limitations, alternatives, and FAQs.
By using temporary tables appropriately, you can significantly improve the efficiency of data processing.