在 2 个查询中使用 CF9 ORM 进行一对一或多对一

发布于 2024-12-04 08:11:47 字数 1528 浏览 0 评论 0原文

我有两个对象

HomeFeatures
<cfcomponent persistent="true">
    <cfproperty name="id" column="homeFeatureid">
    <cfproperty name="tabTitle">
    <cfproperty name="title">
    <cfproperty name="description">
    <cfproperty name="sort">
    <cfproperty name="moretext">
    <cfproperty name="url">
    <cfproperty name="active">
    <cfproperty name="media" fieldtype="many-to-one" fkcolumn="mediaid" cfc="media" lazy="false" fetch="join" missingrowIgnored="true">
</cfcomponent>

Media
<cfcomponent persistent="true" table="cms.dbo.media">
    <cfproperty name="id" column="mediaid">
    <cfproperty name="filename">
</cfcomponent>

因此我有多个 homeFeatures,每个都有一个与之关联的媒体项。同一媒体项可以与多个 homeFeatures 关联,但每个 homeFeature 只能有一个由 homeFeatures.mediaid 连接的媒体项。

如果我执行entityLoad(“homeFeatures”),我会得到1个homeFeatures查询和1个媒体表查询homeFeatures中的每一行。换句话说,这就是N+1问题。为什么?这总共应该是两个查询,一个查询获取所有 homeFeatures,另一个查询获取媒体 ID 位于 homeFeatures 确定的列表中的所有媒体。我正在尝试执行 http://www.rupeshk.org/blog/index.php/2009/09/coldfusion-orm-performance-tuning-fetching-strategy/ 并在adobe 文档。

在这个特定视图中,我需要访问每个条目的媒体记录,因此这就是我将惰性设置为 false 的原因。我发誓我已经尝试了lazy=false|true、batch=999、fieldtype=one-to-one|many-to-one、fetch=join|select的每次迭代,并且所有这些迭代都会产生相同的N+1问题。

有什么想法吗?请帮忙!

I have two objects

HomeFeatures
<cfcomponent persistent="true">
    <cfproperty name="id" column="homeFeatureid">
    <cfproperty name="tabTitle">
    <cfproperty name="title">
    <cfproperty name="description">
    <cfproperty name="sort">
    <cfproperty name="moretext">
    <cfproperty name="url">
    <cfproperty name="active">
    <cfproperty name="media" fieldtype="many-to-one" fkcolumn="mediaid" cfc="media" lazy="false" fetch="join" missingrowIgnored="true">
</cfcomponent>

And

Media
<cfcomponent persistent="true" table="cms.dbo.media">
    <cfproperty name="id" column="mediaid">
    <cfproperty name="filename">
</cfcomponent>

Thus I have multiple homeFeatures, each of which has a media item associated with it. The same media item could be associated with multiple homeFeatures, but each homeFeature can only have one media item joined by homeFeatures.mediaid.

If I do an entityLoad("homeFeatures") I get 1 query for the homeFeatures and 1 query of the media table for EVERY ROW IN homeFeatures. In other words this is the N+1 problem. Why? This should be two queries total, one to get all of the homeFeatures, and one to get all of the media who's media ID is in the list determined by homeFeatures. I am attempting to do "eager" loading as defined at http://www.rupeshk.org/blog/index.php/2009/09/coldfusion-orm-performance-tuning-fetching-strategy/ and in the adobe docs.

In this specific view I need to access the media record for every entry, so that is why I have lazy set to false. I swear I have tried every iteration of lazy=false|true, batch=999, fieldtype=one-to-one|many-to-one, fetch=join|select, and all of them produce the same N+1 problem.

Any ideas? Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-12-11 08:11:47

在这种情况下,解决方案是batchsize。通过在 cfc 或 cfproperty 上指定,我们可以设置批量大小,以便将相关项目分组批处理,而不是单独批处理。

In this case the solution is batchsize. By specifying on the cfc or on the cfproperty we are able to set a batchsize so that the related items are batched in groups, rather than individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文