Я использую Azure Cosmos DB SDK (3.0) для операций CRUD. Когда я пытаюсь вставить 8 000–10 000 записей одновременно, это занимает почти 3-4 минуты.
Вот мой код:
public async Task<ResultDto> HandleAsync(EnableOrDisableSubscriptionCommand command, ILogger logger)
{
logger.Info("Started EnableOrDisableSubscriptionCommand ", nameof(EnableOrDisableSubscriptionCommand));
if (command.UiNotifications.Any())
{
await AddSubscription(command, SubscriptionAction.UiNotification, command.UiNotifications);
logger.Info("Added UI notification subscriptions");
}
if (command.EmailNotifications.Any())
{
await AddSubscription(command, SubscriptionAction.Email, command.EmailNotifications);
logger.Info("Added Email notification subscriptions");
}
return new ResultDto { ResultType = ResultType.Success, Message = $"User {command.UserId} SubscriptionStatus" };
}
private async Task AddSubscription(EnableOrDisableSubscriptionCommand command, SubscriptionAction subscriptionAction, IList<int> notificationCategoryTypes)
{
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)))
{
await _repository.Create(subscriptionAction, notificationType,
payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, accountNumber, command.UserRole,
command.UserId);
}
}
else
{
await _repository.Create(subscriptionAction, notificationType,
payerAccountSubscriptions.ColCoId, payerAccountSubscriptions.PayerNumber, null, command.UserRole,
command.UserId);
}
}
}
}
}
Метод создания репозитория подписок:
public async Task 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 }
};
await CreateItemAsync(subscriptionBase);
}
Generi c Репозиторий:
public async Task<ItemResponse<T>> CreateItemAsync(T item)
{
return await _container.CreateItemAsync<T>(item);
}
Из-за этой проблемы My Http Trigger Azure Функция, возвращающая System.OutOfMemoryException.
Как я могу улучшить это?