Sunday 4 November 2012

Deferred vs Immediate Query Execution in LINQ


Deferred vs Immediate Query Execution in LINQ

Posted by: Suprotim Agarwal , on 8/12/2011, in Category LINQ 
Views: 45390
Abstract: In LINQ, queries have two different behaviors of execution: immediate and deferred. In this article, we will take a quick overview of how Deferred query execution and Immediate Query Execution works in LINQ
In LINQ, queries have two different behaviors of execution: immediate and deferred. In this article, we will take a quick overview of how Deferred query execution and Immediate Query Execution works in LINQ
If you do LINQ programming, do check my article 50 LINQ Examples, Tips and How To's

Deferred Query Execution

To understand Deferred Query Execution, let’s take the following example which declares some Employees and then queries all employees with Age > 28:
image
LINQ Deferred Execution
OUTPUT: Jack, Rahul
Looking at the query shown above, it appears that the query is executed at the point where the arrow is pointing towards. However that’s not true. The query is actually executed when the query variable isiterated over, not when the query variable is created. This is called deferred execution.
Now how do we prove that the query was not executed when the query variable was created? It’s simple. Just create another Employee instance after the query variable is created
LINQ Deferred execution
Notice we are creating a new Employee instance after the query variable is created. Now had the query been executed when the query variable is created, the results would be the same as the one we got earlier, i.e. only two employees would meet the criteria of Age > 28. However the output is not the same
OUTPUT: Jack, Rahul, Bill.
What just happened is that the execution of the query was deferred until the query variable was iterated over in a foreach loop. This allows you to execute a query as frequently as you want to, like fetching the latest information from a database that is being updated frequently by other applications. You will always get the latest information from the database in this case.

Immediate Query Execution

You can also force a query to execute immediately, which is useful for caching query results. Let us say we want to display a count of the number of employees that match a criteria.
LINQ Immediate Execution
In the query shown above, it order to count the elements that match the condition, the query must be executed, and this is done automatically when Count( ) is called. So adding a new employee instanceafter the query variable declaration does not have any effect here, as the query is already executed. The output will be 2, instead of 3.
The basic difference between a Deferred execution vs Immediate execution is that Deferred execution of queries produce a sequence of values, whereas Immediate execution of queries return a singleton value and is executed immediately. Examples are using Count(), Average(), Max() etc.
Note: To force immediate execution of a query that does not produce a singleton value, you can call the ToList(), ToDictionary() or the ToArray() method on a query or query variable. These are called conversion operators which allow you to make a copy/snapshot of the result and access is as many times you want, without the need to re-execute the query.
Hopefully novice developers will now know the basic difference between Deferred and Immediate Execution of queries.
I hope you liked the article and I thank you for viewing it.

1 comment:

  1. When we measure performance, we need to see the time of execution the first time as well as subsequent times. At least 8 recordings are needed so that any kind of .NET runtime performance is averaged out. There are two important points we can conclude from the experiment:

    We need to excuse the first reading as there can be a lot of .NET Dramework object initialization. It can lead to a lot of wrong conclusions as there is a lot of noise associated with the first run.
    The subsequent readings have the real meat difference. The average difference between them is 5 times. In other words, a LINQ query executed using no compilation is 5 ms slower than compiled LINQ queries.

    ReplyDelete

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...