Understanding lazy loading versus eager loading
When we make a query to load the data from a database, there is a huge possibility that this operation might define the response time of the applications we build. This happens mainly when there is a lot of data that needs to be loaded and the application waits on the database to return all those rows and columns back to it.
Such operations may take some time, ranging from a few milliseconds to more than 10 seconds, depending on how much data is being queried from the database. The question here is, can we optimize this to improve the response times of our application?
The answer to this lies in the use of SQL relationships and ORM layer loading techniques. While relationships can help us to define how the two models relate to each other, loading techniques define how relationships are retrieved by the ORM. When a lot of data needs to be loaded, this can prove to be of great help, by not only providing a mechanism through which we can defer the loading of data of the relationships until they are required, but by also saving quite a lot in terms of memory footprint of the application. So, let's take a look at these techniques.