MVC Read Agility

Asked

Viewed 35 times

2

I have an application in MVC 4 that has no addition/deletion/editing operation, it only reads and provides data (as filters).

The data basically comes from 4 views.

  • Being the first of about 1,500 records - instant read via query
  • The second of about 15 thousand - read about 14 seconds via query
  • The third of about 30 records - instant read via query
  • The fourth of about 1,300 records - instant read via query

Each view has its respective model. Consecutively, in each PartialView necessary, call the necessary domain.

Previously worked in a similar way to this example:

var bdModelA = new SegundaViewEF(Contexto); // View da query de 14 segundos
var Todos = bdModelA.ListarTodos();
var Quantidade = Todos.Count();
var NF = Todos.Where(x => x.Tipo == "NF");
var NCC = Todos.Where(x => x.Tipo == "NCC");
var QuantidadeNF = NF.Count();
var QuantidadeNCC = NCC.Count();

foreach(var Item in NF){
// codigo aqui
}

foreach(var Item in NCC){
// codigo aqui
}

I noticed slowness, I started working like this:

var bdModelA = new SegundaViewEF(Contexto);
var Todos = bdModelA.ListarTodos();
var QuantidadeNF = 0;
var QuantidadeNCC = 0;
var Quantidade = 0;

foreach(var Item in Todos){
Quantidade++;
if(Tipo == "NF"){
QuantidadeNF++;
//Codigo aqui
}else if(Tipo =="NCC"){
QuantidadeNCC++;
//Codigo aqui
}
}

Continued slow.

I would like to know whether the best practice is the one described in the first example or the second and whether there is any limitation of the MVC in relation to reading view, because I’ve always worked with table reading.

I call from one to three different domains in each of my Partialview, being that of the largest query (14 seconds), I call at least 4 times. I own about 7 to 8 Partialview.

My application takes about 8 minutes to open (connection via VPN) and 5 minutes via direct connection.

What can I do to improve?

1 answer

1


I would like to know if the best practice is the one described in the first example or the second and if there is any limitation of the MVC in relation to view reading, because I have always worked with table reading.

Neither. It is the repetition of the already manjado error of encapsulating the context. It won’t work right because then you remove the code options to create queries already optimized for each query you will make. The way it is, all the readings will be TABLE SCAN.

What would I do:

// var bdModelA = new SegundaViewEF(Contexto);
// var Todos = bdModelA.ListarTodos();
// var Quantidade = Todos.Count();
var Todos = Contexto.SegundaViewEF.AsQueryable();
var NF = Todos.Where(x => x.Tipo == "NF").ToList();
var NCC = Todos.Where(x => x.Tipo == "NCC").ToList();
var QuantidadeNF = NF.Count();
var QuantidadeNCC = NCC.Count();

foreach(var Item in NF){
// codigo aqui
}

foreach(var Item in NCC){
// codigo aqui
}

What can I do to improve?

There’s something else that can be done: Install the Miniprofiler.Mvc4 (serves for MVC5 as well) and check the queries.

See how to configure the project here.

  • Okay, thank you Gypsy. In the MVC work with view or table, in the same ? Do you think my application was so slow for the reasons cited?

  • The Entity Framework does not analyze whether the object is a table or a view. It simply executes the database commands as a normal user would. About your application being slow, on her side what was wrong, I mentioned. On the view side, you will have to study the query plan and see the cost of each operation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.