Thursday, September 8, 2011

my thoughts on ORM and database performance

Some people (especially junior developers) who just learnedORM technologies did not understand that sometimes, it is important to write SQL instead of traversing the objects. Yes it is simpler to just get the object from the database and traverse it but depending on the 'loading' mode, you could be writing a program with bad performance issue.

On eager fetching, ALL the related objects will be fetched. You don't really want to do this all the time as it is a resource consuming process.

On lazy fetching, only the object that is directly specified in the query is fetched. Only when we want to traverse along the relation, then only it will load the related objects. This will cause multiple queries to the database.

So, in my humble opinion, it is better to directly query (using the ORM's query language) for the objects that you need instead of being lazy and only depend on the ORM's fetching mechanism. Probably a bit hard to get it right if you aren't familiar with multiple table joins but seriously it is worth the time and yields better application performance.

Conclusion: a good software developer should not neglect learning database related aspect of the field.


No comments: