开发者

One-to-One or Many-to-One with CF9 ORM in 2 queries

开发者 https://www.devze.com 2023-04-04 12:13 出处:网络
I have two objects HomeFeatures <cfcomponent persistent=\"true\"> <cfproperty name=\"id\" column=\"homeFeatureid\">

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号