Temporary tables are one of the most useful yet underutilized features in MySQL. The SELECT INTO shortcut makes it incredibly easy to instantiate a temp table and load data in one shot. But temporary tables can do much more than stash intermediate results or subsets of data.

When leveraged effectively, temp tables provide versatility for data transformations, enable faster analytic queries, and facilitate all sorts of database operations. In this comprehensive guide, we‘ll unpack when and how to use temporary tables in MySQL along with best practices picked up over years of development work.

What Makes Temporary Tables Useful?

Some key advantages temporary tables have over normal base tables:

Performance

  • Data stored in memory for faster access, especially for small lookup tables
  • Avoid expensive operations like filesort when joining with temp table
  • Can leverage memory engine features like hash indexes

Isolation

  • Experiment/test things without impacting real data
  • Work with subset of data from different sources
  • Manipulate data with abandon – truncate/alter temp structure freely

Caching

  • Store results of complex queries or remote calls for low latency retrieval
  • Refresh periodically to keep cache table up to date

Scratch Space

  • Use for transient data like sessions, shopping carts, temporary user content
  • Serve as intermediate landing pad before loading into analytics tables

With creativity they can support many other use cases like providing datasets for machine learning jobs.

Creating and Populating Temporary Tables

We already covered the SELECT INTO syntax for instantiating and loading temp tables in one shot. Here are a few other handy ways to create and populate temporary tables:

Copy a Base Table‘s Structure

CREATE TEMPORARY TABLE temp_orders 
AS (SELECT * FROM orders LIMIT 0);

Creates an empty temp table with identical schema to the base orders table by selecting 0 records.

Generate with Query Results

CREATE TEMPORARY TABLE temp_top_products
SELECT * FROM products 
ORDER BY unit_sales DESC 
LIMIT 10;

Creates a temp table populated with top 10 products by sales.

Convert View to Temporary Table

CREATE TEMPORARY TABLE temp_customers
AS (SELECT * FROM v_active_customers); 

Populates temp table from view containing subset of customers.

Import CSV Data

CREATE TEMPORARY TABLE temp_inventory (
  product_id INT,
  qty INT  
)
LOAD DATA INFILE ‘/data/inventory.csv‘ 
INTO TABLE temp_inventory;

Loads CSV file into new temp table in one shot.

This diversity of creation approaches enables flexibility in how temp tables are instantiated in your environment.

Temporary Table Performance Factors

Understanding how temporary tables relate to storage engines and memory allocation helps unlock maximum performance.

By default, temp tables use the memory storage engine which stores row data in memory rather than data files on disk. This delivers optimal speed for lookup tables that fit in memory.

But what happens when tables exceed memory capacity?

Spilling to Disk

If a memory temp table surpasses allocated memory, additional data spills to disk into the temptable_max_size variable space by default:

+-----------------+
| In-Memory Data  | 
+--------------------------------------------+
| Data Spilled to Disk (temptable_max_size) |
+-----------------+  

So performance degrades but operations won‘t fail.

Alternative Storage Engines

Alternatively you can define a persistent storage engine upfront:

CREATE TEMPORARY TABLE temp_big (
  id INT PRIMARY KEY, 
  data VARCHAR(100)  
) ENGINE=InnoDB;

This forces the temp table to use InnoDB on disk rather than memory – better for predictability with big datasets.

Memory Configuration

Make sure your tmp_table_size and max_heap_table_size variables align with realities of your temp tables‘ memory requirements.

Testing with production-esque data at scale is key here.

Indexes and Partitions

Strategically applying indexes, partitions, and other optimizations compatible with the temp table use case can further tune performance.

Persisting Temporary Table Data

A tradeoff with temporary tables is data gets deleted after the database session. What if you need the data to live longer?

A couple options…

Save Define Statements

Save the SQL statements that define and populate the temporary table:

CREATE TABLE mytemptable AS...

INSERT INTO mytemptable ...

Then rerun after login to recreate.

Permanent Table Conversion

Convert the temp table to a permanent table with CREATE TABLE AS:

CREATE TABLE realtable AS
SELECT * FROM tmptable;

And optionally drop the temp table after.

These patterns allow persisting data outside a single database session when required.

Temporary Tables vs. Derived Tables

Derived tables are similar concept as temporary tables – so what‘s the difference?

Derived Tables

  • Instant virtual tables from a query
  • Only exist during query execution
  • Useful for simplifying complex joins and filtering
SELECT *
FROM
  (SELECT id, name 
   FROM products
   WHERE discontinued = 1) AS discontinued_products;

Temporary Tables

  • Physically created tables with data persistence
  • Exist for the session until dropped
  • Store intermediate results for additional processing
CREATE TEMPORARY TABLE discounted_products
  SELECT id, name  
  FROM products
  WHERE discount_percentage > 30;

SELECT * FROM discounted_products; 

In summary – derived tables help simplify single query complexity while temporary tables are reusable for querying multiple times during a session.

Managing Large Temporary Tables

What about when you‘re working with temp tables containing millions or even billions of rows?

Here are some tips for handling big temp tables:

  • Monitor Memory Usage – keep an eye on overall memory consumption and tmp table memory allocators.

  • Tune Memory Settings – increase settings like tmp_table_size and max_heap_table_size if needed.

  • Partition Tables – for very large temp tables partition on date or other appropriate columns.

  • Persist to Disk – use InnoDB or other on-disk engine for better stability.

  • Index Columns – strategically apply indexes aligned to query patterns.

  • Drop When Done – delete large temp tables manually via DROP TABLE when finished using.

With proper planning, even enormous temporary tables can be processed efficiently.

Use Cases Showcasing Value

Beyond basic data transfers and transformations, here are some advanced ways to employ temporary tables:

Session Data

Store shopping cart items selected by each user in a temporary table, then process or move to orders table on checkout.

Job Workspaces

Data engineers can leverage large temp tables for data wrangling pipelines.

Caching

Temporarily cache responses from costly database queries or external service calls.

Machine Learning

Engineer features in temporary tables and join against test data during model development.

There are many more possibilities – hopefully these ideas spark inspiration!

Wrapping Up

While often overlooked, temporary tables unlock extremely useful capabilities ranging from better session handling to enabling performant analytic queries. By mastering temp table creation, storage configuration, and retention approaches – developers gain a versatile tool for streamlining MySQL database applications.

Remember – creativity is key! There are always new ways to employ temporary tables to simplify workflows and optimize complex data transformations.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *