DBAtlas · 数据库运维图谱

固定 / 强制查询计划(plan baseline / hint)

Oracle

-- Load the good plan from the cursor cache as a baseline:
DECLARE n NUMBER; BEGIN
  n := DBMS_SPM.LOAD_PLANS_FROM_CURSOR_CACHE(sql_id => '&sql_id');
END;
/

SQL Plan Management 锁定执行计划;是防止计划回归的最强手段。也可以使用 /*+ ... */ hint。

PostgreSQL

-- There is no built-in plan pinning; the pg_hint_plan extension:
/*+ IndexScan(customer customer_city_idx) */ SELECT ... ;

替代方案:通过 enable_seqscan 等设置在会话级进行引导;持久固定使用 pg_hint_plan。

MySQL

SELECT /*+ INDEX(customer customer_city_idx) */ * FROM customer WHERE city='Ankara';

优化器 hint 嵌入在查询中。持久改写使用 query rewrite 插件。

SQL Server

-- Force the good plan in Query Store:
EXEC sp_query_store_force_plan @query_id = 42, @plan_id = 17;

必须启用 Query Store。也可以使用 OPTION (USE PLAN N'...') 或计划指南(plan guide)。

MongoDB

// Force an index for the query:
db.customer.find({ city: "Ankara" }).hint({ city: 1 })
// Persistent: db.customer.getPlanCache() / index filter

hint 强制使用特定索引;使用 planCacheClear 清除过时的计划。

Redis

# Not applicable: there is no query planner.

性能取决于数据结构和命令复杂度。

Elasticsearch

# There is no plan pinning; optimize the query/index and routing.
GET <index>/_search { "profile": true, "query": {...} }

使用 profile 查看哪个查询开销大;通过分片/routing 进行引导。

在 DBAtlas 中打开 →