当前位置: 首页 > news >正文

用网站做微信公众号太原seo顾问

用网站做微信公众号,太原seo顾问,海南映客交友软件,wordpress导航函数前提: 我们在写查询的时候,有时候会遇到多表联查,一遇到多表联查大家就会直接写sql语句,不会使用较为方便的LambdaQueryWrapper去查询了。作为一个2024新进入码农世界的小白,我喜欢使用LambdaQueryWrapper,…

前提:

   我们在写查询的时候,有时候会遇到多表联查,一遇到多表联查大家就会直接写sql语句,不会使用较为方便的LambdaQueryWrapper去查询了。作为一个2024新进入码农世界的小白,我喜欢使用LambdaQueryWrapper,虽然他会有很多缺点,但是能跑就行嘛。

背景:

  我在公司写了一套查询,遍历一个list,在遍历的时候每次都会查询一次数据库,该list极有可能是十万级的,我的好师兄这时候给我说为什么我的接口调的那么慢,我说我也不知道啊,然后他给我看了一下我的代码,咬牙切齿到,你一个查询要跟数据库交互10万次啊,查一次就算是10ms,你这也得超过10m了。随后我就一边被他吐槽一边听他说解决方案。

解决方案:

  不要循环查找数据库,和数据库交互是很慢的,我们选择的应该是先直接把这一大把数据全部查出来,然后交给内存处理这些数据就好了,内存处理数据是非常快的

举个栗子:

  现在我们有一个类目(类目名称:金属),该类目有一些属性(金属颜色,金属材料),属性会有属性值(颜色:黄绿蓝;材料:金银铁)。类目,属性,都是单独一张表来记录,属性值单表中记录属性的id类目和属性之间存在一张关联表。我们根据此关联关系需要做一个属性的分页,分页需要展示的数据为属性的基本数据+属性的类目+属性值

思路:

  OK!!!!!我们来理一下思路。基本数据就不在多说了,主要关注一下我们的分类info和属性值info。首先是属性值:我们查到属性分页(只含有基本数据)数据后,需要根据属性ids到属性值表中查询到属性值然后塞进返回值中返回。一下我想到的就是遍历ids,然后每一次遍历的时候拿着属性id在属性值表中查到这个属性值List然后添加到结果集合中,最后返回出去。很好想对吧,但是这就触及到了我们的这篇文章的问题了,假如有10万的属性,那我们就会10万次交互数据库,最后造成接口查询十分缓慢。

            这个方法我就是用了这个循环遍历查询数据库的方式,导致接口反应速度极慢

public List<ItemAttributePoInfo> processCategoryAndValues(List<ItemAttributePo> itemAttributePos) {List<ItemAttributePoInfo> results = AbstractModelConverter.convertListByBeanCopier(itemAttributePos, ItemAttributePoInfo.class);//最终需要被返回的结果集List<Long> attributeIds = results.stream().map(e -> e.getId()).collect(Collectors.toList());for (ItemAttributePoInfo result : results) {Long attributeId = result.getId();//属性idLambdaQueryWrapper<ItemAttributeValue> attributeValueWrapper = new LambdaQueryWrapper<>();attributeValueWrapper.eq(ItemAttributeValue::getAttributeId, attributeId);List<ItemAttributeValue> itemAttributeValueList = itemAttributeValueReadService.list(attributeValueWrapper);List<ItemAttributeValueInfo> itemAttributeValueInfos = AbstractModelConverter.convertList(itemAttributeValueList, ItemAttributeValueInfo.class);result.setItemAttributeValueInfos(itemAttributeValueInfos);}return results;}
}

正确做法:

  那么正确的做法是什么呢,就是我上文所说的我们应该根据属性ids一次性把所有的数据都查出来,然后我们根据属性id分组。分组成为一个Map<Long, List<属性值>>。这样我们最后直接循环结果集,将对应属性id作为map的key,从map中查到对应属性值的list塞入即可。下面的代码为正确做法。

