Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORM vs traditional database query, which are their fields?

Tags:

ORM seems to be a fast-growing model, with both pros and cons in their side. From Ultra-Fast ASP.NET of Richard Kiessig (http://www.amazon.com/Ultra-Fast-ASP-NET-Build-Ultra-Scalable-Server/dp/1430223839/ref=pd_bxgy_b_text_b):

"I love them because they allow me to develop small, proof-of-concept sites extremely quickly. I can side step much of the SQL and related complexity that I would otherwise need and focus on the objects, business logic and presentation. However, at the same time, I also don't care for them because, unfortunately, their performance and scalability is usually very poor, even when they're integrated with a comprehensive caching system (the reason for that becomes clear when you realize that when properly configured, SQL Server itself is really just a big data cache"

My questions are:

  • What is your comment about Richard's idea. Do you agree with him or not? If not, please tell why.

  • What is the best suitable fields for ORM and traditional database query? in other words, where you should use ORM and where you should use traditional database query :), which kind/size... of applications you should undoubtedly choose ORM/traditional database query

Thanks in advance

like image 627
Quan Mai Avatar asked Jul 29 '10 07:07

Quan Mai


People also ask

Which one is better ORM or normal query?

Hands-On Management. When it comes to hands-on management, SQL is higher than ORM. It is because of the human expertise involved in running queries in data management and retrieval. It is important to know how to use SQL in order to maximize the benefits and performance of the database.

What is the difference between ORM and query builder?

If you are more aware of efficiency rather than ease of development, go for query builder. If you dealing with one entity go for ORM, (Eloquent). If you dealing with multiple entities it's better to deal with query builder. If you are new to mysql or your application is not very complex, definitely choose ORM.

Is ORM faster than raw SQL queries True or false?

Performance. Since ORMs usually execute SQL queries under the hood, they can only hope to match the performance of an equivalent optimized SQL query; in practice, though, ORMs are often much slower. Nested fetch operations are typically split into a set of simpler, serially-executed SQL queries.

What are ORM queries?

Object-Relational Mapping (ORM) is a technique that lets you query and manipulate data from a database using an object-oriented paradigm. When talking about ORM, most people are referring to a library that implements the Object-Relational Mapping technique, hence the phrase "an ORM".


1 Answers

I can't agree to the common complain about ORMs that they perform bad. I've seen many plain-SQL applications until now. While it is theoretically possible to write optimized SQL, in reality, they ruin all the performance gain by writing not optimized business logic.

When using plain SQL, the business logic gets highly coupled to the db model and database operations and optimizations are up to the business logic. Because there is no oo model, you can't pass around whole object structures. I've seen many applications which pass around primary keys and retrieve the data from the database on each layer again and again. I've seen applications which access the database in loops. And so on. The problem is: because the business logic is already hardly maintainable, there is no space for any more optimizations. Often when you try to reuse at least some of your code, you accept that it is not optimized for each case. The performance gets bad by design.

An ORM usually doesn't require the business logic to care too much about data access. Some optimizations are implemented in the ORM. There are caches and the ability for batches. This automatic (and runtime-dynamic) optimizations are not perfect, but they decouple the business logic from it. For instance, if a piece of data is conditionally used, it loads it using lazy loading on request (exactly once). You don't need anything to do to make this happen.

On the other hand, ORM's have a steep learning curve. I wouldn't use an ORM for trivial applications, unless the ORM is already in use by the same team.

Another disadvantage of the ORM is (actually not of the ORM itself but of the fact that you'll work with a relational database an and object model), that the team needs to be strong in both worlds, the relational as well as the oo.

Conclusion:

  • ORMs are powerful for business-logic centric applications with data structures that are complex enough that having an OO model will advantageous.
  • ORMs have usually a (somehow) steep learning curve. For small applications, it could get too expensive.
  • Applications based on simple data structures, having not much logic to manage it, are most probably easier and straight forward to be written in plain sql.
  • Teams with a high level of database knowledge and not much experience in oo technologies will most probably be more efficient by using plain sql. (Of course, depending on the applications they write it could be recommendable for the team to switch the focus)
  • Teams with a high level of oo knowledge and only basic database experience are most probably more efficient by using an ORM. (same here, depending on the applications they write it could be recommendable for the team to switch the focus)
like image 174
Stefan Steinegger Avatar answered Oct 21 '22 08:10

Stefan Steinegger