网站 选项卡 图标,wordpress 开发h5页面,做黄金期货的网站,设计网址合集PageHelper自定义Count查询及其优化 文章目录 PageHelper自定义Count查询及其优化一#xff1a;背景1.1、解决方法 二#xff1a;利用反射判断请求参数是否有模糊查询2.1、分页不执行count2.2、思路2.3、代码示例 三#xff1a;自定义COUNT查询SQL(只适用于单表)3.1、局限性…PageHelper自定义Count查询及其优化 文章目录 PageHelper自定义Count查询及其优化一背景1.1、解决方法 二利用反射判断请求参数是否有模糊查询2.1、分页不执行count2.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);
如果将此参数设置为falsepageHlper不执行count方法2.2、思路 
1.只有分页参数--》不执行默认count方法设置为false--》自定义count--》返回自定义count总条数2.有模糊查询分页参数--》执行默认count方法设置为true--》pageHelper自带count--》count参数pageHelper会返回2.3、代码示例 // 定义一个私有静态列表来存储需要忽略的字段名称,填写当前类的字段private static final ListString 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);ListGoodsSpu resultList goodsSpuMapper.goodsSpuList(daoReq);PageInfoGoodsSpu pageInfo  new PageInfo(resultList);if (status) {//count全查pageInfo.setTotal(goodsSpuMapper.goodsSpuListCount(daoReq));}else{//count条件查,走默认分页的count}select idgoodsSpuList parameterTypecom.kkd.goods.model.in.GoodsSpuListDaoReq resultTypecom.kkd.goods.entity.GoodsSpuselect DISTINCT r.id dis_id,r.* from(select gsp.* from gd_goods_spu gspLEFT JOIN gd_goods_sku gsk on gsp.spugsk.spuLEFT JOIN gd_goods_sku_upc gsu on gsp.spugsu.spuLEFT JOIN gd_goods_shop_category_relation gscr on gsp.spugscr.spuwheregsp.is_delete0and gsp.org_id#{orgId}if testname ! null and name !  and gsp.namelike concat(%,#{name},%)/ifif testspuList ! null and spuList.size  0AND  gsp.spu INforeach itemid indexindex collectionspuList open( separator, close)#{id}/foreach/ifif testskuList ! null and skuList.size  0AND  gsk.sku INforeach itemid indexindex collectionskuList open( separator, close)#{id}/foreach/ifif testupcList ! null and upcList.size  0AND  gsu.upc INforeach itemid indexindex collectionupcList open( separator, close)#{id}/foreach/ifif testcategoryCodes ! null and categoryCodes.size  0AND  gsp.category_code INforeach itemid indexindex collectioncategoryCodes open( separator, close)#{id}/foreach/ifif testshopCategoryIds ! null and shopCategoryIds.size  0AND  gscr.shop_category_id INforeach itemid indexindex collectionshopCategoryIds open( separator, close)#{id}/foreach/ifif testePlatformCategoryId ! nulland gsp.e_platform_category_id#{ePlatformCategoryId}/ifif testisNormal ! nulland gsp.normal#{isNormal}/ifif testimageEmpty ! null  and imageEmpty  1 and gsp.images is null/ifif testisMaster ! null and gsp.master  #{isMaster}/ifif testmasterSpu ! null and masterSpu ! and gsp.master_spu  #{masterSpu}/ifORDER BY gsp.update_time desc) r
/selectselect idgoodsSpuListCount resultTypejava.lang.Long parameterTypecom.kkd.goods.model.in.GoodsSpuListDaoReqselect count(*) from gd_goods_spu gspwheregsp.is_delete0and gsp.org_id#{orgId}
/select三自定义COUNT查询SQL(只适用于单表) 
3.1、局限性 
1.对于单表查询:分页执行的sql执行效率都慢count执行的时候首先考虑命中索引如果拆分出来效率能得到提升再用
2.对于多表查询如果查询条件仅仅只是主表中的条件此方法适用如果查询条件需要从表中的条件自定义的这个count就不满足
3.缓存count业务上总数实时性要求不高或者总数变化不快的情况下可以使用dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion5.0.4/version
/dependency3.2、使用方式 
原有的代码不需要动只需要在Mybatis的xml文件里添加一个count查询 这里注意以下三点即可 
id和对应的查询语句保持一致并且以 _COUNT 结尾入参和对应的查询语句保持一致出参为 resultType“Long” 
select idgoodsSpuList_COUNT parameterTypecom.kkd.goods.model.in.GoodsSpuListDaoReq resultTypejava.lang.Longselect count(*)from gd_goods_spu gspwhere gsp.is_delete  0and gsp.org_id  #{orgId}
/select四各种模糊查询XML中示例 select idgoodsSpuList parameterTypecom.kkd.goods.model.in.GoodsSpuListDaoReq resultTypecom.kkd.goods.entity.GoodsSpuselect DISTINCT r.id dis_id,r.* from(select gsp.* from gd_goods_spu gspLEFT JOIN gd_goods_sku gsk on gsp.spugsk.spuLEFT JOIN gd_goods_sku_upc gsu on gsp.spugsu.spuLEFT JOIN gd_goods_shop_category_relation gscr on gsp.spugscr.spuwheregsp.is_delete0and gsp.org_id#{orgId}if testname ! null and name !  and gsp.namelike concat(%,#{name},%)/ifif testspuList ! null and spuList.size  0AND  gsp.spu INforeach itemid indexindex collectionspuList open( separator, close)#{id}/foreach/ifif testskuList ! null and skuList.size  0AND  gsk.sku INforeach itemid indexindex collectionskuList open( separator, close)#{id}/foreach/ifif testupcList ! null and upcList.size  0AND  gsu.upc INforeach itemid indexindex collectionupcList open( separator, close)#{id}/foreach/ifif testcategoryCodes ! null and categoryCodes.size  0AND  gsp.category_code INforeach itemid indexindex collectioncategoryCodes open( separator, close)#{id}/foreach/ifif testshopCategoryIds ! null and shopCategoryIds.size  0AND  gscr.shop_category_id INforeach itemid indexindex collectionshopCategoryIds open( separator, close)#{id}/foreach/ifif testePlatformCategoryId ! nulland gsp.e_platform_category_id#{ePlatformCategoryId}/ifif testisNormal ! nulland gsp.normal#{isNormal}/ifif testimageEmpty ! null  and imageEmpty  1 and gsp.images is null/ifif testisMaster ! null and gsp.master  #{isMaster}/ifif testmasterSpu ! null and masterSpu ! and gsp.master_spu  #{masterSpu}/ifORDER BY gsp.update_time desc) r/select参考Pagehelper自定义count查询