Linq To Entities - Any VS First VS Exists

2019-02-21 10:06发布

I am using Entity Framework and I need to check if a product with name = "xyz" exists ...

I think I can use Any(), Exists() or First().

Which one is the best option for this kind of situation? Which one has the best performance?

Thank You,

Miguel

4条回答
贼婆χ
2楼-- · 2019-02-21 10:50

Any translates into "Exists" at the database level. First translates into Select Top 1 ... Between these, Exists will out perform First because the actual object doesn't need to be fetched, only a Boolean result value.

At least you didn't ask about .Where(x => x.Count() > 0) which requires the entire match set to be evaluated and iterated over before you can determine that you have one record. Any short-circuits the request and can be significantly faster.

查看更多
男人必须洒脱
3楼-- · 2019-02-21 10:57

One would think Any() gives better results, because it translates to an EXISTS query... but EF is awfully broken, generating this (edited):

SELECT 
CASE WHEN ( EXISTS (SELECT 
    1 AS [C1]
    FROM [MyTable] AS [Extent1]
    WHERE Condition
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT 
    1 AS [C1]
    FROM [MyTable] AS [Extent2]
    WHERE Condition
)) THEN cast(0 as bit) END AS [C1]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]

Instead of:

SELECT 
CASE WHEN ( EXISTS (SELECT 
    1 AS [C1]
    FROM [MyTable] AS [Extent1]
    WHERE Condition
)) THEN cast(1 as bit)
   ELSE cast(0 as bit) END AS [C1]
FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]

...basically doubling the query cost (for simple queries; it's even worse for complex ones)

I've found using .Count(condition) > 0 is faster pretty much always (the cost is exactly the same as a properly-written EXISTS query)

查看更多
唯我独甜
4楼-- · 2019-02-21 11:00

Any() and First() is used with IEnumerable which gives you the flexibility for evaluating things lazily. However Exists() requires List.

I hope this clears things out for you and help you in deciding which one to use.

查看更多
可以哭但决不认输i
5楼-- · 2019-02-21 11:08

Okay, I wasn't going to weigh in on this, but Diego's answer complicates things enough that I think some additional explanation is in order.

In most cases, .Any() will be faster. Here are some examples.

Workflows.Where(w => w.Activities.Any())
Workflows.Where(w => w.Activities.Any(a => a.Title == "xyz"))

In the above two examples, Entity Framework produces an optimal query. The .Any() call is part of a predicate, and Entity Framework handles this well. However, if we make the result of .Any() part of the result set like this:

Workflows.Select(w => w.Activities.Any(a => a.Title == "xyz"))

... suddenly Entity Framework decides to create two versions of the condition, so the query does as much as twice the work it really needed to. However, the following query isn't any better:

Workflows.Select(w => w.Activities.Count(a => a.Title == "xyz") > 0)

Given the above query, Entity Framework will still create two versions of the condition, plus it will also require SQL Server to do an actual count, which means it doesn't get to short-circuit as soon as it finds an item.

But if you're just comparing these two queries:

  1. Activities.Any(a => a.Title == "xyz")
  2. Activities.Count(a => a.Title == "xyz") > 0

... which will be faster? It depends.

The first query produces an inefficient double-condition query, which means it will take up to twice as long as it has to.

The second query forces the database to check every item in the table without short-circuiting, which means it could take up to N times longer than it has to, depending on how many items need to be evaluated before finding a match. Let's assume the table has 10,000 items:

  • If no item in the table matches the condition, this query will take roughly half the time as the first query.
  • If the first item in the table matches the condition, this query will take roughly 5,000 times longer than the first query.
  • If one item in the table is a match, this query will take an average of 2,500 times longer than the first query.
  • If the query is able to leverage an index on the Title and key columns, this query will take roughly half the time as the first query.

So in summary, IF you are:

  1. Using Entity Framework 4 (since newer versions might improve the query structure) Entity Framework 6.1 or earlier (since 6.1.1 has a fix to improve the query), AND
  2. Querying directly against the table (as opposed to doing a sub-query), AND
  3. Using the result directly (as opposed to it being part of a predicate), AND
  4. Either:
    1. You have good indexes set up on the table you are querying, OR
    2. You expect the item not to be found the majority of the time

THEN you can expect .Any() to take as much as twice as long as .Count(). For example, a query might take 100 milliseconds instead of 50. Or 10 instead of 5.

IN ANY OTHER CIRCUMSTANCE .Any() should be at least as fast, and possibly orders of magnitude faster than .Count().

Regardless, until you have determined that this is actually the source of poor performance in your product, you should care more about what's easy to understand. .Any() more clearly and concisely states what you are really trying to figure out, so stick with that.

查看更多
登录 后发表回答