我有一个与使用连接表Campaign
具有多对多关系的实体。Contact
Recipient
class Campaign {
...
static hasMany = [recipients: Recipient]
...
}
class Recipient {
...
Contact contact
static belongsTo = [campaign: Campaign]
...
}
class Contact {
...
}
营销活动可以有数十万个联系人,但由于延迟获取是默认设置,因此可以快速从数据库中检索营销活动。但是,当我调用campaign.refresh()
GORM 尝试加载所有收件人时,Grails 内存不足。
List<Campaign> campaigns = Campaign.findAllWhere([status: CampaignStatus.STARTED])
campaigns.each { campaign ->
if (campaign.refresh().isStarted()) {
campaign.send();
}
}
我打开了 logSql,看看正在执行什么。此代码已被清理以提高可读性。
-- Campaign.findAllWhere
select * from campaign where (status=?)
-- campaign.refresh()
select * from campaign c left outer join recipient r on c.campaign_id=r.campaign_id where c.campaign_id=?
为什么刷新加入收件人但查找查询不是?