Skip to content

Instantly share code, notes, and snippets.

@HugoPoi
Created February 1, 2019 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoPoi/93c069272a01ba75d6fd34ae332083d6 to your computer and use it in GitHub Desktop.
Save HugoPoi/93c069272a01ba75d6fd34ae332083d6 to your computer and use it in GitHub Desktop.
MongoDB get estimate oplog per collection with profiler
// https://docs.mongodb.com/manual/tutorial/manage-the-database-profiler/#enable-and-configure-database-profiling
(() => {
return db.system.profile.aggregate([
{ $match: { $or: [{op: {$in: ['update', 'insert', 'delete']}}, {'command.writeConcern': {$exists: true}}] } },
{ $group: {_id: {op : '$op', ns: '$ns', client: "$client"}, count: {$sum: 1}, start: {$min: "$ts"}, end: {$max: "$ts"} } },
{ $match: { count: { $gt: 50}}},
{ $sort: {count: -1} }
]).map((requestStats) => {
let stats = db.getCollection(requestStats._id.ns.split('.')[1]).stats();
requestStats.queryPerHour = ( 3600000 / (requestStats.end - requestStats.start) ) * requestStats.count;
requestStats.estimateOpLogPerHour = stats.avgObjSize * requestStats.queryPerHour;
requestStats.estimateOpLogPerHourMB = requestStats.estimateOpLogPerHour / ( 1024 * 1024 );
return requestStats;
})
//.reduce((acc,e) => e.estimateOpLogPerHourMB + acc, 0);
.sort((a,b) => b.estimateOpLogPerHourMB - a.estimateOpLogPerHourMB);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment