DBAtlas · 数据库运维图谱

执行并行查询 / 设置并行度

Oracle

-- At query level with a hint:
SELECT /*+ PARALLEL(t, 4) */ * FROM big_table t;
-- Force at session level:
ALTER SESSION FORCE PARALLEL QUERY PARALLEL 4;

PARALLEL hint 对表执行并行扫描;degree=DOP。过高的 DOP 会耗尽服务器资源,需谨慎选择。parallel_degree_policy=AUTO 可自动管理。

PostgreSQL

-- Set the number of parallel workers for the session:
SET max_parallel_workers_per_gather = 4;
-- The planner adds a Gather/Gather Merge node:
EXPLAIN (ANALYZE) SELECT count(*) FROM big_table;

如果执行计划中出现 Gather/Parallel Seq Scan,说明并行已生效。max_parallel_workers 和 min_parallel_table_scan_size 也起作用。

MySQL

-- Limited InnoDB parallelism (certain operations only):
SET GLOBAL innodb_parallel_read_threads = 4;
-- Effective for COUNT(*) and primary key scans.

MySQL 不提供通用的并行查询执行;innodb_parallel_read_threads 只能并行化聚集索引扫描(如 COUNT(*))。复杂的 JOIN 是单线程的。

SQL Server

-- MAXDOP hint at query level:
SELECT * FROM dbo.big_table OPTION (MAXDOP 4);
-- Instance level: sp_configure 'max degree of parallelism', 4;

MAXDOP 限制并行线程数;MAXDOP 1 会禁用并行。超过“cost threshold for parallelism”后,执行计划将变为并行。

MongoDB

// Allow disk use for a large aggregation:
db.sale.aggregate([ { $group: { _id: "$city", t: { $sum: "$amount" } } } ], { allowDiskUse: true })
// A sharded cluster processes each shard in parallel.

在分片集群中,查询在各分片上并行运行;在单节点上,聚合阶段大多是串行的。allowDiskUse 允许执行超出内存限制的操作。

Redis

# Not applicable: there is no concept of query parallelism.

命令执行是单线程的;可通过 Cluster(分片)扩展,或使用 io-threads 实现网络 I/O 并行。

Elasticsearch

# Shard count = unit of parallelism; a query runs in parallel across shards.
# Parallelism by slicing within a single shard (with scroll/PIT):
GET <index>/_search
{ "slice": { "id": 0, "max": 4 }, "query": { "match_all": {} } }

搜索在所有相关分片上并发运行;主分片数量决定基础并行度。slices 可拆分大型 scroll/reindex 操作。

在 DBAtlas 中打开 →