高性能mongodb之应用程序跑执行计划

栏目: 数据库 · 发布时间: 5年前

内容简介:之前发了一篇执行计划的模式为三种: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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms for Image Processing and Computer Vision

Algorithms for Image Processing and Computer Vision

Parker, J. R. / 2010-12 / 687.00元

A cookbook of algorithms for common image processing applications Thanks to advances in computer hardware and software, algorithms have been developed that support sophisticated image processing with......一起来看看 《Algorithms for Image Processing and Computer Vision》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具