内存表 / 加速
Oracle
-- Keep the table in memory with Database In-Memory:
ALTER TABLE orders INMEMORY;
-- Memory size (init parameter):
ALTER SYSTEM SET inmemory_size = 4G SCOPE=SPFILE;Oracle Database In-Memory 将频繁查询的表以列式格式保存在内存中以加速分析(单独许可)。对于 OLTP,Buffer Cache 和 KEEP 池也会将频繁访问的块保留在内存中。内存方案通过磁盘上的行格式保持持久性。
PostgreSQL
-- Fast table without durability (writes no WAL):
CREATE UNLOGGED TABLE temp_calc (id int, data text);
-- Session-private temporary table (tends to stay in memory):
CREATE TEMP TABLE session_data (id int) ON COMMIT DELETE ROWS;PostgreSQL 没有专门的内存表引擎;UNLOGGED 表跳过 WAL 写入以加快写入速度(崩溃时内容丢失;不具备持久性)。TEMP 表在会话期间存在,并尽可能通过 temp_buffers 保存在内存中。热数据缓存在 shared_buffers 中。
MySQL
-- Table using the MEMORY (HEAP) storage engine:
CREATE TABLE cache_table (
id INT PRIMARY KEY,
data VARCHAR(100)
) ENGINE=MEMORY;MySQL MEMORY(原 HEAP)存储引擎将表数据完全保存在 RAM 中;速度非常快,但服务器重启时数据丢失(结构保留)。它不支持 BLOB/TEXT,并使用固定行长。对于需要持久化的热数据,推荐使用 InnoDB 缓冲池。
SQL Server
-- In-Memory OLTP: memory-optimized table
CREATE TABLE dbo.session_state (
id INT NOT NULL PRIMARY KEY NONCLUSTERED,
data NVARCHAR(100)
) WITH (MEMORY_OPTIMIZED = ON, DURABILITY = SCHEMA_AND_DATA);SQL Server In-Memory OLTP(Hekaton)通过内存优化表(MEMORY_OPTIMIZED=ON)和无闩锁结构提供高并发 OLTP。DURABILITY=SCHEMA_AND_DATA 保证数据持久;SCHEMA_ONLY 只保留 schema。需要内存优化文件组。
MongoDB
-- Hot data is kept in the WiredTiger cache automatically.
-- Set the cache size (default ~50% of RAM):
// mongod --wiredTigerCacheSizeGB 8
// or in the configuration: storage.wiredTiger.engineConfig.cacheSizeGBMongoDB 没有单独的内存表类型(旧的 In-Memory Storage Engine 仅限 Enterprise,且已弃用)。默认的 WiredTiger 引擎将频繁访问的数据和索引保存在内存缓存中,以提升读取速度;操作系统文件缓存也有贡献。
Redis
# Redis already runs entirely in memory; nothing extra is needed.
# Persistence and memory policy settings:
CONFIG SET maxmemory 4gb
CONFIG SET maxmemory-policy allkeys-lruRedis 将所有数据保存在 RAM 中;这正是其速度的设计来源。持久化由 RDB(快照)和/或 AOF(命令日志)提供。maxmemory 上限和淘汰策略(LRU/LFU/random)决定内存满时的行为。
Elasticsearch
-- Not applicable: there is no fully in-memory table mode.
# Place hot data on fast nodes with a hot/cold architecture:
PUT sale/_settings
{ "index.routing.allocation.require.data": "hot" }Elasticsearch 将数据存储在磁盘上(Lucene 段);没有专门的内存表引擎。速度依赖操作系统文件缓存(page cache)、doc_values 和 hot-warm-cold 架构。充足的 RAM 和 heap 设置对性能至关重要。