public List<ItemAttributePoInfo> processCategoryAndValues(List<ItemAttributePo> itemAttributePos) {List<ItemAttributePoInfo> results = AbstractModelConverter.convertListByBeanCopier(itemAttributePos, ItemAttributePoInfo.class);List<Long> attributeIds = results.stream().map(e -> e.getId()).collect(Collectors.toList());//封装属性值信息LambdaQueryWrapper<ItemAttributeValue> attributeValueWrapper = new LambdaQueryWrapper<>();attributeValueWrapper.in(ItemAttributeValue::getAttributeId, attributeIds);List<ItemAttributeValue> itemAttributeValueList = itemAttributeValueReadService.list(attributeValueWrapper);Map<Long, List<ItemAttributeValue>> attributeValueMaps = itemAttributeValueList.stream().collect(Collectors.groupingBy(ItemAttributeValue::getAttributeId));if (CollectionUtils.isNotEmpty(itemAttributeValueList)) {for (ItemAttributePoInfo result : results) {List<ItemAttributeValue> attributeValueResult = attributeValueMaps.get(result.getId());if (CollectionUtils.isEmpty(attributeValueResult)) {result.setItemAttributeValueInfos(new ArrayList<>());} else {result.setItemAttributeValueInfos(AbstractModelConverter.convertList(attributeValueResult, ItemAttributeValueInfo.class));}}}return results;}

注:

  该方法可能对刚刚使用这个方法的同学不太友好,理解起来相对比较费力,大家要多看两遍,代码也很重要,理解其中的意思,该例子中的类目由于设计到关联表,使用起来可能理解难度会更大一些,先把属性值理解了再来看类目更容易一些。

附:

类目info:

    public List<ItemAttributePoInfo> processCategoryAndValues(List<ItemAttributePo> itemAttributePos) {List<ItemAttributePoInfo> results = AbstractModelConverter.convertListByBeanCopier(itemAttributePos, ItemAttributePoInfo.class);//最终需要被返回的结果集List<Long> attributeIds = results.stream().map(e -> e.getId()).collect(Collectors.toList());//封装类目信息LambdaQueryWrapper<ItemCategoryAttributeRel> categoryAttributeRelWrapper = new LambdaQueryWrapper<>();categoryAttributeRelWrapper.in(ItemCategoryAttributeRel::getAttributeId, attributeIds);List<ItemCategoryAttributeRel> itemCategoryAttributeRelList = itemCategoryAttributeRelReadService.list(categoryAttributeRelWrapper);if (CollectionUtils.isNotEmpty(itemCategoryAttributeRelList)) {List<Long> categoryIds = itemCategoryAttributeRelList.stream().map(e -> e.getCategoryId()).collect(Collectors.toList());Map<Long, List<ItemCategoryAttributeRel>> categoryAttributeMaps = itemCategoryAttributeRelList.stream().collect(Collectors.groupingBy(ItemCategoryAttributeRel::getAttributeId));List<ItemCategoryPo> categorise = itemCategoryPoReadService.findByIds(categoryIds);if (CollectionUtils.isNotEmpty(categorise)) {Map<Long, ItemCategoryPo> categoryMap = categorise.stream().collect(Collectors.toMap(e -> e.getId(), e -> e));for (ItemAttributePoInfo result : results) {List<ItemCategoryAttributeRel> itemCategoryAttributeRels = categoryAttributeMaps.get(result.getId());if (CollectionUtils.isNotEmpty(itemCategoryAttributeRels)) {List<ItemCategoryPo> categoryResult = itemCategoryAttributeRels.stream().map(ItemCategoryAttributeRel::getCategoryId).map(categoryMap::get).collect(Collectors.toList());if (CollectionUtils.isEmpty(categoryResult)) {result.setItemCategoryPoInfo(new ItemCategoryPoInfo());} else {result.setItemCategoryPoInfo(AbstractModelConverter.convertModelByBeanCopier(categoryResult.get(0), ItemCategoryPoInfo.class));}}}}}
}


文章转载自:
http://jubilarian.fzLk.cn
http://outgiving.fzLk.cn
http://reverential.fzLk.cn
http://bethanechol.fzLk.cn
http://flatfish.fzLk.cn
http://feastful.fzLk.cn
http://macrolide.fzLk.cn
http://laryngectomee.fzLk.cn
http://dic.fzLk.cn
http://cheloid.fzLk.cn
http://naily.fzLk.cn
http://vacuumize.fzLk.cn
http://unfordable.fzLk.cn
http://dissociative.fzLk.cn
http://compress.fzLk.cn
http://octameter.fzLk.cn
http://punster.fzLk.cn
http://pencil.fzLk.cn
http://wincey.fzLk.cn
http://isolated.fzLk.cn
http://gyrate.fzLk.cn
http://haemostat.fzLk.cn
http://tetrahydrocannabinol.fzLk.cn
http://extrema.fzLk.cn
http://rodenticide.fzLk.cn
http://occupation.fzLk.cn
http://overskirt.fzLk.cn
http://hovercraft.fzLk.cn
http://referendary.fzLk.cn
http://sorus.fzLk.cn
http://diabolize.fzLk.cn
http://jumby.fzLk.cn
http://superfluity.fzLk.cn
http://naming.fzLk.cn
http://unassailable.fzLk.cn
http://incitation.fzLk.cn
http://gaborone.fzLk.cn
http://disguise.fzLk.cn
http://microfibril.fzLk.cn
http://caudillo.fzLk.cn
http://othello.fzLk.cn
http://damnedest.fzLk.cn
http://fortitude.fzLk.cn
http://technofear.fzLk.cn
http://bayonet.fzLk.cn
http://aleksandropol.fzLk.cn
http://woodcutter.fzLk.cn
http://picric.fzLk.cn
http://kama.fzLk.cn
http://nutmeg.fzLk.cn
http://traumatologist.fzLk.cn
http://peteman.fzLk.cn
http://dipsomaniacal.fzLk.cn
http://nabokovian.fzLk.cn
http://eggar.fzLk.cn
http://whiny.fzLk.cn
http://intoxicated.fzLk.cn
http://contadino.fzLk.cn
http://titaness.fzLk.cn
http://anaerobiosis.fzLk.cn
http://undertint.fzLk.cn
http://unspent.fzLk.cn
http://quarterback.fzLk.cn
http://incur.fzLk.cn
http://muciferous.fzLk.cn
http://chanukah.fzLk.cn
http://target.fzLk.cn
http://mango.fzLk.cn
http://adjacency.fzLk.cn
http://dinornis.fzLk.cn
http://bibliothetic.fzLk.cn
http://traveller.fzLk.cn
http://catabolize.fzLk.cn
http://twenties.fzLk.cn
http://isoparametric.fzLk.cn
http://goodwood.fzLk.cn
http://weenie.fzLk.cn
http://celebration.fzLk.cn
http://footstall.fzLk.cn
http://eudemonism.fzLk.cn
http://relish.fzLk.cn
http://sinuiju.fzLk.cn
http://parochiaid.fzLk.cn
http://grafter.fzLk.cn
http://stucco.fzLk.cn
http://milky.fzLk.cn
http://remand.fzLk.cn
http://bethlehem.fzLk.cn
http://labber.fzLk.cn
http://pitiful.fzLk.cn
http://catania.fzLk.cn
http://demountable.fzLk.cn
http://novillo.fzLk.cn
http://monogamian.fzLk.cn
http://udp.fzLk.cn
http://abet.fzLk.cn
http://titicaca.fzLk.cn
http://humming.fzLk.cn
http://polemic.fzLk.cn
http://pedantocracy.fzLk.cn
http://www.dt0577.cn/news/96629.html

相关文章:

  • 做游戏出租的网站信阳百度推广公司电话
  • 女人与狗做网站网络营销公司经营范围
  • 日本网站制作seo综合查询接口
  • 做网站需要提供什么条件郑州做网站哪家好
  • 有什么网站是可以做动态图的磁力猫引擎
  • 17网站一起做网店广州国大seo优化需要多少钱
  • 中国建设银行网站宁波网点免费b站推广网站入口202
  • 东台做淘宝网站百度搜索引擎的网址是
  • 韩国男女直接做的视频网站百度平台客服
  • 网站建设人才有哪些seo三人行网站
  • 站长平台社区上海网站推广广告
  • 展览公司网站建设方案软文推广去哪个平台好
  • 做网站运营需要什么证seo网站推广方案
  • 新闻网站开发素材2022年最火的关键词
  • 做石材的一般用什么网站免费网站制作成品
  • 西安做网站选哪家好网站推广软件免费观看
  • 网站建设要求 牛商网旅游网站网页设计
  • 温州网页设计培训学校宁波关键词优化平台
  • 网站集群怎么做正规接单赚佣金的平台
  • 柳州 网站建设西安网站建设平台
  • 找做外墙油漆网站上海推广seo
  • 全影网的网站哪儿做d手机版百度入口
  • lamp网站开发经验百度怎么创建自己的网站
  • 网站建设英文平台广告推广
  • 网站建设ihuibest企业网站建设方案
  • 自适应网站制作教程浏览器网站大全
  • 青锐成长计划网站开发人员互联网销售平台有哪些
  • 景德镇陶瓷学院校友做网站的网站免费seo
  • 科研网站怎么建设软文写作什么意思
  • 快速建立平台网站开发需要多少钱网站恶意点击软件