0

I want to use Container.CreateTransactionlBatch for bulk insert operation. Currently I am doing this with container.CreateItemAsync method which is taking more time.

Is it possible to replace container.CreateItemAsync with Container.CreateTransactionalBatch?

private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, SubscriptionAction subscriptionAction, IList<int> notificationCategoryTypes)
        {
            List<Task> bulkOperations = new List<Task>();
            foreach (var notificationCategory in notificationCategoryTypes)
            {
                var notificationTypes = Utility.GetNotificationTypes((NotificationCategoryType)notificationCategory);

                foreach (var notificationType in notificationTypes)
                {
                    foreach (var payerAccountSubscriptions in command.Subscriptions)
                    {
                        if (payerAccountSubscriptions.AccountNumbers?.Any() ?? false)
                        {
                            foreach (var accountNumber in payerAccountSubscriptions.AccountNumbers.Where(a => !string.IsNullOrEmpty(a)))
                            {
                                bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                      payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, accountNumber, command.UserRole,
                                      command.UserId));
                            }
                        }
                        else
                        {
                            bulkOperations.Add(_repository.Create(subscriptionAction, notificationType,
                                payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, null, command.UserRole,
                                command.UserId));

                        }
                    }
                }
            }
            await Task.WhenAll(bulkOperations);
        }

 public async Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
            int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
        {
            var eventType = Utility.GetEventType(notificationType);

            var subscriptionBase = new Subscription
            {
                Id = Guid.NewGuid(),
                IsActive = true,
                Action = subscriptionAction,
                ActionDesc = subscriptionAction.ToString(),
                Version = (int)SubscriptionVersion.V2,
                NotificationType = notificationType,
                NotificationTypeDesc = notificationType.ToString(),
                EventType = eventType,
                EventTypeDesc = eventType.ToString(),
                ColCoId = colCoId,
                PayerNumber = payerNumber,
                AccountNumber = accountNumber,
                CardId = cardId,
                DistributionGroups = new List<string> { userRole.ToString() },
                DistributionUserIds = new List<string> { userId }
            };
            return await CreateItemAsync(subscriptionBase);
        }

public async Task<ItemResponse<T>> CreateItemAsync(T item)
        {
            return await _container.CreateItemAsync<T>(item);
        }
1
  • Are you trying to do bulk inserts or are you trying to do a transactional batch? These are not the same thing. Commented Aug 6, 2020 at 15:21

1 Answer 1

2

There are two different things.

By the code you are sharing it looks like you are trying to use Bulk? If so, you need to make sure your CosmosClient instance is created with a CosmosClientOptions that has Bulk on:

CosmosClientOptions options = new CosmosClientOptions() { AllowBulkExecution = true };
CosmosClient cosmosclient = new CosmosClient(connectionString, options);

In your code also, you might want to remove the await from the other methods, and just return the Task, for example:

public Task<ItemResponse<Subscription>> Create(SubscriptionAction subscriptionAction, NotificationType notificationType,
    int colCoId, string payerNumber, string accountNumber, UserRole userRole, string userId, string cardId = null)
{
    // ... OTHER CODE
    return CreateItemAsync(subscriptionBase);
}

public Task<ItemResponse<T>> CreateItemAsync(T item)
{
    return _container.CreateItemAsync<T>(item);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! for your reply. Now I am able insert 12,000 items and its take more than 3 mins to insert. How Can I improve the insertion performance. Could you please suggest.
Verify the provisioned throughput (RU/s) is enough to process the volume of data you want to push through, you can check if you are getting throttles (429s) on the metrics. Also verify the CPU on the instance is not spiking up (the bottleneck in that case could be the local CPU). Please mark the answer if it helped solve the initial issue.
Also make sure you are using the latest SDK version, we have performance improvements on each release.
I have set 3000 Throughtput value for the subscription container, but still i m getting "Response status code does not indicate success: TooManyRequests (429); Substatus: 3200;" in Azure Function logs
How much Throughput value i need to set to insert/delete 10-12k items in bulk?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.