User:Dudu9929/sandbox

Eager Loading is a design pattern that is used in software engineering to load objects from databases to applications. When eager loading is applied to a process, a single query will load all the data of the related entities from the database so that the frequency of the database access will be reduced.

Description
Eager loading is a loading mechanism designed for Object-Relational Mapping patterns. As the name suggests, eager loading loads the data "eagerly" as opposed to the default enabled lazy loading. When a database query is raised in some related entities, eager loading will load everything upon being asked, while lazy loading will load a certain object only when that object is explicitly needed.

For example, there is a list of objects  with attributes   and. The Ruby code for printing out the Year of Birth of each student and the corresponding SQL code are   When loading objects from database, this ruby code will adopt lazy loading by default. However, there is an underlying problem in this code. As the entity requests data for each object, the SQL code will execute 10+1=11 queries to the database. This is the 'N+1 problem' in Object-Relational Mapping. As compared to the first code block, this ruby code section utilizes the eager_load method to realize a simple eager loading pattern and the corresponding SQL statement is The SQL code above executes only 1 query as eager loading will load all the data in one combined large query. This code will run much faster and helps to solve the 'N+1 problem' in lazy loading.

Implementation
Eager loading can be implemented in different Object-Relational Mapping patterns such as Active Record and Microsoft Entity Framework.

Rails in Active Record
There are three different methods to achieve eager loading from database in Active Record with Ruby on Rails. For the same "student" example above:

Preload
When Preload method is used, rails uses separate queries for each database to get related data.

Eager_load
As is shown above, eager_load method can load all attributes of the object in one single query by adding a LEFT OUTER JOIN statement in corresponding SQL code.

Includes
Includes method behaves more flexibly in different situations.
 * Situation 1 In this situation, include method behaves the same as preload method with 2 separate queries for 2 databases.
 * Situation 2 In this case, includes.reference method behaves the same as eager_load method. The reference method will force the include method use one query to complete the data loading.

=== JavaScript in Microsoft Entity Framework === In Microsoft Entity Framework, eager loading can be achieved by JavaScript. Also, for the "student" case, include method can be used to implement eager loading.

Application Scenarios
=== Multiple Navigation Properties Access === When the code needs to access lots of navigation properties from the entities, it will access many addresses of the database and largely increase the number of queries. On this occasion, we can use multiple include statement with eager loading to reduce the queries and increase the operation efficiency.

Data Known at Run Time
If data needed at run time is exactly known, adopting eager loading will help load the precise datasets much faster.

Increased Network Latency
When network condition is unfavorable and large latency is detected, eager loading will typically be the choice as it requires fewer data round trips.

Higher Complexity
Assume lots of objects are defined in the code and only a few of them have to be loaded from database. To realize eager loading, code has to be written for each defined object, but for lazy loading, only the loaded objects have to be included in the code, which makes the code for eager loading much more complex in most cases.

More Resources
When an end user is using eager loading, more space has to be reserved to load a large number of objects from database. It will result in much space overhead if the data received is huge.

Larger Payload
When more than 3 entities are required in a query, much navigation properties from the entities are needed to be accessed. In this case, payload returned from database can be extremely large. To avoid the data transmission declination for the payload exceeding the limits, lazy loading can be applied in this situation.