-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplayground.mongodb.js
63 lines (56 loc) · 1.78 KB
/
playground.mongodb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* global use, db */
// MongoDB Playground
use('bmap');
// Show indexes for our main collections
print('\n=== Bitcoin Schema Collection Indexes ===');
const collections = [
'follow',
'unfollow',
'unlike',
'like',
'message',
'repost',
'friend',
'post',
'ord',
];
// Get collection stats and indexes
for (const collection of collections) {
const coll = db.getCollection(collection);
if (coll) {
try {
const stats = coll.stats();
const indexes = coll.getIndexes();
print(`\n=== ${collection} ===`);
print('Indexes:');
printjson(indexes);
print('\nStats:');
print(`Documents: ${stats.count}`);
print(`Size: ${(stats.size / 1024 / 1024).toFixed(2)} MB`);
print(`Average document size: ${(stats.avgObjSize / 1024).toFixed(2)} KB`);
if (stats.indexSizes) {
print('\nIndex Sizes:');
for (const [indexName, size] of Object.entries(stats.indexSizes)) {
print(`${indexName}: ${(size / 1024 / 1024).toFixed(2)} MB`);
}
}
// Analyze a sample query using block height index
print('\nQuery Analysis (block height index):');
const explain = coll
.find({
'blk.i': { $gt: 875000 },
})
.explain('executionStats');
if (explain.executionStats) {
print(`Execution time: ${explain.executionStats.executionTimeMillis}ms`);
print(`Documents examined: ${explain.executionStats.totalDocsExamined}`);
print(`Documents returned: ${explain.executionStats.nReturned}`);
if (explain.executionStats.executionStages.stage === 'COLLSCAN') {
print('WARNING: Collection scan detected - consider adding an index');
}
}
} catch (e) {
print(`Error analyzing ${collection}: ${e.message}`);
}
}
}