目录
优化前
优化后
场景:数据库表两个字段组成唯一键,需要批量用唯一键查询记录
UNIQUE KEY `uk_channel_org` (`channel`, `org`),
优化前
for (ChannelOrg channelOrg : channelOrgList) {
//循环中查询数据库
ChannelOrgLevelConfigDO config = channelOrgLevelConfigService.getNode(channelOrg);
//...省略
}
缺:循环中查询数据库,有大量IO,影响性能,不可控
优化后
//组合查询后,组装得到对应记录map
Map<ChannelOrg, ChannelOrgLevelConfigDO> channelOrgConfigMap = channelOrgLevelConfigService.getNodeMap(channelOrgList);
//循环处理(避免循环查询数据库,大量IO影响性能)
for (ChannelOrg channelOrg : channelOrgList) {
ChannelOrgLevelConfigDO config = channelOrgConfigMap.get(channelOrg);
//...省略
}
优:避免循环中查询数据库,提高接口性能
sql实现?
//sql 查询得到list
select * from pss_channel_org_level_config where (channel, org) in
<foreach close=")" collection="params" item="item" open="(" separator=",">
(#{item.channel}, #{item.orgCode})
</foreach>
sql执行计划:命中唯一键索引
|