Created
December 20, 2022 05:55
-
-
Save PureKrome/e4de6ab438421a51214bfe051225dcce to your computer and use it in GitHub Desktop.
MongoDB tricks to check performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on https://studio3t.com/knowledge-base/articles/mongodb-query-performance/ | |
// THESE ARE THE Database.Collection | |
var scope = [ | |
"apps.appInstalls", | |
"apps.apps" | |
] | |
// These are the operations to check | |
var generalOps = [ | |
"query", | |
"insert", | |
"update", | |
"remove" | |
] // all: "count", "distinct", "command", "geoNear", "getMore", "group", "insert", "mapReduce", "query", "remove", "update" | |
// Reset / Turn profiling on | |
/* | |
db.setProfilingLevel(0) | |
db.system.profile.drop() | |
db.createCollection("system.profile", { capped: true, size:100000000 } ) //100MB | |
db.setProfilingLevel(2) | |
*/ | |
// Turn off profiling | |
/* | |
db.setProfilingLevel(0) | |
db.system.profile.drop() | |
*/ | |
// Most recent queries | |
db.system.profile.find({ | |
"op": { $in: generalOps }, | |
"ns": { $in: scope } | |
}) | |
.sort({ ts: -1 }) | |
.pretty() | |
//Most recent updates | |
db.system.profile.find({ | |
"op": { $in: generalOps }, | |
"ns": { $in: scope }, | |
"command.u": { $exists: true } | |
}) | |
.sort({ ts: -1 }) | |
.pretty() | |
// Most recent inserts | |
db.system.profile.find({ | |
"op": { $in: generalOps }, | |
"ns": { $in: scope }, | |
"command.insert": { $exists: true } | |
}) | |
.sort({ ts: -1 }) | |
.pretty() | |
// Find all queries doing a COLLSCAN because there is no suitable index | |
db.system.profile.find({ | |
"planSummary": { $eq: "COLLSCAN" }, | |
"op": { $in: generalOps }, | |
"ns": { $in: scope } | |
}) | |
.sort({ millis: -1 }) | |
.pretty() | |
// Find the top ten slowest queries | |
db.system.profile.find({ | |
"op": { $in: generalOps }, | |
"ns": { $in: scope } | |
}) | |
.sort({millis:-1}) | |
.pretty() | |
// Top ten slowest commands/aggregations | |
db.system.profile.find({ | |
"op" : {$eq:"command"} | |
}, { | |
"command.pipeline" : NumberInt(1), | |
"millis": NumberInt(1) | |
}) | |
.sort({millis:-1}) | |
.pretty() | |
//Commenting your queries | |
db.modelClasses.find().comment("some comment"); | |
// Display all queries with comments | |
db.system.profile.find({ "query.comment": "some comment" }, { query: 1 }).pretty() | |
// It is also possible to put a comment in the $match stage of an aggregation expression | |
db.modelClasses.aggregate([ | |
{ $match : { "_meta.deleted": null } }, | |
{ $comment: "some comment" }, | |
{$limit:10} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment