栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mysql百万数据分页查询+分表优化

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mysql百万数据分页查询+分表优化

Mysql百万数据分表分页查询优化

​ 券码表(msp_voucher)使用shardingsphere分成了32张表,分表字段为卡券号(coupon_no),主要是对查询券码表的优化。
考虑到联查效率问题,接口使用代码逻辑查询。

券码表共有1111033条数据

{
    "pageNum":100000,
    "pageSize":10
}

查询券码表sql如下,分页使用PageHelper,用时:

1,10100,1010000,10100000,10
659 ms896 ms29.94 s2 m 2.76 s
select

from msp_voucher

    
        voucher_no = #{voucherNo}
    
    
        and voucher_end_time between #{dateRangeBegin} and date_add(#{dateRangeEnd},interval 1 day)
    
    
        and status = #{status}
    
    
        and coupon_no in
        
            #{couponNo}
        
    

order by create_time desc

覆盖索引:覆盖索引是指一个索引包含或者覆盖了所有需要查询的字段的值,不需要根据索引回表查询数据
覆盖索引必须要存储索引列的值,因此MySQL只能用BTree索引做覆盖索引。

  • 索引条目通常远小于数据行大小,极大减少数据访问量。
  • 因为索引按照顺序存储,所以io密集型查询会比随机从磁盘读取每一行数据的io少的多。
  • innoDB使用聚簇索引,innoDB的二级索引在叶子节点保存了主键,如果二级主键能覆盖查询就可以避免对主键索引的二次查询。

优化一:使用覆盖索引,先查出具体十条id,再id in ()去查。由于sharding-jdbc不支持在子查询中出现同样的表,无法自关联查询,会报错:Must have one sharding with subquery。因此分成两个sql查询。

1,10100,1010000,10100000,10
918 ms1066 ms3 s25s
select
id
from msp_voucher

    
        voucher_no = #{voucherNo}
    
    
        and voucher_end_time between #{dateRangeBegin} and date_add(#{dateRangeEnd},interval 1 day)
    
    
        and status = #{status}
    
    
        and coupon_no in
        
            #{couponNo}
        
    

order by create_time desc
limit #{offset}, #{size}
select

from msp_voucher
where id in

  #{id}

and coupon_no in

    #{couponNo}

优化二:依然是覆盖索引,先查出符合条件的第一个id,再使用id作为条件过滤,id为雪花算法生成,为趋势递增。

1,10100,1010000,10100000,10
764ms913ms2s17s
select
id
from msp_voucher

    
        voucher_no = #{voucherNo}
    
    
        and voucher_end_time between #{dateRangeBegin} and date_add(#{dateRangeEnd},interval 1 day)
    
    
        and status = #{status}
    
    and coupon_no in
    
        #{couponNo}
    

order by id desc
limit #{offset}, 1
select

from msp_voucher
where id  #{id}

   and voucher_no = #{voucherNo}


    and voucher_end_time between #{dateRangeBegin} and date_add(#{dateRangeEnd},interval 1 day)


    and status = #{status}

and coupon_no in

    #{couponNo}

order by id desc
limit #{size}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/963984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号