本文介绍了的对象关系映射的缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ORM的粉丝 - 对象关系映射和我一直在使用它使用Rails在过去的一年半的时间。在此之前,我用它来编写使用JDBC原始查询,并通过数据库存储过程做繁重。有了ORM,我最初乐意做的东西,如 coach.manager manager.coaches 这是非常简单和容易阅读。

I am a fan of ORM - Object Relational Mapping and I have been using it with Rails for the past year and a half. Prior that, I use to write raw queries using JDBC and make Database do the heavy lifting via Stored Procedures. With ORM, I was initially happy to do stuff like coach.manager and manager.coaches which were very simple and easy to read.

不过,随着时间的推移有中,许多协会爬起来,我终于实现了 ABCD 这是在所有方向发射的查询,在幕后。随着轨和红宝石,垃圾收集器疯了,把疯狂的时间来加载一个非常复杂的网页,其中涉及到相对较小的数据。我有一个简单的存储过程,我看到的结果是巨大的,以取代本ORM风格code。这需要50秒来加载一个页面只需要2秒。

But as time went by there were in-numerous associations creeping up and I ended up doing a.b.c.d which were firing queries in all directions, behind the scenes. With rails and ruby, the garbage collector went nuts and took insane time to load a very complex page which involves relatively lesser data. I had to replace this ORM style code by a simple Stored procedure and the result I saw was enormous. A page that took 50 seconds to load now takes only 2 seconds.

通过这种巨大的差异,我应该继续使用ORM?这是非常清楚它有严重的开销相比,原始查询。

With this huge difference, should I continue using ORM? It is very clear it has severe overheads compared to a raw query.

在一般情况下,什么是使用ORM框架如Hibernate的一般缺陷,ActiveRecord的?

In general, what are the general pitfalls of using an ORM framework like Hibernate, ActiveRecord?

推荐答案

我想你已经确定了ORM软件相关的重要权衡。每次添加一个新的抽象层,它试图提供的东西,你用来做手工广义的实施有将是性能/效率的一些损失。

I think you've already identified the major tradeoff associated with ORM software. Every time you add a new layer of abstraction that tries to provide a generalized implementation of something that you used to do by hand there is going to be some loss of performance/efficiency.

正如你提到的,穿越如 ABCD倍数关系可能是低效的,因为大多数的ORM的软件会做的每个一个独立的数据库查询。 前进的道路上。但我不知道这意味着你应该消除ORM完全。大多数的ORM解决方案(或者至少,肯定休眠)允许您指定自定义查询,你可以带回你想在一个单一的数据库操作到底是什么。这应该是一样快竭诚为您的SQL。

As you noted, traversing multiple relationships such as a.b.c.d can be inefficient, because most ORM software will be doing an independent database query for each . along the way. But I'm not sure that means you should eliminate ORM altogether. Most ORM solutions (or at least, certainly Hibernate) allow you to specify custom queries where you can bring back exactly what you want in a single database operation. This should be about as fast as your dedicated SQL.

真正的问题是关于如何理解ORM层在幕后工作,并认识到虽然像 ABCD 简单写,它所引起的ORM层做,因为它被评估不是。作为一般规则,我总是去开始,然后编写优化的查询,在它有道理/,其中很明显,简单的方法将不能扩展区域最简单的方法。

Really the issue is about understanding how the ORM layer is working behind the scenes, and realizing that while something like a.b.c.d is simple to write, what it causes the ORM layer to do as it is evaluated is not. As a general rule I always go with the simplest possible approach to begin, and then write optimized queries in areas where it makes sense/where it is obvious that the simple approach will not scale.

这篇关于的对象关系映射的缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:41