|
| 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: avijitkgupta |
| 6 | +ms.author: avijitkgupta |
| 7 | +ms.reviewer: gahllevy |
| 8 | +ms.service: cosmos-db |
| 9 | +ms.subservice: mongodb-vcore |
| 10 | +ms.topic: conceptual |
| 11 | +ms.date: 06/27/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +# Background indexing (Preview) |
| 15 | + |
| 16 | +Background indexing is a technique that enables a database system to perform indexing operations on a collection without blocking other queries or updates. Azure CosmosDB for Mongo vcore accepts the indexing request and asynchronously performs it in background. |
| 17 | + |
| 18 | +Azure Cosmos DB for Mongo vcore allows indexing in background with property `enableIndexBuildBackground` set to true”. All indexes would be created in background except the unique indexes, post enabling the property. |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> If anticipated data size is large enough for a collection, then it might be sensible to create index on an empty collection. |
| 22 | +
|
| 23 | +## Monitor index build |
| 24 | + |
| 25 | +We can learn about the progress of index build with command `currentOp`. |
| 26 | + |
| 27 | +```javascript |
| 28 | +db.currentOp("db_name":"<db_name>", "collection_name":"<collection_name>") |
| 29 | +``` |
| 30 | + |
| 31 | +- `db_name` is an optional parameter. |
| 32 | +- `collection_name` is optional parameter. |
| 33 | + |
| 34 | +```json |
| 35 | +// Output for reviewing build status |
| 36 | +{ |
| 37 | +inprog: [ |
| 38 | + { |
| 39 | + shard: 'defaultShard', |
| 40 | + active: true, |
| 41 | + type: 'op', |
| 42 | + opid: '10000003049:1701252500485346', |
| 43 | + op_prefix: Long("10000003049"), |
| 44 | + currentOpTime: ISODate("2024-06-24T10:08:20.000Z"), |
| 45 | + secs_running: Long("2"), |
| 46 | + command: {createIndexes: '' }, |
| 47 | + op: 'command', |
| 48 | + waitingForLock: true |
| 49 | + }, |
| 50 | + { |
| 51 | + shard: 'defaultShard', |
| 52 | + active: true, |
| 53 | + type: 'op', |
| 54 | + opid: '10000003050:1701252500499914', |
| 55 | + op_prefix: Long("10000003050"), |
| 56 | + currentOpTime: ISODate("2024-06-24T10:08:20.000Z"), |
| 57 | + secs_running: Long("2"), |
| 58 | + command: { |
| 59 | + createIndexes: 'BRInventory-Multivendor', }, |
| 60 | + indexes: [ |
| 61 | + { |
| 62 | + v:2, |
| 63 | + key: {vendorItemId: 1, vendorId: 1, itemType: 1}, |
| 64 | + name: 'compound_idx' |
| 65 | + } |
| 66 | + ], |
| 67 | + '$db': 'test' |
| 68 | + op: 'command', |
| 69 | + waitingForLock: false, |
| 70 | + progress: { |
| 71 | + blocks_done: Long("12616"), |
| 72 | + blocks_done: Long("1276873"), |
| 73 | + documents_d: Long("0"), |
| 74 | + documents_to: Long("0") |
| 75 | + }, |
| 76 | + msg: 'Building index.Progress 0.0098803875. Waiting on op_prefix: 10000000000.' |
| 77 | + } |
| 78 | + ], |
| 79 | + ok: 1 |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +We can observe the build status with values depicted in table. |
| 84 | + |
| 85 | +| Status_Value | Status | Description | |
| 86 | +|--------------|-------------|-------------| |
| 87 | +| 1 | Queued | Request is queued | |
| 88 | +| 2 | In progress | Request is picked | |
| 89 | +| 3 | Failed | Request is already failed, 3 more retries will be attempted | |
| 90 | +| 4 | Skippable | It should not be picked for processing. This should be deleted after 20 mins | |
| 91 | + |
| 92 | +## Limitations |
| 93 | + |
| 94 | +- Unique indexes cannot be created as background index. We recommend user to create unique index on empty collection first and then load the data into it. |
| 95 | +- Background index are performed in sequence over a single collection. Though background indexes on multiple collections can run in parallel. |
0 commit comments