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

网站公安备案查询重庆seo排名优化

网站公安备案查询,重庆seo排名优化,萨隆wordpress,阿里云服务器 放多个网站PageHelper自定义Count查询及其优化 文章目录 PageHelper自定义Count查询及其优化一:背景1.1、解决方法 二:利用反射判断请求参数是否有模糊查询2.1、分页不执行count2.2、思路2.3、代码示例 三:自定义COUNT查询SQL(只适用于单表)3.1、局限性…

PageHelper自定义Count查询及其优化

文章目录

  • PageHelper自定义Count查询及其优化
    • 一:背景
      • 1.1、解决方法
    • 二:利用反射判断请求参数是否有模糊查询
      • 2.1、分页不执行count
      • 2.2、思路
      • 2.3、代码示例
    • 三:自定义COUNT查询SQL(只适用于单表)
      • 3.1、局限性
      • 3.2、使用方式
    • 四:各种模糊查询XML中示例

一:背景

PageHelper默认情况下会帮我们根据查询语句自动生成COUNT查询SQL,但是有些情况下,PageHelper自动生成COUNT查询SQL存在效率问题。比如,其中使用了GROUP BY语句,多表关联,生成的COUNT查询SQL查询效率很慢

1.1、解决方法

1.count()有优化空间的直接优化,Count执行条件慢无外乎索引是否命中,执行SQL是否多表关联

2.count()没办法优化的,只能从业务入手,取消关联的一些条件查询

3.不返回count总条数,只能一页一页往下翻

4.缓存总条数,实时性保证不保证

5.异步查询加载,前端后端一起优化

也就是点击一次请求查询两个接口,list接口肯定很快返回,可以直接进行列展示,供用户操作查看等;
count接口返回较慢,分页插件下面展示loading(提示正在加载),异步执行完成后告诉前端

6.彻底解决:引入ES或其他数据库

7.代码层面:投机取巧(二:利用反射判断请求参数是否有模糊查询)

每次请求分页接口,没有任何条件查询的时候,count会计算总条数,数据量大时非常耗时;但是模糊查询的话,速度还可以接受;是否有一种方法可以:如果只是有pageIndex和pageSize参数的时候,我用自定义的count;如果有其他模糊条件查询的时候,我选择pageHelper自带的count,执行原有复杂SQL语句,维持count准确性

二:利用反射判断请求参数是否有模糊查询

2.1、分页不执行count

PageHelper.startPage(req.getCurPage(), req.getPageSize(),false);
如果将此参数设置为false,pageHlper不执行count方法;

2.2、思路

1.只有分页参数--》不执行默认count方法,设置为false--》自定义count--》返回自定义count总条数2.有模糊查询分页参数--》执行默认count方法,设置为true--》pageHelper自带count--》count参数pageHelper会返回

2.3、代码示例

 // 定义一个私有静态列表来存储需要忽略的字段名称,填写当前类的字段private static final List<String> IGNORED_FIELDS = Arrays.asList("curPage", "pageSize");//用作分页count选择,如果原始分页,选择全表差,反之用条件查public boolean areAllFieldsEmptyExceptIgnored() {//获取父类的字段//Field[] declaredFields = this.getClass().getSuperclass().getDeclaredFields();//只获取了当前类的字段for (Field field : this.getClass().getDeclaredFields()) {// 忽略静态字段和transient字段if (java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||java.lang.reflect.Modifier.isTransient(field.getModifiers())) {continue;}// 忽略配置列表中的字段if (IGNORED_FIELDS.contains(field.getName())) {continue;}// 确保私有字段也可以访问field.setAccessible(true);try {// 获取字段值Object value = field.get(this);//检查字段值是否为空// 检查字段值是否为空if (value instanceof String && ((String) value).isEmpty()) {//字段为空字符串,这是允许的,继续检查下一个字段continue;} else if (value instanceof String) {// 字段为非空字符串return false;} else if (value instanceof List && ((List<?>) value).isEmpty()) {// 字段为非空列表,这是允许的,继续检查下一个字段continue;}else if (value instanceof List) {//字段为非空集合return false;}  else if (value != null) {//字段为非空对象return false;}} catch (IllegalAccessException e) {// 处理可能的非法访问异常throw new RuntimeException("Error accessing field: " + field.getName(), e);}}//所有字段都是空的return true;}

