Is ORM (Object-Relational mapping) the root cause of performance problem on web sites use them? [closed]
Most of MVC based web sites using ORM to map between the model and corresponding database entities. What's sitting in-between are the SQL/non-SQL queries that either auto-gen'd or provided by developers. The problem with that approach is that - 1. database I/O is the bottle-neck for real-time traffic 2. no easy to plug in mechanisms to do the caching. 3. scalability. etc
I would like to hear your opinions on that.
I would argue that it depends - ORMs can cause performance issues in some cases, but to blame them generally for all performance issues probably isn't accurate.
I see two main performance pitfalls for ORM usage:
1) Getting too much data back
In most cases, you are getting more data back than you really need. Typically, this isn't a huge problem, but if the model is mapped improperly, you can sometimes get large graphs of data when you only wanted one little piece.
2) Being unable to tune the SQL generated by the ORM
Sometimes ORMs don't generate the most optimal SQL for certain situations, and it is difficult or impossible to modify it. I've mitigated this in the past by being relatively quick to bypass the ORM in situations where some SQL tuning will net large performance gains.
To your other point - that ORMs make it difficult to utilize caching - this is actually a source of (sometimes significant) performance gains for many ORMs, since most ORMs typically include some sort of built-in caching. For example, Hibernate has a first level cache (the session/persistence context) and can be easily configured to use a second-level cache as well.
It is probably too much of a generalization to say that an ORM is the root cause of poor performance on any web site that uses it; performance is more complex than that. If you are using a relational database, the best approach is probably to start out using an ORM, then if performance is not good enough, use a profiler to find the real bottlenecks and address them. You may end up using an ORM in some places and hand-tuned SQL in others.
精彩评论