mysql批量插入

错误写法

insert into `oactivity_people`(hr_id,type,remark,open_id) values(
select * from (select hr_id,'massage','高管',wx_id from staff where st_id >4610 and st_id <6000 and wx_id is not null )
)  

快捷技巧
select group_concat(distinct COLUMN_NAME) from information_schema.COLUMNS where table_name = ‘表名’;

正确写法

insert into `oactivity_people`(hr_id,type,remark,open_id) 
select hr_id,'massage','高管',wx_id from staff where st_id >4610 and st_id <6000 and wx_id is not null  
UPDATE omeal_record
        SET batch_id = #{batchId}
        WHERE  order_date = CURRENT_DATE
            AND batch_id IS NULL
            and lunch = 1
            AND id IN (
                       select id from (
                          SELECT	omr.id
                                     FROM	meal_record omr,
                                    meal_statitics oms
								      where 	omr.dept_code = oms.dept_code
                                                AND omr.batch_id IS NULL
                                                and omr.lunch = 1
                                                and omr.order_date = CURRENT_DATE
								                and oms.order_date = CURRENT_DATE
								                and oms.company_type = #{companyType}
								                and omr.company_type = #{companyType}
                                     ORDER BY    oms.lunch_num,omr.dept_code,omr.dept_level,omr.real_dept_code
								     limit #{perBatchSize}
                                     ) t
                       )

见其他书签优化(inset value (),(),()…其实不快)[LOAD DATA]

    saveBatch(notNullList, 1000);   // 1953条 5034ms
        insertTask.stop();
        log.info("重新生成工作日之批量插入用时:{}", insertTask.getTotalTimeMillis());
    }

    /**
     * 多线程+批量插入
     *
     * @param list
     * @param dealSize
     */
    private void saveBatch(List<EhrCheckworkAttendanceDetailsEntity> list, int dealSize) {
        int totalSize = list.size();
        //线程数
        int runSize = (totalSize / dealSize) + 1;
        //thread
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("ehr-saveBatch-%d").build();
        ExecutorService executor = new ThreadPoolExecutor(5, 10, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), namedThreadFactory);
        CountDownLatch latch = new CountDownLatch(runSize);

        for (int i = 0; i < runSize; i++) {
            //计算每个线程执行的列表数据
            int startIndex = (i * dealSize);
            List<EhrCheckworkAttendanceDetailsEntity> threadList;
            if ((i + 1) == runSize) {
                int endIndex = totalSize;
                threadList = list.subList(startIndex, endIndex);
            } else {
                int endIndex = (i + 1) * dealSize;
                threadList = list.subList(startIndex, endIndex);
            }

            executor.execute(
                    () -> {
                        try {
                            ehrCheckworkAttendanceDetailsService.insertBatch(threadList);
                        } finally {
                            latch.countDown();
                        }
                    });
        }

        try {
            latch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
        executor.shutdown();
        try {
            //等待所有子线程任务执行完毕再结束
            executor.awaitTermination(1, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值