6
I have a performance problem in one method, in addition to causing the full use of the CPU
server that method takes a long time to run.
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 method dbContexto.SaveChanges()
or dbContext.SaveChangesAsync()
, takes more than 10 minutes to finish, causing timeout
.
I searched the subject and found some options as for example this response from Stackoverflow in English, I left saving 400 in 400 records and created a new instance of dbContext
, but it did not help the performace, it still took more than 10 minutes.
I would like to know how to make this method run faster, I have no idea where to go, would be happy with any hint or explanations.
Related: https://answall.com/questions/133553/howto run o-sqlbulkcopy
– novic
Related: https://answall.com/questions/92884/erro-ao-insertion-dados-no-banco-ef-6
– novic