英文:
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
索引构建在任何数据库中都不是顺序的。有两种情况可以考虑:
-
如果您的集合为空,则不需要索引任何数据。因此,
createIndex
命令只需为将来的目的(写操作)创建索引定义。 -
如果您的集合已填充数据,则将创建索引定义,并且索引过程将在后台启动,当集合中的所有数据都被索引后,索引才会被标记为可用。
因此,即使您在此处迭代调用 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:
-
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). -
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论