选择N + 1是否比渴望加入更好? [英] Is select N + 1 ever better than eager join?

查看:55
本文介绍了选择N + 1是否比渴望加入更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nhibernate探查器,并在一些地方给了我选择n + 1"警告.

i am using nhibernate profiler and seeing a few places where it is giving me a "Select n + 1" warning.

一个明显的解决方法是进行一些前期热切的联接,但是让我思考的是,在某些情况下,与选择可能要加载许多您从未实际使用的数据的联接相比,使用Select N +1的情况是否更简单或更快速

The obvious fix is to make some upfront eager joins but it got me thinking if in certain cases its actually simpler or faster to live with Select N + 1 compared to doing joins that may be load lots of data that you never actually use.

推荐答案

您的想法是正确的.例如,在某些情况下,所有根实体都引用另一个实体的一个或两个实例.进行2或3个小选择而不是非规范化的选择(即使用联接)可能会更快.

Your thinking is correct. In some cases, for example, all the root entities reference one or two instances of another entity. It might be faster to do 2 or 3 small selects instead of a denormalized one (i.e. with joins)

几乎在所有情况下都有一种使此操作更方便的方法:batch-size.如果您在实体和集合中都将此属性设置为通常的页面大小,则将获得恒定数量的小选择(每种实体类型一个).

There is a way to make this convenient in almost all cases: batch-size. If you set this attribute in both the entities and the collections to your usual page size, you'll get a constant number of small selects (one per entity type).

这篇关于选择N + 1是否比渴望加入更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