Home > Net >  Ado slowing the linq query
Ado slowing the linq query

Time:11-30

Database using MS SQL - single table, the data quantity is probably around 1000

Interface with mention winform

Using ADO entity, the database of as mapping out

Now just do a LINQ, this form now open, slow, and all of a sudden a bunch of extra events,

On this COUNT need 2-3 minutes



Don't know what is going wrong,

CodePudding user response:

Do not use before the where judgment ToList operation; At the same time if and to the Count () and want to traverse, should not object to a query (expression) direct call these two functions, and should be aimed at a ToList results for the two operation,

For example:
 var query=the from x in xxxTablProvider where x... Select new {... }; 
Var result=query. ToList ();
If (result. Count & gt; 0)
.
The foreach (var result in x)
{
.
}


Query expression object query, it is a "delayed", every time you call the Count (), traverse, and other methods, are in fact to perform a database query and loading process, so you should at once copy the query result to the one you put the List data structure, to perform the Count () and traversal,

Likewise, if you early to perform ToList operations, so you put all the data in the table the loaded into memory and client deserialization, after execution Linq to objects in memory where the query, and not to use Linq to database provider will query the where condition to the back-end database to query, this is clearly in the table data some time is not enough, a little more

Finally, assuming that the business logic is just the judgment "whether there is accord with the where condition equipment" in the table, and do not need all show specific details on the client, then write the
 var query=(... ). FirstOrDefault (); 
is enough, and the database to return one (1) at best match record is enough, the data deserialization client memory, is one of the most waste of time steps,

CodePudding user response:

Oh, may change slightly above the variable named
 var f=(... ). FirstOrDefault (); 
in case that the query is the query before,

CodePudding user response:

In addition to this, I feel suspicious about your AsNoTracking () the design problem, you can use ADO.NET framework queries the data directly, also fee for little things, like to use 100 dollars to the big hospital to watch the small cold, do not go to cost you 5000 dollars to the clinic to see,

CodePudding user response:


I later to lambda expressions

Var query=db. Pc_info. Where (x=& gt; X.m ac_address==tempmac);

If (query. The Count ()!=0)
{... }

The normal speed is

At that time I also conveniently to write a query, thinking about the single condition, also did not think much,

Which know the database is only 1000, they became so

CodePudding user response:

CodePudding user response:

reference 4 floor cpp_1 response:
I later to lambda expressions

Var query=db. Pc_info. Where (x=& gt; X.m ac_address==tempmac);

If (query. The Count ()!=0)
{... }

The normal speed is

At that time I also conveniently to write a query, thinking about the single condition, also did not think much,

Which know the database is only 1000, they became so


As said on the second floor, as one is to load all the data into memory and iterative filtering, one is direct conditions in the database query, speed must be different,
  •  Tags:  
  • LINQ
  • Related