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?
It’s called a projection and just translates into a
SELECT column1, column2, ...in SQL:Instead of
a => new { ... }(creates a list of “anonymous” objects) you can also use a named helper class (or “view model”):a => new MyViewModel { ... }that contains only the selected properties (but you can’t usea => new Article { ... }as an entity itself).For such a projection you don’t need
AsNoTracking()because projected data are not tracked anyway, only full entity objects are tracked.Instead of using
Containsthe more common way is to useWherelike:This would select only the articles that are not older than a year. The
Whereis just translated into a SQLWHEREstatement and the filter is performed in the database (which is as fast as the SQL query is, depending on table size and proper indexing, etc.). Only the result of this filter is loaded into memory.Edit
Refering to your comment below:
Don’t confuse
IEnumerable<T>.Contains(T t)withstring.Contains(string subString). The answer you have linked in your question talks about the first version ofContains. If you want to search for articles that have the string"keyword"in the text body you need the secondContainsversion:This will translate into something like
WHERE Body like N'%Entity Framework%'in SQL. The answer about the poor performance ofContainsdoesn’t apply to this version ofContainsat all.