数据库存储过程
这个东西目前用的不多,但是很强大,性能很高,用python插入三百万数据非常慢,CPU还烧到90度,存储过程执行了存储语句之后,相当迅速
复杂的存储过程
存储过程:保存在MySQL上的一个别名 => 一坨SQL语句
别名()
用于替代程序员写SQL语句
方式一:数据库存储过程,程序调用
MySQL: 存储过程
程序:调用存储过程
方式二:程序直接写语句
MySQL:。。
程序:SQL语句
方式三:程序利用类和对象
MySQL:。。
程序:类和对象(SQL语句)
1. 简单
create procedure p1()
BEGIN
select * from student;
INSERT into teacher(tname) values("ct");
END
call p1()
cursor.callproc('p1')
2. 传参数(in,out,inout)
delimiter //
create procedure p2(
in n1 int,
in n2 int
)
BEGIN
select * from student where sid > n1;
END //
delimiter ;
call p2(12,2)
cursor.callproc('p2',(12,2))
3. 参数 out
delimiter //
create procedure p3(
in n1 int,
inout n2 int
)
BEGIN
set n2 = 123123;
select * from student where sid > n1;
END //
delimiter ;
set @v1 = 10;
call p2(12,@v1)
select @v1;
set @_p3_0 = 12
ser @_p3_1 = 2
call p3(@_p3_0,@_p3_1)
select @_p3_0,@_p3_1
cursor.callproc('p3',(12,2))
r1 = cursor.fetchall()
print(r1)
cursor.execute('select @_p3_0,@_p3_1')
r2 = cursor.fetchall()
print(r2)
=======> 特殊
a. 可传参: in out inout
b. pymysql
cursor.callproc('p3',(12,2))
r1 = cursor.fetchall()
print(r1)
cursor.execute('select @_p3_0,@_p3_1')
r2 = cursor.fetchall()
print(r2)
为什么有结果集又有out伪造的返回值?
delimiter //
create procedure p3(
in n1 int,
out n2 int 用于标识存储过程的执行结果 1,2
)
BEGIN
insert into vv(..)
insert into vv(..)
insert into vv(..)
insert into vv(..)
insert into vv(..)
insert into vv(..)
END //
delimiter ;
这个就是我写的简单的插入数据库三百万条数据的存储过程
表中插入3百万行数据:
delimiter //
create procedure p10()
begin
declare i int;
set i=1;
while i<=3000000 do
insert into helloxu(sid,sname,email,gender) values
(i,CONCAT("alex",i),CONCAT("fox",i),"man");
set i=i+1;
end while;
end //
delimiter ;
call p10()

浙公网安备 33010602011771号