开发者

asp.net mvc Linq going through 1 million records(proccess time)

开发者 https://www.devze.com 2023-02-05 07:12 出处:网络
Ok in this scenario if if i bring all the records at once from this query var pages=_db.Pages; and then i will process according to some scenario

Ok in this scenario if if i bring all the records at once from this query

var pages=_db.Pages;

and then i will process according to some scenario

pages.Where(m => m开发者_开发技巧.int_PostStatusId == 2) 

pages.Where(m => m.int_PostStatusId == 3)  and so on

is it really saving some processing time? or not (what really to do ?)


It's important to know that this almost certainly does not bring "all the records at once":

var pages = _db.Pages;

Assuming this is LINQ to SQL or something similar, this just creates a query - it doesn't actually fetch anything from the database.

When you use pages.Where(...) that creates a new query - again, it doesn't actually fetch anything from the database until you start reading from the query (e.g. by calling ToList). As such, it's very hard to tell how to help you optimize your code - we don't know what you're doing.

Are you trying to do anything with all the pages, or only with a subset? If you're trying group by PostStatusId, I suggest you do that explicitly rather than by fetching with lots of different filters.


Assuming that pages is a database aware linq query provider then the most efficient using
way would be to write a single query to get all the data you want:

// Create the query
var pages = 
      _db.Pages.Where(p => p.int_PostStatusId >= 2 && p.int_PostStatusId <= 100);

// Execute the query
foreach(var page in pages)
    ProcessPage(page);

This would generate SQL a bit like this:

select *
from PAGE_TABLE
where PostStatusId >=2 and PostStatusId <= 100
0

精彩评论

暂无评论...
验证码 换一张
取 消