gaoke 发表于 2018-10-26 10:24:59

MongoDB 3.0 集合方法 db.collection.explain()

  MongoDB 3.0 集合方法 db.collection.explain()
  描述
  
  db.collection.explain()
  3.0版本的新功能
  返回如下操作的查询计划信息:
  aggregate();
  count();
  find();
  group();
  remove();
  update();
  为了使用db.collection.explain(),追加以上可用方法到db.collection.explain()来做解析:
  db.collection.explain().
  例如:
db.products.explain().remove({category:"apparel"},{justOne:true})  更多例子,查看下面示例部分。使用db.collection.explain()的可用方法列表,查看db.collection.explain().help()部分。
  db.collection.explain()方法有如下参数:
  参数 类型 描述
  verbosity string 可选。指定解析输出的详细信息模式。该模式影响了explain()的行为,决定了返回信息的数量。可能的模式有:“queryPlanner”、“executionStats”和“allPlansExecution”。
  默认的模式是“queryPlanner”。
  为了cursor.explain()早期版本的向后兼容,MongoDB标识true为“allPlansExecution”,false为“queryPlanner”。
  关于模式的更多信息,查看详细信息模式部分。
  行为
  
  详细信息模式
  db.collection.explain()的行为和返回信息数量依赖于verbosity模式。
  queryPlanner模式
  
  默认情况下,db.collection.explain()运行在queryPlanner详细信息模式。
  MongoDB运行查询优化器选择评估操作的最优计划。
  db.collection.explain()返回被评估方法的queryPlanner信息。
  executionStats模式
  
  MongoDB运行查询优化器选择最优计划,执行最优计划来完成操作,并返回描述最优计划执行的统计信息。
  对于写操作,db.collection.explain()返回关于将会被执行的更新或删除操作的信息,但不应用修改到数据库。
  db.collection.explain()对于被评估的方法返回queryPlanner和executionStats信息。然而,executionStats对于放弃的计划不提供查询执行信息。
  allPlansExecution模式
  
  MongoDB运行查询优化器选择最优计划,执行最优计划来完成操作。在“allPlansExecution”模式,MongoDB返回描述最优计划的执行统计信息,也返回在计划选择期间其他备选计划的统计信息。
  对于写操作,db.collection.explain()返回关于将会被执行的更新或删除操作的信息,但不应用修改到数据库。
  db.collection.explain()对于被评估的方法返回queryPlanner和executionStats信息。executionStats对于最优计划博阿含完整的查询执行信息。
  如果查询优化器评估了多个计划,对于最优和被放弃的备选计划,executionStats信息也包含在计划选择阶段捕获的部分执行信息。
  explain()技巧
  
  db.collection.explain()方法包装了explain命令,它是运行explain的更好方式。
  db.collection.explain().find()类似于db.collection.find().explain()有如下关键不同:
  1.db.collection.explain().find()结构允许增加查询修饰符链。列出查询修饰符列表,查看db.collection.explain().find().help()部分。
  2.db.collection.explain().find()返回一个游标,它需要调用.next(),或者它的别名.finish(),以返回explain()结果。如果交互式运行在mongo shell,mongo shell会自动调用.finish()来返回结果。对于脚本,然而,你必须显式调用.next()或.finish()以返回结果。列出游标相关的方法,查看db.collection.explain().find().help()部分。
  db.collection.explain().aggregate()等价于传递explain选项到db.collection.aggregate()方法。
  help()
  
  为了查看db.collection.explain()支持的操作的列表,运行:
  db.collection.explain().help()
  db.collection.explain().find()返回一个游标,它允许增加查询修饰符链。为了查看db.collection.explain().find()支持的查询修饰符的列表以及游标相关的方法,运行:
  db.collection.explain().find().help()
  你可以链接多个修饰符到db.collection.explain().find()。示例,可以查看使用修饰符解析find()部分。
  示例
  
  queryPlanner模式
  
  默认情况下,db.collection.explain()运行在“queryPlanner”详细信息模式。
  下面的示例对于指定的count()操作,以“queryPlanner”模式运行db.collection.explain()来返回查询计划信息:
db.products.explain().count( { quantity: { $gt: 50 } } )  executionStats模式
  
  下面的示例对于指定的find()操作,以“executionStats”详细信息模式运行db.collection.explain()来返回查询计划和执行信息:
db.products.explain("executionStats").find(  
{ quantity: { $gt: 50 }, category: "apparel" }
  
)
  allPlansExecution模式
  
  下面的示例以“allPlansExecution”详细信息模式运行db.collection.explain()。对于指定的update()操作,db.collection.explain()返回所有的评估的计划的queryPlanner和executionStats:
  注意:
  解析的执行将不会修改数据,但是会运行更新操作的预查询。对于备选计划,MongoDB返回在计划选择阶段捕获的执行信息。
db.products.explain("allPlansExecution").update(  
{ quantity: { $lt: 1000}, category: "apparel" },
  
{ $set: { reorder: true } }
  
)
  使用修饰符解析find()
  
  db.collection.explain().find()结构允许查询修饰符链。例如,下面的操作提供了使用sort()和hint()查询修饰符的find()方法的信息:
db.products.explain("executionStats").find(  
{ quantity: { $gt: 50 }, category: "apparel" }
  
).sort( { quantity: -1 } ).hint( { category: 1, quantity: -1 } )
  获得可用的查询修饰符列表,在mongo shell中运行:
  db.collection.explain().find().help()
  重申explain().find()返回游标
  
  db.collection.explain().find()返回解析结果的游标。如果在mongo shell交互式运行,mongo shell自动使用.next()方法重复这个游标。对于脚本,然而,你必须显式调用.next()(或者它的别名.finish())以返回结果:
var explainResult = db.products.explain().find( { category: "apparel" } ).next();  输出
  
  db.collection.explain()操作可以返回相关信息:
  1.queryPlanner,它描述了查询优化器选择的计划和列出放弃的计划;
  2.executionStats,它描述了最优计划和放弃计划的执行;
  3.serverInfo,它提供了关于MongoDB实例的信息。
  详细信息模式(例如:queryPlanner、executionStats、allPlansExecution)决定了结果是否包含executionStats,和executionStats是否包含在计划选择阶段捕获的数据。
  关于输出的详细信息,查看解析结果部分。
  对于一个混合版本的分片集群,带有3.0版本的mongos和至少一个2.6版本的mongod分片,当你在3.0版本的mongo shell运行db.collection.explain(),db.collection.explain()将重试$explain操作以2.6版本格式返回结果。
  参见:
  http://docs.mongodb.org/manual/reference/method/db.collection.explain/
  http://docs.mongodb.org/manual/reference/command/explain/
  http://docs.mongodb.org/manual/reference/explain-results/
  http://docs.mongodb.org/manual/reference/operator/meta/explain/
  http://docs.mongodb.org/manual/reference/operator/query-modifier/


页: [1]
查看完整版本: MongoDB 3.0 集合方法 db.collection.explain()