MYSQL表数据复制(指定数量)及JAVA代码实现分批复制数据

MYSQL表数据复制(指定数量)

INSERT INTO sichuan_mobile_v2_202307 SELECT * FROM sichuan_mobile_v2 WHERE create_time >='2023-07-01 00:00:00' and create_time <='2023-07-31 23:59:59' LIMIT 100000
由于业务中不指定数量的话,如果数据太多容易写入失败,故添加LIMIT 限制。

JAVA代码实现分批复制数据

相关业务代码展示:

public boolean movemsg(String lasttable, String starttime, String endtime) {
		// 2022年11月2日09:34:21,由于数据量已经400万了一次性写入失败,现优化:分批次写入。
		long maxId = -1L;
//		boolean run  =  true;
//		while(run) {
			String insertSql = " INSERT INTO " + lasttable + " SELECT * FROM sichuan_mobile_v2 WHERE create_time >='" + starttime + "' and  create_time <='" + endtime + "'";
			// 2.查询已经写入最大主键值
			String maxIdSql = " SELECT * FROM "+lasttable+" ORDER BY id DESC LIMIT 1 ";
			List<Map<String, Object>> list = dao.getSession().createSQLQuery("select xx.* from ( "+maxIdSql+" )xx ")
					.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
			if(list.size() >0) {
				if(list.get(0)!= null) {
					maxId = Long.parseLong(list.get(0).get("id").toString());
				}
			}
			// 1.用最大ID去主表查询是否还有数据
			String countSql = " SELECT count(*) num FROM sichuan_mobile_v2 WHERE create_time >='" + starttime + "' and  create_time <='" + endtime + "'"
					+ " and id > "+ maxId;
			String tempCount = dao.getSession().createSQLQuery("select xx.* from ( "+countSql+" )xx ").uniqueResult().toString();
			long count = Long.parseLong(tempCount);
			if(count > 0) {
				if(maxId > -1) {
					insertSql += " and id > " + maxId;
				}
				insertSql += " LIMIT 100000 "; // 10万条一批写入
				try {
					dao.insert(insertSql);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
//					run = false;
					return false;
				}
			}
//			else {
//				run = false;
//			}
//		}
		return true;
	}```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值