我的 Amazon DocumentDB(兼容 MongoDB)数据库集群需要很长时间才能创建索引。或者,我尝试在 Amazon DocumentDB 集群上创建索引时收到错误。
解决方法
Amazon DocumentDB 允许您一次只能为每个集合创建一个索引。但是,您可以使用并行索引更新将数据同时加载到多个集合中。最佳做法是先创建索引,然后再加载数据。
Amazon DocumentDB 支持进行后台索引创建和前台索引创建。
监控索引进度状态
索引创建需要经历多个阶段,包括初始化、扫描集合、排序键和插入键。
要监控索引进度状态,请运行以下查询:
db.currentOp({
"command.createIndexes": {
$exists: true
}
})
前面的命令会输出每个阶段的完成百分比、扫描的数据块以及排序键或插入键。当最后一个阶段的完成率达到 100% 时,表明 Amazon DocumentDB 已成功创建了所有索引。如果使用 Amazon DocumentDB 5.0 及更高版本,您可以监控索引进度状态。有关详细信息,请参阅 Managing Amazon DocumentDB indexes(管理 Amazon DocumentDB 索引)。
解决“Command failed with error 14031: 'Not enough disk space' on server”错误
Amazon Document 使用数据库实例可用的 FreeLocalStorage,根据主要资源的实例类创建索引。所需的存储大小取决于您创建的索引的大小。如果 FreeLocalStorage 值在索引创建过程中降低,索引创建速度可能会减慢,并且您可能会收到以下错误消息:
“Command failed with error 14031: 'Not enough disk space' on server.”
要监控可用存储空间,请为 FreeLocalStorage 指标设置 Amazon CloudWatch 警报。如果您想在大型集合上创建索引,最佳做法是在创建索引之前纵向扩展实例类。然后,在索引创建完成后缩减。
解决“Existing index build in progress on the same collection.Collection is limited to a single index build at a time”错误
Amazon DocumentDB 支持一次只能为每个集合创建一个后台索引。例如,如果您尝试在一个集合上同时运行多项 DDL 操作(例如 createIndex() 或 dropIndex(),则可能会收到以下错误消息:
“Existing index build in progress on the same collection.Collection is limited to a single index build at a time.”
为防止出现上述错误,一次只能为每个集合运行一项后台索引创建操作。
要查看正在进行的索引创建操作,请运行以下查询:
db.currentOp({
"command.createIndexes": {
$exists : true
}
})
解决“Key too large to index”错误
如果您创建的索引键超过 2,048 个字节,索引创建将失败,并出现“key too large to index”错误。有关键大小限制的详细信息,请参阅 Amazon DocumentDB quotas and limits(Amazon DocumentDB 配额和限制)。
解决“createIndex error: namespace name generated from index name is too long”错误
当您尝试从 MongoDB 迁移到 Amazon DocumentDB 时,如果索引名称超过 255 个字符,索引创建将失败。然后,您会收到以下错误消息:
“createIndex error: namespace name generated from index name is too long.”
要解决此错误,请查看 Amazon DocumentDB 命名限制。
解决由节流引起的问题
数据库实例出现资源节流会导致索引操作失败。在创建索引之前,请确保 CPU、内存、DBConnections 和 FreeLocalStorage 未出现节流。在创建索引之前,使用 CloudWatch 图表和 Performance Insights 来验证您的实例负载。
解决由长时间运行的交易引起的问题
Amazon DocumentDB 集群中长时间运行的交易会导致索引创建延迟或失败。
要查看哪些交易当前正在进行中,请运行以下查询:
db.currentOp({
"active": true,
$and: [
{
op: "command",
"command.createIndexes": {
$exists: true
}
}
]
})
解决“createIndex error: Field 'weights' is currently not supported”错误
当您从包含文本类型索引的 MongoDB 恢复数据时,索引创建会因以下错误而失败:
“createIndex error: Field 'weights' is currently not supported.”
版本低于 5.0 的 Amazon DocumentDB 不支持此文本索引类型。要解决此问题,请在源服务器中删除文本索引或将索引更改为支持的类型。然后,创建新的数据转储并将新的转储恢复到 Amazon DocumentDB。
解决“unsupported_index_options”错误
您可以使用 Amazon DocumentDB 3.6 及更高版本创建 2dsphere,使用 Amazon DocumentDB 5 及更高版本创建部分索引。当您尝试在不支持的版本上创建部分索引或 2dsphere 时,您会收到以下错误:
“unsupported_index_options": [ "partialFilterExpression" ] or ["2dsphereIndexVersion"]”
为避免因不支持的索引类型导致错误,请将索引格式更改为支持的类型,然后重新启动索引创建。
相关信息
Troubleshooting indexes(对索引进行故障排除)
How to index on Amazon DocumentDB (with MongoDB compatibility)(如何在 Amazon DocumentDB(兼容 MongoDB)上编制索引)