Lets say I have a model, Article
that has a large amount of columns and the database contains more than 100,000 rows. If I do something like var articles = db.Articles.ToList()
it is retrieving the entire article model for each article in the database and holding it in memory right?
So if I am populating a table that only shows the date of the entry and it's title is there a way to only retrieve just these columns from the database using the entity framework, and would it be more efficient?
According to this,
There is a cost required to track returned objects in the object context. Detecting changes to objects and ensuring that multiple requests for the same logical entity return the same object instance requires that objects be attached to an ObjectContext instance. If you do not plan to make updates or deletes to objects and do not require identity management , consider using the NoTracking merge options when you execute queries.
it looks like I should use NoTracking
since the data isn't being changed or deleted, only displayed. So my query now becomes var articles = db.Articles.AsNoTracking().ToList()
. Are there other things I should do to make this more efficient?
Another question I have is that according to this answer, using .Contains(...)
will cause a large performance drop when dealing with a large database. What is the recommended method to use to search through the entries in a large database?