分析查询性能

    在本页面

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

    db.collection.explain() 提供关于其他操作(如db.collection.update())执行的信息。详细信息请参见db.collection.explain()

    评估查询的性能

    考虑一个包含以下文件的收集清单:

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

    没有索引的查询

    以下查询检索的文档中,quantity字段的值在100200之间,包括:

    1. db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )

    查询返回以下文档:

    1. { "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
    2. { "_id" : 3, "item" : "p1", "type" : "paper", "quantity" : 200 }
    3. { "_id" : 4, "item" : "p2", "type" : "paper", "quantity" : 150 }

    要查看所选的查询计划,请将cursor.explain("executionStats")游标方法链接到find命令的末尾:

    1. db.inventory.find(
    2. { quantity: { $gte: 100, $lte: 200 } }
    3. ).explain("executionStats")

    explain() 返回以下结果:

    1. {
    2. "queryPlanner" : {
    3. "plannerVersion" : 1,
    4. ...
    5. "winningPlan" : {
    6. "stage" : "COLLSCAN",
    7. ...
    8. }
    9. },
    10. "executionStats" : {
    11. "executionSuccess" : true,
    12. "nReturned" : 3,
    13. "executionTimeMillis" : 0,
    14. "totalKeysExamined" : 0,
    15. "totalDocsExamined" : 10,
    16. "executionStages" : {
    17. "stage" : "COLLSCAN",
    18. ...
    19. },
    20. ...
    21. },
    22. ...
    23. }

    匹配文档的数量和检查文档的数量之间的差异可能表明,为了提高效率,查询可能会受益于索引的使用。

    查询与索引

    为了支持对quantity字段的查询,请在quantity字段上添加索引:

    1. db.inventory.createIndex( { quantity: 1 } )

    要查看查询计划统计信息,请使用explain(“executionStats”)方法:

    1. db.inventory.find(
    2. { quantity: { $gte: 100, $lte: 200 } }
    3. ).explain("executionStats")

    explain()方法返回以下结果:

    1. {
    2. "queryPlanner" : {
    3. "plannerVersion" : 1,
    4. ...
    5. "winningPlan" : {
    6. "stage" : "FETCH",
    7. "inputStage" : {
    8. "stage" : "IXSCAN",
    9. "keyPattern" : {
    10. "quantity" : 1
    11. },
    12. ...
    13. }
    14. },
    15. "rejectedPlans" : [ ]
    16. },
    17. "executionStats" : {
    18. "executionSuccess" : true,
    19. "nReturned" : 3,
    20. "executionTimeMillis" : 0,
    21. "totalKeysExamined" : 3,
    22. "totalDocsExamined" : 3,
    23. "executionStages" : {
    24. ...
    25. },
    26. ...
    27. },
    28. ...
    29. }

    如果没有索引,查询将扫描包含10个文档的整个集合,以返回3个匹配的文档。查询还必须扫描每个文档的全部内容,可能会将它们拉到内存中。这将导致昂贵的查询操作,并且可能会很慢。

    当使用索引运行时,查询扫描了3个索引项和3个文档,以返回3个匹配的文档,从而产生一个非常高效的查询。

    比较索引的性能

    要手动比较使用多个索引的查询的性能,可以将 hint()方法与explain()方法结合使用。

    考虑以下查询:

    1. db.inventory.find( {
    2. quantity: {
    3. $gte: 100, $lte: 300
    4. },
    5. type: "food"
    6. } )

    查询返回以下文档:

    1. { "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
    2. { "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }

    要支持查询,添加复合索引。对于复合索引,字段的顺序很重要。

    例如,添加以下两个复合索引。第一个索引首先按数量字段排序,然后按类型字段排序。第二个索引首先按类型排序,然后是quantity字段。

    1. db.inventory.createIndex( { quantity: 1, type: 1 } )
    2. db.inventory.createIndex( { type: 1, quantity: 1 } )

    评估第一个索引对查询的影响:

    1. db.inventory.find(
    2. { quantity: { $gte: 100, $lte: 300 }, type: "food" }
    3. ).hint({ quantity: 1, type: 1 }).explain("executionStats")

    explain()方法返回如下输出:

    1. {
    2. "queryPlanner" : {
    3. ...
    4. "winningPlan" : {
    5. "stage" : "FETCH",
    6. "inputStage" : {
    7. "stage" : "IXSCAN",
    8. "keyPattern" : {
    9. "quantity" : 1,
    10. "type" : 1
    11. },
    12. ...
    13. }
    14. }
    15. },
    16. "rejectedPlans" : [ ]
    17. },
    18. "executionStats" : {
    19. "executionSuccess" : true,
    20. "nReturned" : 2,
    21. "executionTimeMillis" : 0,
    22. "totalKeysExamined" : 5,
    23. "totalDocsExamined" : 2,
    24. "executionStages" : {
    25. ...
    26. }
    27. },
    28. ...
    29. }

    MongoDB扫描了5个索引键(executionStats.totalKeysExamined)以返回2个匹配的文档(executionStats.nReturned)。

    评估第二个索引对查询的影响:

    1. db.inventory.find(
    2. { quantity: { $gte: 100, $lte: 300 }, type: "food" }
    3. ).hint({ type: 1, quantity: 1 }).explain("executionStats")

    explain()方法返回如下输出:

    1. {
    2. "queryPlanner" : {
    3. ...
    4. "winningPlan" : {
    5. "stage" : "FETCH",
    6. "inputStage" : {
    7. "stage" : "IXSCAN",
    8. "keyPattern" : {
    9. "type" : 1,
    10. "quantity" : 1
    11. },
    12. ...
    13. }
    14. },
    15. "rejectedPlans" : [ ]
    16. },
    17. "executionStats" : {
    18. "executionSuccess" : true,
    19. "nReturned" : 2,
    20. "executionTimeMillis" : 0,
    21. "totalKeysExamined" : 2,
    22. "totalDocsExamined" : 2,
    23. "executionStages" : {
    24. ...
    25. }
    26. },
    27. ...
    28. }

    MongoDB扫描了2个索引键(executionStats.totalKeysExamined)以返回2个匹配的文档(executionStats.nReturned)。

    对于这个示例查询,复合索引{type: 1, quantity: 1}比复合索引{quantity: 1, type: 1}更有效。

    ​ 也可以看看

    查询优化查询计划优化查询性能索引策略

    译者:杨帅

    校对:杨帅

    参见

    原文 - Analyze Query Performance