Which one is faster SingleOrDefault or FirstOrDefault

Generally, I don't see too much performance problem whether you use Single Or First. However if you have a table with lots of columns like 300 you might notice slight improvement if you use FirstOrDefault. Before we go deeper into the performance difference let’s understand how they are semantically different.

When you use FirstOrDefault, you will get the first item that matches a given criteria. When you use SingleOrDefault, you are saying that there can be only one value that matches the criteria. If there are more then 1 value that matches the criteria, throw an exception.

To see clear difference let’s query against Categories table with FirstOrDefault and SingleOrDefault. Code below shows the query along with its sql translation.

var beverage1 = db.Categories.SingleOrDefault(c => c.CategoryName == "Beverage");
image

 

Query using FirstOrDefault

var beverage1 = db.Categories.FirstOrDefault(c => c.CategoryName == "Beverage");                    

image

I think when you look at the sql translation it would make more sense why FirstOrDefault performs better.. In the SingleOrDefault, EF actually sends top(2) query which retrieve top 2 records that match the criteria specified for SingleOrDefault. This means that two rows have to be retrieved from the database and materialized. On the .net side, EF checks to see if the record count is greater then 1, then throw an exception. What’s worse is, it actually materializes two records into entities and then throws an exception. 

In the code below, I am retrieving products with categoryid equal 4 using SingleOrDefault. Since this code will throw exception, i wrap it inside of try catch block. Inside the exception, i query the object context to see how many entities got tracked and sure enough the result returned was 2. I think the future version should improve on this behavior where it really does not need to materialize two rows to entities before throwing an exception.

In the case of FirstOrDefault, only one row is retrieved from the database so it performs slightly better then SingleOrDefault. Normally such a small difference is hardly noticeable but on the current application i am working on, there are several tables which have more then 200 columns(don’t ask me why), you can actually see the difference between SingleOrDefault and FirstOrDefault.

Just watch out for this gotcha!

1 Comment

  • I don't think of FirstOrDefault and SingleOrDefault methods as alternatives for the reason you stated above, they behave differently.
    So, FirstOrDefault should be used when you just want to get the first record satisfying the given condition, but if the business case requires you to have one-or-zero records satisfying the condition you should use SingleOrDefault (or just use Single() and catch the exception).

Comments have been disabled for this content.