数据库水平分割,动态表名查询

     MySQL本身受单表数据文件大小限制,数据量将成为性能瓶颈。当单表数据量很大,或预计会很大时,将单个大表和单个大表数据文件,拆分成多个小表就是一个简单有效的提升新能的方式

由于业务上需要对数据存储六个月,六个月数据将是百万级甚至更多,为了提升性能,所以做了拆分

1、通过取模%5,将一张大表拆分成五张小表,并通过取模来存不同的表

     我这边是通过人员卡号ID(card)来取模,方便查看也方便存取

 

2、动态拼接查询表简单处理如下

我这里动态查询的逻辑如下,查询指定时间间隔的数据,人员历史轨迹查询

mapper层如下

public interface PersonPointMapper extends BaseMapper<PersonPoint> {


   List<PersonPoint> findDetailsByCarId(@Param("req") PersonPointREQ personPointREQ);
}

XML如下

数据库只能采用${}方式进行拼接,其他的采用#{}进行拼接,防止SQL注入

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.nswi.kuangshanapi.mapper.PersonPointMapper">
    <select id="findDetailsByCarId" resultType="PersonPoint">
        select * from ${req.tableName} where (
        DATE_FORMAT(#{req.startTime},'%i') - DATE_FORMAT(time,'%i'))%#{req.frequency} = 0
        and time between #{req.startTime} and #{req.endTime} and card = #{req.card} ORDER BY time ASC;
    </select>
</mapper>

请求参数如下

@Data
public class PersonPointREQ {

    /**
     * page
     */
    Long page;
    /**
     * size
     */
    Long size;
    /**
     * 人员卡ID
     */
    String card;

    /***
     * 开始时间
     */
//    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    Date startTime;
    /***
     * 结束时间
     */
//    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    Date endTime;

    /**
     * 频率
     */
    int  frequency;
    /**
     * 数据库表名称
     */
    String tableName;

}

接口如下

动态的改变表名

 @PostMapping("/listPageTime")
    public Result findDetailsByCarIdController(@RequestBody PersonPointREQ personPointREQ) {
        String card = personPointREQ.getCard();

        if(StringUtils.isNotEmpty(card)){
            int a=Integer.parseInt(card);
            int pointTable=a%5;
            if(pointTable==0){
                personPointREQ.setTableName("tb_person_point_history0");

            }else if(pointTable==1){
                personPointREQ.setTableName("tb_person_point_history1");

            }else if(pointTable==2){
                personPointREQ.setTableName("tb_person_point_history2");

            }else if(pointTable==3){
                personPointREQ.setTableName("tb_person_point_history3");
            }else if(pointTable==4){
                personPointREQ.setTableName("tb_person_point_history4");
            }else {
                return  Result.error("card格式错误");
            }
            return iPersonPointService.findDetailsByCarId(personPointREQ);
        }else {
            return   Result.error("card不能为空");
        }



    }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值