|
| 1 | +--- |
| 2 | +title: Background indexing |
| 3 | +titleSuffix: Background indexing on Azure Cosmos DB for MongoDB vCore |
| 4 | +description: Background indexing to enable non-blocking operation during index creation |
| 5 | +author: avijitgupta |
| 6 | +ms.author: avijitgupta |
| 7 | +ms.reviewer: gahllevy |
| 8 | +ms.service: cosmos-db |
| 9 | +ms.subservice: mongodb-vcore |
| 10 | +ms.topic: conceptual |
| 11 | +ms.date: 07/01/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +# Background indexing (Preview) |
| 15 | + |
| 16 | +[!INCLUDE[MongoDB vCore](~/reusable-content/ce-skilling/azure/includes/cosmos-db/includes/appliesto-mongodb-vcore.md)] |
| 17 | + |
| 18 | +Background indexing is a technique that enables a database system to perform indexing operations on a collection without blocking other queries or updates. Azure Cosmos DB for Mongo vcore accepts the background indexing request and asynchronously performs it in background. |
| 19 | + |
| 20 | +If working with smaller tiers or workloads with higher I/O needs, it's recommended to predefine indexes on empty collections and avoid relying on background indexing. |
| 21 | + |
| 22 | +> [!NOTE] |
| 23 | +> Background indexing is a Preview feature. Enabling this feature requires raising a support request. |
| 24 | +
|
| 25 | +> [!IMPORTANT] |
| 26 | +> It is advised to create `unique` indexes on an empty collection as those are created in foreground, which results in blocking of reads and writes. |
| 27 | +> |
| 28 | +> It is advised to create indexes based on query predicates beforehand, while the collection is still empty. It prevents resource contention if pushed on read-write heavy large collection. |
| 29 | +
|
| 30 | +## Monitor index build |
| 31 | + |
| 32 | +We can learn about the progress of index build using command `currentOp()`. |
| 33 | + |
| 34 | +```javascript |
| 35 | +db.currentOp("db_name":"<db_name>", "collection_name":"<collection_name>") |
| 36 | +``` |
| 37 | + |
| 38 | +- `db_name` is an optional parameter. |
| 39 | +- `collection_name` is optional parameter. |
| 40 | + |
| 41 | +```javascript |
| 42 | +// Output for reviewing build status |
| 43 | +{ |
| 44 | +inprog: [ |
| 45 | + { |
| 46 | + shard: 'defaultShard', |
| 47 | + active: true, |
| 48 | + type: 'op', |
| 49 | + opid: '10000003049:1701252500485346', |
| 50 | + op_prefix: Long("10000003049"), |
| 51 | + currentOpTime: ISODate("2024-06-24T10:08:20.000Z"), |
| 52 | + secs_running: Long("2"), |
| 53 | + command: {createIndexes: '' }, |
| 54 | + op: 'command', |
| 55 | + waitingForLock: true |
| 56 | + }, |
| 57 | + { |
| 58 | + shard: 'defaultShard', |
| 59 | + active: true, |
| 60 | + type: 'op', |
| 61 | + opid: '10000003050:1701252500499914', |
| 62 | + op_prefix: Long("10000003050"), |
| 63 | + currentOpTime: ISODate("2024-06-24T10:08:20.000Z"), |
| 64 | + secs_running: Long("2"), |
| 65 | + command: { |
| 66 | + createIndexes: 'BRInventory', }, |
| 67 | + indexes: [ |
| 68 | + { |
| 69 | + v:2, |
| 70 | + key: {vendorItemId: 1, vendorId: 1, itemType: 1}, |
| 71 | + name: 'compound_idx' |
| 72 | + } |
| 73 | + ], |
| 74 | + '$db': 'test' |
| 75 | + op: 'command', |
| 76 | + waitingForLock: false, |
| 77 | + progress: { |
| 78 | + blocks_done: Long("12616"), |
| 79 | + blocks_done: Long("1276873"), |
| 80 | + documents_d: Long("0"), |
| 81 | + documents_to: Long("0") |
| 82 | + }, |
| 83 | + msg: 'Building index.Progress 0.0098803875. Waiting on op_prefix: 10000000000.' |
| 84 | + } |
| 85 | + ], |
| 86 | + ok: 1 |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Limitations |
| 91 | + |
| 92 | +- Unique indexes can't be created in the background. It's best to create them on an empty collection and then load the data. |
| 93 | +- Background indexing is performed sequentially within a single collection. However, the number of simultaneous index builds on different collections is configurable (default: 2). |
| 94 | + |
| 95 | +## Next Steps |
| 96 | + |
| 97 | +> [!div class="nextstepaction"] |
| 98 | +> [Best practices](how-to-create-indexes.md) |
0 commit comments