MongoDB 分析查询性能

2020-05-22 00:00:00 索引 查询 文档 返回 扫描

cursor.explain("executionStats")和 db.collection.explain("executionStats") 方法提供关于查询性能的相关信息。这些信息可用于衡量查询是否使用了索引以及如何使用索引。

db.collection.explain() 还提供有关其他操作的执行信息。例如 db.collection.update()。 有关详情信息,请参见 db.collection.explain() 。

评价查询性能

考虑采用以下的 inventory 集合文档:

db.inventory.insert([
    { "_id" : 1, "item" : "f1", type: "food", quantity: 500 },
    { "_id" : 2, "item" : "f2", type: "food", quantity: 100 },
    { "_id" : 3, "item" : "p1", type: "paper", quantity: 200 },
    { "_id" : 4, "item" : "p2", type: "paper", quantity: 150 },
    { "_id" : 5, "item" : "f3", type: "food", quantity: 300 },
    { "_id" : 6, "item" : "t1", type: "toys", quantity: 500 },
    { "_id" : 7, "item" : "a1", type: "apparel", quantity: 250 },
    { "_id" : 8, "item" : "a2", type: "apparel", quantity: 400 },
    { "_id" : 9, "item" : "t2", type: "toys", quantity: 50 },
    { "_id" : 10, "item" : "f4", type: "food", quantity: 75 }
]);

相关文章