使用高级选项收集统计信息(直方图等)
Oracle
EXEC DBMS_STATS.GATHER_TABLE_STATS(
ownname=>'HR', tabname=>'EMPLOYEE',
estimate_percent=>DBMS_STATS.AUTO_SAMPLE_SIZE,
method_opt=>'FOR ALL COLUMNS SIZE AUTO', degree=>4);method_opt 控制直方图,degree 控制并行度。使用 LOCK_TABLE_STATS 锁定统计信息。
PostgreSQL
ALTER TABLE customer ALTER COLUMN city SET STATISTICS 500;
ANALYZE customer;按列设置采样目标;default_statistics_target 设置全局默认值。
MySQL
ANALYZE TABLE customer UPDATE HISTOGRAM ON city WITH 32 BUCKETS;MySQL 8.0 支持直方图;适用于用作过滤条件但选择性不高的列。
SQL Server
UPDATE STATISTICS dbo.customer WITH FULLSCAN;
-- Filtered statistics: CREATE STATISTICS ... WHERE ...FULLSCAN 最准确;大表上使用 SAMPLE。自动更新使用 AUTO_UPDATE_STATISTICS。
MongoDB
// There are no manual statistics; the planner measures at runtime.
db.customer.getPlanCache().clear()除了清除过时的计划缓存外,无需手动维护统计信息。
Redis
# Not applicable.没有统计信息/优化器。
Elasticsearch
# There is no statistics gathering; refresh searchability:
POST <index>/_refresh查询时使用 field data 和 doc values;force merge 可减少段数量。