Skip to content

MySQL临时表

概述

临时表是仅对当前连接可见的特殊表,连接关闭时会自动删除。它们对于中间结果、临时存储和数据处理很有用。

临时表特征

连接特定:仅对创建连接可见 自动清理:连接结束时删除 隔离:对其他连接不可见 允许同名:不同连接可以有同名的临时表

创建临时表

基本语法

sql
-- Create temporary table

-- Create with SELECT

-- Create with LIKE

创建带数据的临时表

sql
-- Create and insert in one statement

-- Create empty table, then insert

从另一个表创建临时表

sql
-- Copy structure and data

-- Copy with filter

-- Copy specific columns

临时表操作

INSERT操作

sql
-- Insert into temporary table

-- Insert from another table

-- Bulk insert

UPDATE操作

sql
-- Update temporary table

-- Update with JOIN

DELETE操作

sql
-- Delete from temporary table

-- Delete with condition

-- Truncate temporary table

临时表查询

基本查询

sql
-- Select from temporary table

-- Join with regular tables

-- Aggregate data

复杂查询

sql
-- Join multiple temporary tables

-- Analyze customer data

临时表索引

添加索引

sql
-- Create indexed temporary table

-- Add index to existing temporary table

-- Add composite index

索引性能

sql
-- Indexes improve query performance

-- Query uses index

临时表存储

存储引擎

sql
-- Specify engine for temporary table

-- Use MEMORY engine for fast access

-- Check engine

内存临时表

sql
-- MEMORY engine for temporary storage

-- Fast lookups

-- Limitations of MEMORY engine
-- No TEXT/BLOB types
-- Fixed row size
-- Data lost on server restart

临时表生命周期

连接特定

sql
-- Connection 1

-- Connection 2

自动清理

sql
-- Temporary tables automatically dropped when:
-- 1. Connection closes normally
-- 2. Connection is killed
-- 3. Explicit DROP TABLE
-- 4. Server restarts

-- Manual drop (optional)

-- Check if table is temporary
-- TEMPORARY tables show as 'TEMPORARY'

使用场景

数据处理

sql
-- Multi-step data transformation
-- Step 1: Extract data

-- Step 2: Clean and transform

-- Step 3: Load into production table

-- Step 4: Cleanup (automatic on disconnect)

中间结果

sql
-- Complex query broken into steps
-- Step 1: Aggregate orders

-- Step 2: Calculate averages

-- Step 3: Final result

数据比较

sql
-- Compare two data sets

-- Find new records

-- Find changed records

会话特定数据

sql
-- User session data

-- Insert session data

-- Update session activity

性能优化

sql
-- Temporary table for caching

-- Populate cache

-- Fast lookups

临时表限制

限制

sql
-- Cannot use certain features
-- No foreign keys (in older MySQL versions)
-- Cannot be referenced by views (in older versions)
-- No TRIGGERS on temporary tables

-- Check temporary table limitations

最佳实践

sql
-- 1. Use descriptive names

-- 2. Clean up explicitly when done

-- 3. Use appropriate engine
-- MEMORY for small, frequently accessed data
-- InnoDB for larger datasets

-- 4. Add indexes for performance

存储过程

使用临时表

sql
    -- Create temporary table
    
    -- Populate with data
    
    -- Return results
    
    -- Temporary table automatically dropped when procedure ends

-- Call procedure

监控临时表

检查临时表

sql
-- View temporary tables in current database

-- View all temporary tables across all databases

-- Check temporary table size

小结

临时表适用于:

中间处理:多步骤数据转换 会话数据:连接特定存储 性能:缓存和优化 隔离:与生产数据分离 自动清理:无需手动维护

将临时表用于不需要在当前连接之外持续存在的临时数据处理任务。


上一个:索引

下一个:复制表