Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List and Linq To Sql Performance Issue

I Have One Table (My Sql) with 2 millions records and one List with 100 Records. I have List Except lamda expression for finding all those Urls that is in List but Not in Table.

Now Issue is that it's taking lot of time around 5 mins. I am working in powerful VPS and code and database in same server.

Please suggest me All possible way to increase the performance of linq to sql and linq to entity.

My Code Is`return

Urls.Except(DbContext.postedurllists.Select(crawl => crawl.PostedUrl).ToList()).ToList();`

Where Urls Is List Which Contain 100 Urls And postedurllists is a table that contains 2 Millions record. Thanks

like image 973
Pankaj Mishra Avatar asked Sep 23 '11 07:09

Pankaj Mishra


People also ask

Which is faster SQL or LINQ?

Stored procedures are faster as compared to LINQ query since they have a predictable execution plan and can take the full advantage of SQL features. Hence, when a stored procedure is being executed next time, the database used the cached execution plan to execute that stored procedure.

Should I avoid LINQ for performance reasons?

Summary. In conclusion although it's true that there are some situations where it does make sense to avoid LINQ for performance reasons, it's not nearly as frequent as you might think, and there are often much better approaches to speeding up your code.

Is LINQ good for performance?

Conclusion. It would seem the performance of LINQ is similar to more basic constructs in C#, except for that notable case where Count was significantly slower. If performance is important it's crucial to do benchmarks on your application rather than relying on anecdotes (including this one).

Is LINQ to SQL deprecated?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.


1 Answers

You're currently pulling all of the URLs from the database. That's not a good idea. Instead, I would suggest pulling the intersection from the database by effectively passing your Urls list into the database, and doing an except based on the results:

var commonUrls = DbContext.postedurllists
                          .Select(c => c.PostedUrl)
                          .Where(url => Urls.Contains(url))
                          .ToList();

var separateUrls = Urls.Except(commonUrls);
like image 157
Jon Skeet Avatar answered Sep 27 '22 18:09

Jon Skeet