Any method materialize object and decrease performance

Description

Any() method in linq cause object materialization. That behaviour can decrease performance if that object is big. I think that it would be better to make some Count() sql query to detect if there is any object.

I have mesured time of invoction of two methods: Any() and Count() and there are results:

code

time

474ms

31ms

Environment

None

Activity

Show:

Alex Zaytsev 
September 3, 2014 at 12:41 AM

I mean at the select clause.

Ricardo Peres 
September 3, 2014 at 12:23 AM

Yes, for collections. From the manual:

from Cat cat where exists elements(cat.kittens)

I see your point... maybe a more intelligent solution would be to translate .Any() to .Take(1).SingleOrDefault()?

Alex Zaytsev 
September 3, 2014 at 12:19 AM
(edited)

Does `exists` work in HQL?

Ricardo Peres 
September 2, 2014 at 10:54 PM

Any() should be identical to SQL EXISTS.
Count() is a COUNT.
EXISTS has better performance, because it stops if at least one element exists.
Maybe there's something else. Can you submit a test?

Details

Assignee

Reporter

Labels

Components

Affects versions

Priority

Who's Looking?

Open Who's Looking?
Created July 1, 2014 at 7:36 PM
Updated September 3, 2014 at 12:41 AM
Who's Looking?