Created
June 8, 2023 18:45
-
-
Save selcukusta/bcd031bcbc835f0f674f64647131604d to your computer and use it in GitHub Desktop.
Devnot 2023 (https://dotnet.devnot.com/) Samples
This file contains 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
function formatBytes(bytes, decimals = 2) { | |
if (!+bytes) return "0 Bytes"; | |
const k = 1024; | |
const dm = decimals < 0 ? 0 : decimals; | |
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | |
const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; | |
} | |
// docker container run -p 27017:27017 --name mongo-sample mongo:4.2 | |
// db.hostInfo().system.memLimitMB //RAM = 7951 MB | |
// 7951 MB = 7,764648438 GB | |
// (7,764648438 - 1) * 0,5 = ~3,38GB WiredTiger Maximum Internal Cache Size | |
// ((7,764648438 - 1) * 0,5) * 0,8 = ~2,70GB (WiredTiger tries keeping the cache under) | |
// ((7,764648438 - 1) * 0,5) * 0,05 = ~174MB (WiredTiger tries keeping the dirty under) | |
formatBytes(db.serverStatus().wiredTiger.cache["tracked dirty bytes in the cache"]) | |
formatBytes(db.serverStatus().wiredTiger.cache["bytes currently in the cache"]) | |
formatBytes(db.serverStatus().wiredTiger.cache["maximum bytes configured"]) |
This file contains 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
function formatBytes(bytes, decimals = 2) { | |
if (!+bytes) return "0 Bytes"; | |
const k = 1024; | |
const dm = decimals < 0 ? 0 : decimals; | |
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | |
const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; | |
} | |
const COLLECTION_NAME = "devnot"; | |
var idxStats = {}; | |
db.getCollection(COLLECTION_NAME) | |
.aggregate({ $indexStats: {} }) | |
.forEach(function (idx) { | |
idxStats[idx.name] = { | |
ops: idx.accesses.ops, | |
since: idx.accesses.since, | |
}; | |
}); | |
Object.keys(db.getCollection(COLLECTION_NAME).stats().indexSizes).map(function ( | |
idx | |
) { | |
idxStats[idx].sizeInBytes = db | |
.getCollection(COLLECTION_NAME) | |
.stats().indexSizes[idx]; | |
idxStats[idx].sizeHumanReadable = formatBytes( | |
db.getCollection(COLLECTION_NAME).stats().indexSizes[idx] | |
); | |
}); | |
print( | |
Object.fromEntries( | |
Object.entries(idxStats).sort( | |
([, a], [, b]) => b.sizeInBytes - a.sizeInBytes | |
) | |
) | |
); |
This file contains 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
[ | |
{ | |
"database": "devnot", | |
"collection": "devnot", | |
"count": 1000000, | |
"content": { | |
"first_name": { | |
"type": "faker", | |
"method": "FirstName" | |
}, | |
"last_name": { | |
"type": "faker", | |
"method": "LastName" | |
}, | |
"country_code": { | |
"type": "enum", | |
"values": [ | |
"Turkey", | |
"Belgium", | |
"Spain", | |
"United Kingdom", | |
"France", | |
"Ukraine", | |
"Russia", | |
"Hungaria", | |
"Italy", | |
"Netherlands" | |
] | |
}, | |
"created_at": { | |
"type": "date", | |
"startDate": "2020-01-01T00:00:00+00:00", | |
"endDate": "2023-01-01T00:00:00+00:00" | |
}, | |
"birth_date": { | |
"type": "date", | |
"startDate": "1987-01-01T00:00:00+00:00", | |
"endDate": "2000-01-01T00:00:00+00:00" | |
} | |
} | |
} | |
] |
This file contains 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
function quick_explain(explainPlan) { | |
var stepNo = 1; | |
var printInputStage = function (step) { | |
if ("inputStage" in step) { | |
printInputStage(step.inputStage); | |
} | |
if ("inputStages" in step) { | |
step.inputStages.forEach(function (inputStage) { | |
printInputStage(inputStage); | |
}); | |
} | |
if ("indexName" in step) { | |
print(stepNo++, step.stage, step.indexName); | |
} else { | |
print(stepNo++, step.stage); | |
} | |
}; | |
printInputStage(explainPlan); | |
} | |
// (E) | |
quick_explain( | |
db.devnot.find({ country_code: "Turkey" }).explain("executionStats") | |
.queryPlanner.winningPlan | |
); | |
// (ES) | |
quick_explain( | |
db.devnot | |
.find({ country_code: "Turkey" }) | |
.sort({ birth_date: 1 }) | |
.explain("executionStats").queryPlanner.winningPlan | |
); | |
// (ESR) | |
quick_explain( | |
db.devnot | |
.find({ | |
country_code: "Turkey", | |
created_at: { $gte: new ISODate("2021-05-13T00:00:00+00:00") }, | |
}) | |
.sort({ birth_date: 1 }) | |
.explain("executionStats").queryPlanner.winningPlan | |
); | |
// (E - OPTIMIZATION) | |
db.devnot.createIndex( | |
{ | |
country_code: 1, | |
first_name: 1, | |
last_name: 1, | |
}, | |
{ name: "country_code_1_first_name_1_last_name_1" } | |
); | |
quick_explain( | |
db.devnot | |
.find({ country_code: "Turkey" }, { _id: 0, first_name: 1, last_name: 1 }) | |
.explain("executionStats").queryPlanner.winningPlan | |
); | |
//( ES - OPTIMIZATION) | |
db.devnot.dropIndex("country_code_1_first_name_1_last_name_1"); | |
db.devnot.createIndex( | |
{ | |
country_code: 1, | |
birth_date: 1, | |
first_name: 1, | |
last_name: 1, | |
}, | |
{ name: "country_code_1_birth_date_1_first_name_1_last_name_1" } | |
); | |
quick_explain( | |
db.devnot | |
.find( | |
{ country_code: "Turkey" }, | |
{ _id: 0, first_name: 1, last_name: 1, birth_date: 1 } | |
) | |
.sort({ birth_date: 1 }) | |
.explain("executionStats").queryPlanner.winningPlan | |
); | |
// (ESR - OPTIMIZATION) | |
db.devnot.dropIndex("country_code_1_birth_date_1_first_name_1_last_name_1"); | |
db.devnot.createIndex( | |
{ | |
country_code: 1, | |
birth_date: 1, | |
created_at: 1, | |
first_name: 1, | |
last_name: 1, | |
}, | |
{ name: "country_code_1_birth_date_1_created_at_1_first_name_1_last_name_1" } | |
); | |
quick_explain( | |
db.devnot | |
.find( | |
{ | |
country_code: "Turkey", | |
created_at: { $gte: new ISODate("2021-05-13T00:00:00+00:00") }, | |
}, | |
{ _id: 0, first_name: 1, last_name: 1, birth_date: 1, created_at: 1 } | |
) | |
.sort({ birth_date: 1 }) | |
.explain("executionStats").queryPlanner.winningPlan | |
); |
This file contains 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
// Build collection via BLOATED PATTERN | |
db.getCollection("listings").insertMany([ | |
{ | |
"_id": ObjectId("6475149e9f76586af8dabac8"), | |
"SKU": "ABC123", | |
"Price": 100, | |
"Manufacturers": [ | |
{ | |
"_id": ObjectId("647516b1bbe1afbc4b70f9bc"), | |
"Name": "Kardeşler Ticaret", | |
}, | |
{ | |
"_id": ObjectId("647516e596a5688d22521348"), | |
"Name": "Usta LTD. ŞTİ.", | |
} | |
] | |
}, | |
{ | |
"_id": ObjectId("647514a519060cc6aef408c6"), | |
"SKU": "ABC456", | |
"Price": 200, | |
"Manufacturers": [ | |
{ | |
"_id": ObjectId("647516b1bbe1afbc4b70f9bc"), | |
"Name": "Kardeşler Ticaret", | |
}, | |
{ | |
"_id": ObjectId("647516e96900f8f886b3df50"), | |
"Name": "Devnot A.Ş.", | |
} | |
] | |
} | |
]) | |
db.listings.find({ | |
"Manufacturers.Name": "Kardeşler Ticaret" | |
}, { _id: 0, "Manufacturers.Name": 1, "SKU": 1 }) | |
.explain("executionStats") | |
db.listings.createIndex( | |
{ | |
"Manufacturers.Name": 1, | |
"SKU": 1 | |
}, | |
{ name: "Manufacturers.Name_1_SKU_1" } | |
) | |
// Build collection via SUBSET PATTERN | |
db.getCollection("manufacturers").insertMany([ | |
{ | |
"_id": ObjectId("647516b1bbe1afbc4b70f9bc"), | |
"Name": "Kardeşler Ticaret", | |
}, | |
{ | |
"_id": ObjectId("647516e596a5688d22521348"), | |
"Name": "Usta LTD. ŞTİ.", | |
}, | |
{ | |
"_id": ObjectId("647516e96900f8f886b3df50"), | |
"Name": "Devnot A.Ş.", | |
} | |
]) | |
db.getCollection("listings").insertMany([ | |
{ | |
"_id": ObjectId("6475149e9f76586af8dabac8"), | |
"SKU": "ABC123", | |
"Price": 100, | |
"Manufacturers": [ | |
ObjectId("647516b1bbe1afbc4b70f9bc"), | |
ObjectId("647516e596a5688d22521348"), | |
] | |
}, | |
{ | |
"_id": ObjectId("647514a519060cc6aef408c6"), | |
"SKU": "ABC456", | |
"Price": 200, | |
"Manufacturers": [ | |
ObjectId("647516b1bbe1afbc4b70f9bc"), | |
ObjectId("647516e96900f8f886b3df50"), | |
] | |
} | |
]) | |
//Sorgula.. | |
db.manufacturers | |
.explain("executionStats") | |
.aggregate([ | |
{ | |
$match: { | |
"Name": "Kardeşler Ticaret", | |
} | |
}, | |
{ | |
$project: { | |
"Name": 1, | |
"_id": 1, | |
} | |
}, | |
{ | |
$lookup: { | |
from: "listings", | |
localField: "_id", | |
foreignField: "Manufacturers", | |
as: "l" | |
} | |
}, | |
{ | |
$project: { | |
"Name": 1, | |
"SKU": "$l.SKU", | |
"_id" : 0, | |
} | |
}, | |
]) | |
db.manufacturers.createIndex( | |
{ | |
"Name": 1, | |
"_id": 1, | |
}, | |
{ name: "Name_1__id_1" } | |
) |
This file contains 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
function formatBytes(bytes, decimals = 2) { | |
if (!+bytes) return "0 Bytes"; | |
const k = 1024; | |
const dm = decimals < 0 ? 0 : decimals; | |
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | |
const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; | |
} | |
var jObject = { | |
totalDataSize: 0, | |
totalIndexSize: 0, | |
databases: [], | |
}; | |
databases = db.adminCommand({ | |
listDatabases: 1, | |
nameOnly: true, | |
filter: { name: { $nin: ["admin", "local", "config"] } }, | |
}).databases; | |
databases.map(function (d) { | |
let dbName = d.name; | |
let currentDb = db.getSiblingDB(dbName); | |
jObject.totalDataSize += currentDb.stats().dataSize; | |
jObject.totalDataSizeHumanReadable += formatBytes(currentDb.stats().dataSize); | |
jObject.totalIndexSize += currentDb.stats().indexSize; | |
jObject.totalIndexSizeHumanReadable += formatBytes( | |
currentDb.stats().indexSize | |
); | |
let subObj = { dbName: dbName }; | |
subObj["totalDataSize"] = currentDb.stats().dataSize; | |
subObj["totalDataSizeHumanReadable"] = formatBytes( | |
currentDb.stats().dataSize | |
); | |
subObj["totalIndexSize"] = currentDb.stats().indexSize; | |
subObj["totalIndexSizeHumanReadable"] = formatBytes( | |
currentDb.stats().indexSize | |
); | |
jObject.databases.push(subObj); | |
}); | |
jObject["totalDataSizeHumanReadable"] = formatBytes(jObject.totalDataSize); | |
jObject["totalIndexSizeHumanReadable"] = formatBytes(jObject.totalIndexSize); | |
print(jObject); |
This file contains 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
function formatBytes(bytes, decimals = 2) { | |
if (!+bytes) return "0 Bytes"; | |
const k = 1024; | |
const dm = decimals < 0 ? 0 : decimals; | |
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; | |
const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; | |
} | |
var collectionStats = []; | |
// Get the sizes | |
db.getCollectionNames().forEach(function (collectionName) { | |
if (collectionName !== "system.views") { | |
var collection = db.getCollection(collectionName); | |
var indexSizeInBytes = collection.totalIndexSize(); | |
collectionStats.push({ | |
collection: collectionName, | |
indexSizeInBytes: indexSizeInBytes, | |
sizeHumanReadable: formatBytes(indexSizeInBytes), | |
}); | |
} | |
}); | |
// Sort on collection name or index size | |
var reverse = true; | |
var sortField = "indexSizeInBytes"; | |
collectionStats.sort(function (a, b) { | |
comparison = a[sortField] - b[sortField]; | |
if (isNaN(comparison)) comparison = a.collection.localeCompare(b.collection); | |
if (reverse) comparison *= -1; | |
return comparison; | |
}); | |
undefined; | |
// Print the collection stats | |
print(collectionStats); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment