在多个MongoDB集合中并行创建索引。

huangapple go评论54阅读模式
英文:

Create index across multiple Mongodb collections in parallel

问题

我有很多具有相同字段的集合,我想在它们上创建索引。我可以使用以下代码执行顺序索引:

db = db.getSiblingDB('mydb');
db.getCollectionNames().forEach(function (collectionName) {
    db.collectionName.createIndex( { orderDate: 1, zipcode: -1 } )
});

我想知道是否可以使用类似的脚本并行创建索引。谢谢。

英文:

I have lots of collections which all have same fields that I'd like to index on. I can perform the sequential index by using:

db = db.getSiblingDB('mydb');
db.getCollectionNames().forEach(function (collectionName) {
    db.collectionName.createIndex( { orderDate: 1, zipcode: -1 } )
});

I would like to know if it's possible to create index in parallel with similar script. Thank you.

答案1

得分: 0

索引构建在任何数据库中都不是顺序的。有两种情况可以考虑:

  1. 如果您的集合为空,则不需要索引任何数据。因此,createIndex 命令只需为将来的目的(写操作)创建索引定义。

  2. 如果您的集合已填充数据,则将创建索引定义,并且索引过程将在后台启动,当集合中的所有数据都被索引后,索引才会被标记为可用。

因此,即使您在此处迭代调用 createIndex

db = db.getSiblingDB('mydb');
db.getCollectionNames().forEach(function (collectionName) {
    db.collectionName.createIndex( { orderDate: 1, zipcode: -1 } )
});

索引数据的过程是在后台进行的。因此,如果您触发多个这样的命令,它们的数据将在后台并行填充。

在这里了解更多关于索引构建的信息链接
创建 MongoDB 索引

英文:

Index builds are not sequential internally, in any database. There are two cases you can consider:

  1. Your collection is empty. In this case, no data needs to be indexed. So the createIndex command just has to create an index definition for future purposes (write operations).

  2. Your collection is populated. In this case, the index definition will be created and the indexing process will start in the background, and when all of the data in the collection is indexed, only then the index will be marked as ready for use.

So even, if you are calling createIndex, iteratively here:

db = db.getSiblingDB('mydb');
db.getCollectionNames().forEach(function (collectionName) {
    db.collectionName.createIndex( { orderDate: 1, zipcode: -1 } )
});

The process of populating the data in the index is done in the background. So, if you trigger multiple such commands, their data will be populated parallelly in the background.

Read more about index builds here.
Create Index MongoDB.

huangapple
  • 本文由 发表于 2023年5月17日 11:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定