利用procedure批量插入数据

2022-10-14,,,,

正文

  要求在页面查询到5000条数据,为了方便插入,准备用shell脚本写curl命令调用自己写的代码接口,但是速度慢,而且写的时候遇到点儿小问题,故用sql语句写了这个功能
  由于operationlog表中的ts字段为13位的时间戳,所以采用了截取的方式。

drop table if exists `operationlog`;
create table `operationlog` (
  `sn` int(11) not null auto_increment,
  `opl` varchar(8) not null,
  `src` varchar(32) not null,
  `pid` varchar(32) default null,
  `ts` varchar(13) not null,
  primary key (`sn`)
) engine=innodb default charset=utf8;

drop procedure if exists batchadd;

/*count1 循环次数 opl和src为operationlog的列*/
create procedure batchadd(in count1 int,in opl varchar(32),in src varchar(32))
begin
    declare a int;
    set a=0;
    while a<count1 do
        begin
            /*延时1s*/
            select sleep(1);
            /*获取时间戳1523285555.207000,后面3位是0,现在的需求是ts为13位,即带ms的*/
            select @time1:=unix_timestamp(now(3));
            /*将1523285555.207000的.去掉*/
            select @time1:=replace(@time1, '.', '');
            /*取1523285555207000左边13位*/
            select @time1:=left(@time1, 13);
            /*生成sql,进行insert*/
            insert into operationlog(opl, src, pid, ts) values(opl, src, '1111', @time1);
            /*a加1*/
            set a = a + 1;
        end;

    end while;
end;

--查看procedure
show procedure status;

--调用该procedure
call batchadd(10, 'info', 'ajg');

--删除procedure
drop procedure batchadd;

create procedure batchadd如图所示:

创建好procedure后,可以通过call batchadd(10, 'info', 'ajg');来调用,如下图所示:



本公众号免费提供csdn下载服务,海量it学习资源,如果你准备入it坑,励志成为优秀的程序猿,那么这些资源很适合你,包括但不限于java、go、python、springcloud、elk、嵌入式 、大数据、面试资料、前端 等资源。同时我们组建了一个技术交流群,里面有很多大佬,会不定时分享技术文章,如果你想来一起学习提高,可以公众号后台回复【2】,免费邀请加技术交流群互相学习提高,会不定期分享编程it相关资源。


扫码关注,精彩内容第一时间推给你

《利用procedure批量插入数据.doc》

下载本文的Word格式文档,以方便收藏与打印。