Disadvantages of Relational Object Mapping

I am a fan of ORM - Object Relational Mapping, and I have been using it with Rails for the past year and a half. Before that, I use to write raw queries using JDBC and create a database that does the hard work using stored procedures. With ORM, at first I was pleased to do things like coach.manager and manager.coaches , which were very simple and easy to read.

But over time, in many associations crawled up, and I ended up making abcd , which shot in all directions, backstage. With rails and ruby, the garbage collector went crazy and took a crazy time to load a very complex page that included relatively less data. I had to replace this ORM style code with a simple stored procedure, and the result I saw was huge. A page that takes 50 seconds to load takes just 2 seconds.

With that huge difference, should I continue to use ORM? It is very clear that it has serious overhead compared to a raw request.

In general, what are the common problems of using an ORM structure such as Hibernate, ActiveRecord?

+6
source share
6 answers

I think that you have already identified the main compromise associated with ORM software. Each time you add a new level of abstraction that tries to provide a generalized implementation of what you did manually, there will be some loss in performance / efficiency.

As you noted, moving multiple relationships, such as abcd , can be inefficient, as most ORM programs will perform an independent database query for each . along the way. But I'm not sure that you should completely eliminate ORM. Most ORM solutions (or at least, of course, Hibernate) allow you to specify custom queries where you can return exactly what you want in a single database operation. It should be about as fast as your dedicated SQL.

Actually, the problem is understanding how the ORM layer works behind the scenes, and understanding that while something like abcd easy to write, what it makes the ORM layer to do as it is being evaluated is not. As a rule, I always take the simplest approach to getting started, and then write optimized queries in areas where it makes sense /, where it is obvious that a simple approach will not scale.

+5
source

ORM is just a tool. If you do not use it correctly, you will get poor results.

Nothing prevents you from using selected HQL queries / criteria, with joins or retrieval projections, to return the information displayed on your page as multiple queries as possible. This will take more or less the same time as the allocated SQL queries.

But, of course, if you just get everything by identifier and navigate through your objects, not understanding how many requests it generates, this will lead to a long loading time. The key is to know exactly what the ORM is doing behind the scenes and decide if it is suitable or if another strategy needs to be adopted.

+12
source

I would say you need to use the appropriate tool for different tasks.

For example, for CRUD operations, ORM structures such as Hibernate can speed development, and it will work quite well. Sometimes you need to make some necessary adjustments to achieve acceptable performance. I am not sure that your task (which took 50 seconds with Hibernate) could not be completed with Hibernate because you did not provide us with the details.

On the other hand, for example, bulk operations involving hundreds of thousands of records are not the type of task you expect, Hibernate will do without a significant performance hit.

+3
source

As already mentioned, ORM is just a tool, and you can use it good or bad.

One of the most common performance issues in ORM is the problem with 1 + N queries. This is caused by loading additional objects for each of the objects in the list. This is caused by an impatient selection of 1-in-n-relationship objects for each element in the list, HQL queries are used during processing, indicating fields in the projection or labeling that receive 1-to-n relations for laziness.

At all times, you should know exactly what ORM does to achieve good performance. Failure to understand what operations are performed in the background is the path to disaster (slow, poor and complex code analysis due to unnecessary and erroneously written workarounds).

+2
source

I am with Petrar from your comments regarding the lazy selection. Suppose you have table fields filled with html from an abcd object, you may find that your infrastructure disconnects the database thousands of times (possibly many more). The disadvantage of ORM in this case is that you must carefully read the documentation. Most frameworks support disabling lazy fetching, and many even support adding their own processing logic to bind a dataset.

Cleanliness is that almost any ORM is almost certainly better than anything you intend to write yourself. You will find yourself saddled with preserving huge template libraries or by writing the same code again and again.

+1
source

We are currently exploring the transition from our own data warehouse layer with a clear separation of transfer objects and data access objects on JPA. We used the generator to create TO, DAO and SQL DDL, as well as from some documentation in the docbook format. All this is connected with the documentation, the database structure and the generated Java classes, where it always synchronizes with the good documentation of the database itself.

What we have discovered so far with the help of JPA:

  • Links to foreign keys cannot be used for import, some special requests, etc., since they should not be hosted by a managed organization. JPA only allows the target class.
  • Access to some areas of user sessions is difficult to impossible. We still have no idea how to get the user ID in the 'userWhoLastMadeAnUpdate' column in some PrePersist method.
  • Something expected that with ORM it would be very easy, namely the "class mapping" does not work at all. We use HalDateTime ( http://sourceforge.net/projects/haldatetime/ ) internally. Especially in the client. Matching it to JPA directly is not possible, although HalDateTime supports it. Due to the limitations of JPA, we have to use two fields in essence.
  • JPA uses one XML file to describe the mapping. Therefore, you should look at least two files to understand the relationship between the Java class and the database. And the XML file is becoming huge for large applications.
  • Alternatively, ORMs provide annotations in the Java class itself. So it is easier to recognize and understand the relationship. But this makes you see all these database materials at the client level (completely destroys the correct bundle).
  • You will have to limit yourself to staying as close to the clean database structure as possible. Otherwise, you will probably end up with a mess of ORM requests and assertions.
  • Use ORM, which provides a query language close to SQL (JPA seems quite acceptable here). An ORM-based language makes supporting a large application very expensive.
0
source

Source: https://habr.com/ru/post/896786/


All Articles