内容简介:之前发了一篇执行计划的模式为三种:queryPlanner executionStats allPlansExecution。第一种不会真正跑命令本身,只有响应命令分析后的报告。上面例子的响应结果就是对 db.collecitonName.find({}) 这个查询语句的分析。我使用的是java, mongodb库用的是mongodb-java-driver。mongodb-java-driver的API提供了两种方式去跑执行计划:
执行计划
之前发了一篇 关于 mongodb 执行计划的说明 。利用执行计划,我们可以判断每一次 sql 的执行情况和mongodb给出的执行建议。在mongo shell中跑执行计划的命令,举个例子:
db.collecitonName.find({}).explain("queryPlanner")
执行计划的模式为三种:queryPlanner executionStats allPlansExecution。第一种不会真正跑命令本身,只有响应命令分析后的报告。上面例子的响应结果就是对 db.collecitonName.find({}) 这个查询语句的分析。
程序中跑执行计划
我使用的是java, mongodb库用的是mongodb-java-driver。mongodb-java-driver的API提供了两种方式去跑执行计划:
方式一:
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port));
mongoClient.getDB("xxx").getCollection("yyy").find(quert).explain();
这是一个便捷的方式。这种方式会真正执行命令,也就是说它使用的是executionStats模式。响应结果会有执行时间、扫描记录数等真实的执行情况。如果你的程序想要在命令执行前做一个预判,这个API不是你想要的。
方式二:
API没有提供queryPlanner的方式。我花了一些时间去搜索资料,发现网上没有跑queryPlanner的需求,至少我是没有找到类似的发问和使用例子。纠结了一会儿,最终发现库里有这样一个api, mongoClient.getDB("xxx").command(BasicDBObject command),支持程序传入一个命令。最后在 官方文档 里找到了这样一个说明:
explain New in version 3.0. The explain command provides information on the execution of the following commands: aggregate, count, distinct, group, find, findAndModify, delete, and update. Although MongoDB provides the explain command, the preferred method for running explain is to use the db.collection.explain() and cursor.explain() helpers. The explain command has the following syntax:
语法如下:
{
explain: <command>,
verbosity: <string>
}
explain: <command>。 支持 aggregate, count, distinct, group, find, findAndModify, delete, and update等等的命令。
verbosity: <string>。支持模式"queryPlanner" 、"executionStats" 、"allPlansExecution" (Default)
跟踪 find 进去,find支持的字段如下,应有尽有。
{
"find": <string>,
"filter": <document>,
"sort": <document>,
"projection": <document>,
"hint": <document or string>,
"skip": <int>,
"limit": <int>,
"batchSize": <int>,
"singleBatch": <bool>,
"comment": <string>,
"maxScan": <int>, // Deprecated in MongoDB 4.0
"maxTimeMS": <int>,
"readConcern": <document>,
"max": <document>,
"min": <document>,
"returnKey": <bool>,
"showRecordId": <bool>,
"tailable": <bool>,
"oplogReplay": <bool>,
"noCursorTimeout": <bool>,
"awaitData": <bool>,
"allowPartialResults": <bool>,
"collation": <document>
}
通过阅读文档,跑queryPlanner模式的执行计划应该是这样的:
//查询某个集合,queryCondition是查询条件。
MongoClient mongoClient = MongoUtil.getConnection(mongodb.getHost(), mongodb.getPort(), "", "", mongodb.getDb());
BasicDBObject command = new BasicDBObject();
BasicDBObject find = new BasicDBObject();
find.put("find", "集合名");
find.put("filter", queryCondition);//查询条件,是一个BasicDBObject
command.put("explain", find);
command.put("verbosity", "queryPlanner");
CommandResult explainResult = mongoClient.getDB(mongodb.getDb()).command(command);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- [译] Node.js 高性能和可扩展应用程序的最佳实践 [第 1/3 部分]
- [译] Node.js 高性能和可扩展应用程序的最佳实践 [第 2/3 部分]
- Android里应用程序,应用程序窗口和视图对象之间的关系
- 使用 Bluemix、Watson Discovery 和 Cloudant 构建移动应用程序来分析其他应用程序
- ChromeOS 终端应用程序暗示其即将支持 Linux 应用
- 这几天在C程序中有哪些应用程序?
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。