I have a performance problem in one method, in addition to causing full use of CPU
of the server this method takes a long time to execute.
internal async void NotificacaoIosTodos(string titulo, int posicao, int unidadeId)
{
try
{
_contexto.Configuration.AutoDetectChangesEnabled = false;
_contexto.Configuration.ValidateOnSaveEnabled = false;
var unidade = _contexto.Unidade.FirstOrDefault(l => l.UnidadeId == unidadeId);
var pessoas = _contexto.PessoaUnidade.Include(l => l.Pessoa)
.Where(l => l.Pessoa.Dispositivo == (int)enumDispositivo.Ios && l.UnidadeId == unidadeId)
.ToList();
var quantidadeAdicionada = 0;
Notificacao notificacao = new Notificacao { Posicao = posicao };
_contexto.Notificacao.Add(notificacao);
foreach (PessoaUnidade pessoa in pessoas)
{
PessoaNotificacao pessoaNotificacao = new PessoaNotificacao
{
Visualizado = false,
PessoaUnidade = pessoa,
Notificacao = notificacao
};
_contexto.PessoaNotificacao.Add(pessoaNotificacao);
}
await _contexto.SaveChangesAsync();
}
catch (Exception e)
{
throw;
}
}
The problem is that I have to insert approximately 1700
records at once, when I run the dbContexto.SaveChanges()
or dbContext.SaveChangesAsync()
method, it takes more than 10 minutes to finish, causing timeout
.
I looked it up and found some options such as this answer from StackOverflow in English , I left saving from 400 in 400 records and created a new instance of dbContext
, but it did not help in the performace, it still took more than 10 minutes.
I would like to know how I could make this method run faster, I have no idea where to go, I would be happy with any tips or explanations.