maztt
maztt

Reputation: 12294

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

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 ?)

Upvotes: 0

Views: 597

Answers (2)

Andrew Skirrow
Andrew Skirrow

Reputation: 3451

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500735

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.

Upvotes: 3

Related Questions