这段反射说明:如果只有分页参数,会返回true;如果有模糊查询参数,会返回false;需要忽略的字段,支持自己设置

        boolean status = req.areAllFieldsEmptyExceptIgnored();PageHelper.startPage(req.getCurPage(), req.getPageSize(),!status);List<GoodsSpu> resultList =goodsSpuMapper.goodsSpuList(daoReq);PageInfo<GoodsSpu> pageInfo = new PageInfo<>(resultList);if (status) {//count全查pageInfo.setTotal(goodsSpuMapper.goodsSpuListCount(daoReq));}else{//count条件查,走默认分页的count}
<select id="goodsSpuList" parameterType="com.kkd.goods.model.in.GoodsSpuListDaoReq" resultType="com.kkd.goods.entity.GoodsSpu">select DISTINCT r.id dis_id,r.* from(select gsp.* from gd_goods_spu gspLEFT JOIN gd_goods_sku gsk on gsp.spu=gsk.spuLEFT JOIN gd_goods_sku_upc gsu on gsp.spu=gsu.spuLEFT JOIN gd_goods_shop_category_relation gscr on gsp.spu=gscr.spuwheregsp.is_delete=0and gsp.org_id=#{orgId}<if test="name != null and name != '' ">and gsp.`name`like concat('%',#{name},'%')</if><if test="spuList != null and spuList.size > 0">AND  gsp.spu IN<foreach item="id" index="index" collection="spuList" open="(" separator="," close=")">#{id}</foreach></if><if test="skuList != null and skuList.size > 0">AND  gsk.sku IN<foreach item="id" index="index" collection="skuList" open="(" separator="," close=")">#{id}</foreach></if><if test="upcList != null and upcList.size > 0">AND  gsu.upc IN<foreach item="id" index="index" collection="upcList" open="(" separator="," close=")">#{id}</foreach></if><if test="categoryCodes != null and categoryCodes.size > 0">AND  gsp.category_code IN<foreach item="id" index="index" collection="categoryCodes" open="(" separator="," close=")">#{id}</foreach></if><if test="shopCategoryIds != null and shopCategoryIds.size > 0">AND  gscr.shop_category_id IN<foreach item="id" index="index" collection="shopCategoryIds" open="(" separator="," close=")">#{id}</foreach></if><if test="ePlatformCategoryId != null">and gsp.e_platform_category_id=#{ePlatformCategoryId}</if><if test="isNormal != null">and gsp.normal=#{isNormal}</if><if test="imageEmpty != null  and imageEmpty == 1 ">and gsp.images is null</if><if test="isMaster != null ">and gsp.`master` = #{isMaster}</if><if test="masterSpu != null and masterSpu !='' ">and gsp.`master_spu` = #{masterSpu}</if>ORDER BY gsp.update_time desc) r
</select>
<select id="goodsSpuListCount" resultType="java.lang.Long" parameterType="com.kkd.goods.model.in.GoodsSpuListDaoReq">select count(*) from gd_goods_spu gspwheregsp.is_delete=0and gsp.org_id=#{orgId}
</select>

三:自定义COUNT查询SQL(只适用于单表)

3.1、局限性

1.对于单表查询:分页执行的sql执行效率都慢,count执行的时候首先考虑命中索引,如果拆分出来效率能得到提升再用
2.对于多表查询:如果查询条件仅仅只是主表中的条件,此方法适用如果查询条件需要从表中的条件,自定义的这个count就不满足
3.缓存count,业务上总数实时性要求不高,或者总数变化不快的情况下可以使用
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.0.4</version>
</dependency>

image-20241209165918906

3.2、使用方式

原有的代码不需要动,只需要在Mybatis的xml文件里添加一个count查询
这里注意以下三点即可:

  1. id和对应的查询语句保持一致,并且以 _COUNT 结尾
  2. 入参和对应的查询语句保持一致
  3. 出参为 resultType=“Long”
<select id="goodsSpuList_COUNT" parameterType="com.kkd.goods.model.in.GoodsSpuListDaoReq" resultType="java.lang.Long">select count(*)from gd_goods_spu gspwhere gsp.is_delete = 0and gsp.org_id = #{orgId}
</select>

四:各种模糊查询XML中示例

    <select id="goodsSpuList" parameterType="com.kkd.goods.model.in.GoodsSpuListDaoReq" resultType="com.kkd.goods.entity.GoodsSpu">select DISTINCT r.id dis_id,r.* from(select gsp.* from gd_goods_spu gspLEFT JOIN gd_goods_sku gsk on gsp.spu=gsk.spuLEFT JOIN gd_goods_sku_upc gsu on gsp.spu=gsu.spuLEFT JOIN gd_goods_shop_category_relation gscr on gsp.spu=gscr.spuwheregsp.is_delete=0and gsp.org_id=#{orgId}<if test="name != null and name != '' ">and gsp.`name`like concat('%',#{name},'%')</if><if test="spuList != null and spuList.size > 0">AND  gsp.spu IN<foreach item="id" index="index" collection="spuList" open="(" separator="," close=")">#{id}</foreach></if><if test="skuList != null and skuList.size > 0">AND  gsk.sku IN<foreach item="id" index="index" collection="skuList" open="(" separator="," close=")">#{id}</foreach></if><if test="upcList != null and upcList.size > 0">AND  gsu.upc IN<foreach item="id" index="index" collection="upcList" open="(" separator="," close=")">#{id}</foreach></if><if test="categoryCodes != null and categoryCodes.size > 0">AND  gsp.category_code IN<foreach item="id" index="index" collection="categoryCodes" open="(" separator="," close=")">#{id}</foreach></if><if test="shopCategoryIds != null and shopCategoryIds.size > 0">AND  gscr.shop_category_id IN<foreach item="id" index="index" collection="shopCategoryIds" open="(" separator="," close=")">#{id}</foreach></if><if test="ePlatformCategoryId != null">and gsp.e_platform_category_id=#{ePlatformCategoryId}</if><if test="isNormal != null">and gsp.normal=#{isNormal}</if><if test="imageEmpty != null  and imageEmpty == 1 ">and gsp.images is null</if><if test="isMaster != null ">and gsp.`master` = #{isMaster}</if><if test="masterSpu != null and masterSpu !='' ">and gsp.`master_spu` = #{masterSpu}</if>ORDER BY gsp.update_time desc) r</select>

参考:Pagehelper自定义count查询


文章转载自:
http://dinncotimesaver.zfyr.cn
http://dinncosubteenager.zfyr.cn
http://dinncobiometrician.zfyr.cn
http://dinncothrapple.zfyr.cn
http://dinncoleadman.zfyr.cn
http://dinncoaerosiderolite.zfyr.cn
http://dinncobreeziness.zfyr.cn
http://dinncoorangism.zfyr.cn
http://dinncopismire.zfyr.cn
http://dinncocowlike.zfyr.cn
http://dinncohouseman.zfyr.cn
http://dinncoacathisia.zfyr.cn
http://dinncoastrionics.zfyr.cn
http://dinncoracecard.zfyr.cn
http://dinncocranial.zfyr.cn
http://dinncopulk.zfyr.cn
http://dinncoringworm.zfyr.cn
http://dinncodiscretion.zfyr.cn
http://dinncosoccage.zfyr.cn
http://dinncokanaima.zfyr.cn
http://dinncodully.zfyr.cn
http://dinncopaleichthyology.zfyr.cn
http://dinncocogently.zfyr.cn
http://dinncoembryology.zfyr.cn
http://dinncoinquisitress.zfyr.cn
http://dinncocabaletta.zfyr.cn
http://dinncosquarely.zfyr.cn
http://dinncobead.zfyr.cn
http://dinncoinitiating.zfyr.cn
http://dinncoactualist.zfyr.cn
http://dinncohomeland.zfyr.cn
http://dinncomorphophoneme.zfyr.cn
http://dinncofacedown.zfyr.cn
http://dinncoovergorge.zfyr.cn
http://dinncogardenless.zfyr.cn
http://dinncobertram.zfyr.cn
http://dinncofontanelle.zfyr.cn
http://dinncohypocoristic.zfyr.cn
http://dinncolibera.zfyr.cn
http://dinncoflyswatter.zfyr.cn
http://dinncophenology.zfyr.cn
http://dinncosigil.zfyr.cn
http://dinncoimpress.zfyr.cn
http://dinncochlamydospore.zfyr.cn
http://dinncosteelworker.zfyr.cn
http://dinncovitular.zfyr.cn
http://dinncocalculative.zfyr.cn
http://dinncoluebke.zfyr.cn
http://dinncoinwall.zfyr.cn
http://dinncohospitable.zfyr.cn
http://dinncohasheesh.zfyr.cn
http://dinncovine.zfyr.cn
http://dinncooleomargarine.zfyr.cn
http://dinncothc.zfyr.cn
http://dinncopterylography.zfyr.cn
http://dinncodiagnostics.zfyr.cn
http://dinncoacknowledgedly.zfyr.cn
http://dinncolabroid.zfyr.cn
http://dinncosombre.zfyr.cn
http://dinncolegate.zfyr.cn
http://dinncosyndeton.zfyr.cn
http://dinncofoziness.zfyr.cn
http://dinncodevastating.zfyr.cn
http://dinncoholoplankton.zfyr.cn
http://dinncorelativize.zfyr.cn
http://dinncotumult.zfyr.cn
http://dinncosulphazin.zfyr.cn
http://dinncopassable.zfyr.cn
http://dinncoimpetuosity.zfyr.cn
http://dinncoteledrama.zfyr.cn
http://dinnconickelous.zfyr.cn
http://dinncotritely.zfyr.cn
http://dinncodoodlebug.zfyr.cn
http://dinncoivorian.zfyr.cn
http://dinncospic.zfyr.cn
http://dinncosinnet.zfyr.cn
http://dinncoconsult.zfyr.cn
http://dinncoeliminate.zfyr.cn
http://dinncoreynold.zfyr.cn
http://dinncoadeodatus.zfyr.cn
http://dinncocollage.zfyr.cn
http://dinncosplendour.zfyr.cn
http://dinncoscotoma.zfyr.cn
http://dinncofavourite.zfyr.cn
http://dinncofili.zfyr.cn
http://dinncomott.zfyr.cn
http://dinncocounterproductive.zfyr.cn
http://dinncodesperately.zfyr.cn
http://dinncobilk.zfyr.cn
http://dinnconisus.zfyr.cn
http://dinncocerebratmon.zfyr.cn
http://dinncocompurgation.zfyr.cn
http://dinncowidukind.zfyr.cn
http://dinncooverroof.zfyr.cn
http://dinncotreetop.zfyr.cn
http://dinncoskep.zfyr.cn
http://dinncomemcon.zfyr.cn
http://dinncopolarize.zfyr.cn
http://dinncoresection.zfyr.cn
http://dinncogangrenous.zfyr.cn
http://www.dinnco.com/news/94431.html

相关文章:

  • 做网站工作职责举一个病毒营销的例子
  • dedecms网站关键词简述网络营销的概念
  • 科技帝国从高分子材料开始google seo优化
  • 电子商务网站建设管理论文百度明星人气榜入口
  • 网站一起做网店windows优化大师官方免费
  • 网站宽度 自动收缩泉州搜索推广
  • 网站301跳转怎么做的百度广告平台
  • asp.ney旅游信息网站下载 简洁全案网络推广公司
  • 房地产集团网站建设方案深圳aso优化
  • 营销型网站策划教育培训机构招生方案
  • 做网站下载福州短视频seo方法
  • 上海网站开发开发好的公司电话百度收录批量查询
  • 图片设计在线企业网站优化解决方案
  • php动态网站开发效果图2024年新闻摘抄十条
  • 创建网站百度站点
  • 济南网站建设套餐搜索引擎seo关键词优化
  • 人力资源公司网站建设方案友情链接怎么交换
  • 提供手机网站制作哪家好营销策划与运营团队
  • 深圳网站建设百度指数第一
  • 建设公司自己的网站青岛谷歌优化公司
  • 网站建设经典语录链接提交
  • 重庆今天新闻发布会直播北京seo代理商
  • 滨江区做网站公司青岛seo霸屏
  • 广州微网站建设平台百度云网页版入口
  • 做策划常用的网站关键词林俊杰的寓意
  • dedecms 招聘网站优化大师windows
  • 中文单页面网站模板广告公司推广文案
  • 企业网站免费认证网络促销方案
  • 网站建设业务范围网络推广产品公司
  • 中国新闻网上海新闻旅游seo整站优化