Skip to content

Commit 288c84a

Browse files
committed
A few fixes
1 parent bfe55f4 commit 288c84a

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

skills/mongodb-query-optimizer/references/core-indexing-principles.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The first field of the index should be in the query's filter or sort condition.
88

99
- **Equality** fields first (e.g. `{field: value}`, `{$in: [...]}` with \<= 200 elements, `{field: {$eq: value}}`)
1010
- **Sort** fields next
11-
- **Range** fields last (e.g. `$gt`, `$lt`, `$gte`, `$lte`, `{$in: [...]}` with \> 200 elements in the array), `$ne, anchored case-sensitive $regex`)
11+
- **Range** fields last (e.g. `$gt`, `$lt`, `$gte`, `$lte`, `{$in: [...]}` with \> 200 elements in the array, `$ne`, anchored case-sensitive `$regex`)
1212

1313
If equality is not very selective and range is, then ERS may perform better than ESR.
1414

testing/mongodb-query-optimizer/evals/evals.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
{
55
"id": 1,
66
"prompt": "How do I optimize this query? db.orders.find({ status: 'open', tags: { $in: ['urgent', 'priority', 'escalated'] } }).sort({ createdAt: -1 }).limit(50). The tags field usually has 3-5 values per document but the $in list could grow to much larger from a user filter.",
7-
"expected_output": "Recommends index { status: 1, tags: 1, createdAt: -1 }, notes that $in with many elements is treated as a range scan rather than equality.",
7+
"expected_output": "Recommends index { status: 1, tags: 1, createdAt: 1 }, notes that $in with many elements is treated as a range scan rather than equality.",
88
"files": [],
99
"expectations": [
1010
"Recommends a compound index covering status, tags, and createdAt",
1111
"Mentions ESR ordering or explains equality-before-sort-before-range",
12-
"Suggests that if the $in list stays small, { status: 1, tags: 1, createdAt: -1 } works, but if it grows large the performance is likely to degrade"
12+
"Suggests that if the $in list stays small, { status: 1, tags: 1, createdAt: 1 } works, but if it grows large the performance is likely to degrade"
1313
]
1414
},
1515
{

testing/mongodb-query-optimizer/setup-slow-queries.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,15 @@ async function main() {
189189
console.log(` Inserted ${customers.length} customers`);
190190

191191
console.log(`Generating ${NUM_ORDERS} orders...`);
192-
const orders = generateOrders(customers, NUM_ORDERS);
193-
// Insert in batches of 2000
194-
for (let i = 0; i < orders.length; i += 2000) {
195-
await db.collection("orders").insertMany(orders.slice(i, i + 2000));
192+
const ORDERS_BATCH_SIZE = 2000;
193+
let insertedOrders = 0;
194+
for (let i = 0; i < NUM_ORDERS; i += ORDERS_BATCH_SIZE) {
195+
const batchSize = Math.min(ORDERS_BATCH_SIZE, NUM_ORDERS - i);
196+
const ordersBatch = generateOrders(customers, batchSize);
197+
await db.collection("orders").insertMany(ordersBatch);
198+
insertedOrders += ordersBatch.length;
196199
}
197-
console.log(` Inserted ${orders.length} orders`);
200+
console.log(` Inserted ${insertedOrders} orders`);
198201

199202
// ------------------------------------------------------------------
200203
// 3. Run slow queries for ~30 seconds

0 commit comments

Comments
 (0)