|
| 1 | +--- |
| 2 | +title: "Improve performance using storage partitions when accessing entity data (Microsoft Dataverse) | Microsoft Docs" # Intent and product brand in a unique string of 43-59 chars including spaces |
| 3 | +description: "Learn how to improve performance when accessing non-relational entity date." # 115-145 characters including spaces. This abstract displays in the search result. |
| 4 | +ms.custom: "" |
| 5 | +ms.date: 01/25/2021 |
| 6 | +ms.reviewer: "pehecke" |
| 7 | +ms.service: powerapps |
| 8 | +ms.topic: "article" |
| 9 | +author: "JimDaly" # GitHub ID |
| 10 | +ms.author: "pehecke" # MSFT alias of Microsoft employees only |
| 11 | +manager: "sunilg" # MSFT alias of manager or PM counterpart |
| 12 | +search.audienceType: |
| 13 | + - developer |
| 14 | +search.app: |
| 15 | + - PowerApps |
| 16 | + - D365CE |
| 17 | +--- |
| 18 | +# Improve performance using storage partitions when accessing entity data |
| 19 | + |
| 20 | +An optional partition key can be specified to create a logical partition for non-relational custom entity data stored in NoSql tables of Azure heterogenous storage ([Cosmos DB](/azure/cosmos-db/introduction)). Having a partition key improves application performance for large sets of data (millions of records) by grouping data items into logical sets within a table. For example, a table containing products can be grouped logically into product categories to improve retrieval of all items within a product category. The partition key value can be a string or numeric type. Once specified, the partition key value can't be changed. |
| 21 | + |
| 22 | +When no partition key is specified, the table is the logical boundary and retrieving a single item or a set of logically related items from a large data set will not be as performant as when using a partition key. |
| 23 | + |
| 24 | +## Creating and accessing a partition |
| 25 | + |
| 26 | +A unique new partition key value must be used to create a new logical partition. The same value must be used to create additional items in the same logical partition and to retrieve, update, or delete items belonging to that logical partition. |
| 27 | + |
| 28 | +```csharp |
| 29 | +public void Run(CrmServiceClient client) |
| 30 | +{ |
| 31 | + // Create |
| 32 | + Entity entity = new Entity("new_msdyn_customer"); |
| 33 | + entity["new_firstname"] = "Monica"; |
| 34 | + entity["new_lastname"] = "Thompson"; |
| 35 | + |
| 36 | + // First use of the partition ID value during an entity Create operation |
| 37 | + // also creates the partition where that entity record is stored. |
| 38 | + entity["partitionid"] = "CustomerPartition"; |
| 39 | + Guid id = client.Create(entity); |
| 40 | + |
| 41 | + // Update |
| 42 | + UpdateRequest updateRequest = new UpdateRequest(); |
| 43 | + entity = new Entity("new_msdyn_customer", id); |
| 44 | + entity["new_firstname"] = "Cora"; |
| 45 | + //entity["new_lastname"] = "Thomas"; |
| 46 | + entity["partitionid"] = "CustomerPartition"; |
| 47 | + updateRequest.Target = entity; |
| 48 | + var updateResponse = (UpdateResponse)client.Execute(updateRequest); |
| 49 | + |
| 50 | + // Retrieve |
| 51 | + RetrieveRequest request = new RetrieveRequest(); |
| 52 | + request.ColumnSet = new ColumnSet("new_firstname"); |
| 53 | + request.Target = new EntityReference("new_msdyn_customer", id); |
| 54 | + request["partitionId"] = "CustomerPartition"; |
| 55 | + var response = (RetrieveResponse)client.Execute(request); |
| 56 | + |
| 57 | + // RetrieveMultiple |
| 58 | + RetrieveMultipleRequest retreiveMultipleRequest = new RetrieveMultipleRequest(); |
| 59 | + retreiveMultipleRequest.Query = new QueryExpression() |
| 60 | + { |
| 61 | + EntityName = "new_msdyn_customer", |
| 62 | + ColumnSet = new ColumnSet("new_firstname") |
| 63 | + }; |
| 64 | + retreiveMultipleRequest["partitionId"] = "CustomerPartition"; |
| 65 | + var retrieveResponse = (RetrieveMultipleResponse)client.Execute(retreiveMultipleRequest); |
| 66 | + |
| 67 | + // Update and insert |
| 68 | + UpsertRequest upsertRequest = new UpsertRequest(); |
| 69 | + entity = new Entity("new_msdyn_customer", id); |
| 70 | + entity["new_firstname"] = "Andre"; |
| 71 | + entity["new_lastname"] = "Lawson"; |
| 72 | + entity["partitionid"] = "CustomerPartition"; |
| 73 | + upsertRequest.Target = entity; |
| 74 | + var upsertResponse = (UpsertResponse)client.Execute(upsertRequest); |
| 75 | + |
| 76 | + // Delete |
| 77 | + DeleteRequest deleteRequest = new DeleteRequest(); |
| 78 | + deleteRequest.Target = new EntityReference("new_msdyn_customer", id); |
| 79 | + deleteRequest["partitionId"] = "CustomerPartition"; |
| 80 | + var deleteResponse = (DeleteResponse)client.Execute(deleteRequest); |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## Additional information |
| 85 | + |
| 86 | +Here are a few more details about the partition key and partition management. |
| 87 | + |
| 88 | +- The key value must be unique in the environment. |
| 89 | +- A partition is limited to 20 GB of data, and there is no method available to check the partition's current size. |
| 90 | +- There is no defined limit to the number of partitions you can allocate in an environment. |
| 91 | +- Partition allocation is automatic. Specifying a unique partition key during a Create operation creates a partition. When all data has been deleted from the partition, the partition is automatically deleted. |
| 92 | +- There is no method available to rename a key. |
| 93 | +- Presently, only the Create, Update, Retrieve, and Delete entity operations support storage partitioning. |
| 94 | + |
| 95 | +### See Also |
| 96 | + |
| 97 | +[Passing optional parameters with a request](use-messages.md#passing-optional-parameters-with-a-request) |
| 98 | +[Create entities using the Organization Service](entity-operations-create.md) |
| 99 | +[Retrieve an entity using the Organization Service](entity-operations-retrieve.md) |
| 100 | +[Update and Delete entities using the Organization Service](entity-operations-update-delete.md) |
| 101 | +[Partitioning and horizontal scaling in Azure Cosmos DB](/azure/cosmos-db/partitioning-overview) |
0 commit comments