On this page本页内容
Index builds can impact sharded cluster performance. 索引构建可能会影响分片集群的性能。By default, MongoDB 4.4 and later build indexes simultaneously on all data-bearing replica set members. 默认情况下,MongoDB 4.4及更高版本会在所有包含数据的副本集成员上同时构建索引。Index builds on sharded clusters occur only on those shards which contain data for the collection being indexed. 分片集群上的索引构建只发生在包含被索引集合的数据的那些分片上。For workloads which cannot tolerate performance decrease due to index builds, consider using the following procedure to build indexes in a rolling fashion.对于无法容忍因索引生成而导致性能下降的工作负载,请考虑使用以下过程以滚动方式构建索引。
Rolling index builds take at most one shard replica set member out at a time, starting with the secondary members, and builds the index on that member as a standalone. 滚动索引生成一次最多提取一个碎片副本集成员,从次要成员开始,并将索引作为独立成员构建在该成员上。Rolling index builds require at least one replica set election per shard.滚动索引生成要求每个碎片至少选择一个副本集。
To create unique indexes using the following procedure, you must stop all writes to the collection during this procedure.要使用以下过程创建唯一索引,必须在此过程中停止对集合的所有写入。
If you cannot stop all writes to the collection during this procedure, do not use the procedure on this page. 如果在此过程中无法停止对集合的所有写入,请不要使用此页面上的过程。Instead, build your unique index on the collection by issuing 相反,通过在db.collection.createIndex() on the mongos for a sharded cluster.mongos上为分片集群发出db.collection.createIndex()命令,在集合上构建您的唯一索引。
Ensure that your oplog is large enough to permit the indexing or re-indexing operation to complete without falling too far behind to catch up. 确保您的oplog足够大,以允许索引或重新索引操作完成,而不会落后太多。See the oplog sizing documentation for additional information.有关更多信息,请参阅oplog大小调整文档。
Warning
If you cannot stop all writes to the collection, do not use the following procedure to create unique indexes.如果无法停止对集合的所有写入,请不要使用以下过程创建唯一索引。
db.collection.dropIndex() from a mongos to drop the index from the collection.mongos发出db.collection.dropIndex()命令,从集合中删除索引。Important重要的
The following procedure to build indexes in a rolling fashion applies to sharded clusters deployments, and not replica set deployments. 以下以滚动方式构建索引的过程适用于分片集群部署,而不是副本集部署。For the procedure for replica sets, see Rolling Index Builds on Replica Sets instead.有关副本集的过程,请参阅在副本集上滚动索引构建。
Connect a 将mongo shell to a mongos instance in the sharded cluster, and run sh.stopBalancer() to disable the balancer: mongo shell连接到分片集群中的mongos实例,然后运行sh.stopBalancer()禁用平衡器:[1]
Note
If a migration is in progress, the system will complete the in-progress migration before stopping the balancer.如果正在进行迁移,系统将在停止平衡器之前完成正在进行的迁移。
To verify that the balancer is disabled, run 要验证平衡器是否已禁用,请运行sh.getBalancerState(), which returns false if the balancer is disabled:sh.getBalancerState(),如果平衡器已禁用,则返回false:
| [1] | sh.stopBalancer() also disables auto-splitting for the sharded cluster.sh.stopBalancer()还禁用分片集群的自动拆分。 |
From the 从mongo shell connected to the mongos, refresh the cached routing table for that mongos to avoid returning stale distribution information for the collection. mongoShell连接到mongos,刷新该mongos的缓存路由表,以避免返回集合的过时分发信息。Once refreshed, run 刷新后,对要生成索引的集合运行db.collection.getShardDistribution() for the collection you wish to build the index.db.collection.getShardDistribution()。
For example, if you want to an ascending index on the 例如,如果要在records collection in the test database:test数据库中的records集合上创建升序索引:
The method outputs the shard distribution. 该方法输出碎片分布。For example, consider a sharded cluster with 3 shards 例如,考虑使用shardA, shardB, and shardC and the db.collection.getShardDistribution() returns the following:shardA、shardB和shardC的3个分片集群和db.collection.getShardDistribution()返回以下内容:
From the output, you only build the indexes for 从输出中,您只为test.records on shardA and shardC.shardA和shardC上的test.records构建索引。
For each shard that contains chunks for the collection, follow the procedure to build the index on the shard.对于包含集合块的每个碎片,请按照以下步骤在碎片上构建索引。
For an affected shard, stop the 对于受影响的碎片,请停止与其次要碎片之一关联的mongod process associated with one of its secondary. mongod进程。Restart after making the following configuration updates:进行以下配置更新后重新启动:
If you are using a configuration file, make the following configuration updates:如果使用的是配置文件,请进行以下配置更新:
net.port to a different port. net.port更改为其他端口。[2] replication.replSetName option.replication.replSetName选项。sharding.clusterRole option.sharding.clusterRole选项。skipShardingConfigurationChecks (also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to true in the setParameter section.setParameter部分,将参数skipShardingConfigurationChecks(也可用于MongoDB 3.6.3+、3.4.11+、3.2.19+)设置为true。disableLogicalSessionCacheRefresh to true in the setParameter section.setParameter部分中,将参数disableLogicalSessionCacheRefresh设置为true。For example, for a shard replica set member, the updated configuration file will include content like the following example:例如,对于碎片副本集成员,更新后的配置文件将包括如下示例所示的内容:
And restart:然后重新启动:
Other settings (e.g. 其他设置(如storage.dbPath, etc.) remain the same.storage.dbPath等)保持不变。
If using command-line options, make the following configuration updates:如果使用命令行选项,请进行以下配置更新:
--port to a different port. --port更改为其他端口。[2]--replSet.--shardsvr if a shard member and --configsvr if a config server member.skipShardingConfigurationChecks (also available for MongoDB 3.6.3+, 3.4.11+, 3.2.19+) to true in the --setParameter option.disableLogicalSessionCacheRefresh to true in the --setParameter option.For example, restart your shard replica set member without the --replSet and --shardsvr options. Specify a new port number and set both the skipShardingConfigurationChecks and disableLogicalSessionCacheRefresh parameters to true:
Other settings (e.g. 其他设置(例如--dbpath, etc.) remain the same.--dbpath等)保持不变。
| [2] | (1, 2) mongod on a different port, you ensure that the other members of the replica set and all clients will not contact the member while you are building the index.mongod,可以确保副本集的其他成员和所有客户端在构建索引时不会联系该成员。 |
Connect directly to the 直接连接到新端口上作为独立运行的mongod instance running as a standalone on the new port and create the new index for this instance.mongod实例,并为此实例创建新索引。
For example, connect a 例如,将mongo shell to the instance, and use the db.collection.createIndex() method to create an ascending index on the username field of the records collection:mongo shell连接到实例,并使用db.collection.createIndex()方法在records集合的username字段上创建升序索引:
mongod as a Replica Set Membermongod¶When the index build completes, shutdown the 索引构建完成后,关闭mongod instance. mongod实例。Undo the configuration changes made when starting as a standalone to return to its original configuration and restart.撤消作为独立设备启动时所做的配置更改,以返回其原始配置并重新启动。
Important重要的
Be sure to remove the 确保删除skipShardingConfigurationChecks parameter and disableLogicalSessionCacheRefresh parameter.skipShardingConfigurationChecks参数和disableLogicalSessionCacheRefresh参数。
For example, to restart your replica set shard member:例如,要重新启动副本集碎片成员:
If you are using a configuration file:如果您使用的是配置文件:
replication.replSetName.replication.replSetName的注释。sharding.clusterRole.sharding.clusterRole的注释。skipShardingConfigurationChecks in the setParameter section.setParameter部分中的参数skipShardingConfigurationChecks。disableLogicalSessionCacheRefresh in the setParameter section.setParameter部分中的参数disableLogicalSessionCacheRefresh。Other settings (e.g. 其他设置(如storage.dbPath, etc.) remain the same.storage.dbPath等)保持不变。
And restart:然后重新启动:
If you are using command-line options:如果使用的是命令行选项:
--replSet.--replSet。--shardsvr if a shard member or --configsvr if a config server member.--shardsvr;如果是配置服务器成员,则包括--configsvr。skipShardingConfigurationChecks.skipShardingConfigurationChecks。disableLogicalSessionCacheRefresh.disableLogicalSessionCacheRefresh。For example:例如:
Other settings (e.g. 其他设置(例如--dbpath, etc.) remain the same.--dbpath等)保持不变。
Allow replication to catch up on this member.允许复制赶上此成员。
Once the member catches up with the other members of the set, repeat the procedure one member at a time for the remaining secondary members for the shard:一旦成员赶上集合中的其他成员,对碎片的其余次要成员一次重复一个成员的过程:
When all the secondaries for the shard have the new index, step down the primary for the shard, restart it as a standalone using the procedure described above, and build the index on the former primary:当碎片的所有辅助设备都具有新索引时,请关闭碎片的主设备,使用上述过程将其作为独立设备重新启动,并在前一个主设备上构建索引:
rs.stepDown() method in the mongo shell to step down the primary. mongo shell中的rs.stepDown()方法来降低主进程。mongodOnce you finish building the index for a shard, repeat C. Build Indexes on the Shards That Contain Collection Chunks for the other affected shards.一旦你完成为一个碎片建立索引,重复C. 在包含其他受影响碎片的集合块的碎片上建立索引。
Once you finish the rolling index build for the affected shards, restart the balancer.完成受影响碎片的滚动索引构建后,重新启动平衡器。
Connect a 将mongo shell to a mongos instance in the sharded cluster, and run sh.startBalancer(): mongo shell连接到sharded集群中的mongos实例,然后运行sh.startBalancer():[3]
| [3] | sh.startBalancer() also enables auto-splitting for the sharded cluster.sh.startBalancer()还为分片集群启用自动拆分。 |
A sharded collection has an inconsistent index if the collection does not have the exact same indexes (including the index options) on each shard that contains chunks for the collection. 如果一个分片集合在包含集合块的每个分片上没有完全相同的索引(包括索引选项),则该分片集合的索引不一致。Although inconsistent indexes should not occur during normal operations, inconsistent indexes can occur, such as:虽然正常操作期间不应出现不一致的索引,但也可能出现不一致的索引,例如:
unique key constraint and one shard contains a chunk with duplicate documents. unique键约束的索引时,一个碎片包含一个包含重复文档的区块。Starting in MongoDB 4.4 (and 4.2.6), the config server primary periodically checks for index inconsistencies across the shards for sharded collections. 从MongoDB 4.4(和4.2.6)开始,配置服务器主服务器会定期检查碎片集合中碎片之间的索引不一致。To configure these periodic checks, see 要配置这些定期检查,请参阅启用enableShardedIndexConsistencyCheck and shardedIndexConsistencyCheckIntervalMS.enableShardedIndexConsistencyCheck和shardedIndexConsistencyCheckIntervalMS。
The command 命令serverStatus returns the field shardedIndexConsistency to report on index inconsistencies when run on the config server primary.serverStatus返回字段shardedIndexConsistency,以报告在配置服务器主服务器上运行时的索引不一致。
To check if a sharded collection has inconsistent indexes, see Find Inconsistent Indexes across Shards.要检查分片集合是否有不一致的索引,请参阅在分片之间查找不一致的索引。