Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using EF5 with the MoreLinq extenstion, while testing my program in production (very big database), I found out that the line:

var x = db.TheBigTable.MaxBy(x => x.RecordTime);

Takes very long time (RecordTime is a non-indexed datetime)

Is that because MaxBy always runs on the client side (and firstly gets ALL records from the database)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
353 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is the signature of the MaxBy extension method:

public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source,
    Func<TSource, TKey> selector)
{
    return source.MaxBy(selector, Comparer<TKey>.Default);
}

It returns the maximal element (based on the given projection) of an IEnumerable<T>, not an IQueryable<T>. So the results of the query db.TheBigTable are indeed all loaded into memory first, and then they are iterated to find the maximum.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...