gpt4 book ai didi

mysql - 嵌套 while 循环比嵌套游标花费更多时间

转载 作者:行者123 更新时间:2023-11-29 10:35:58 26 4
gpt4 key购买 nike

我从两个 sql 语句获取数据后插入 n 行。到目前为止我已经使用了两种方法,第一种是 Cursor,另一种是 While 循环。

嵌套光标:

    begin
declare userId,taskId int default 0;
declare userCnt int default 0;
declare c1 cursor for select us_id from us_uxusermaster ;
declare continue handler for not found set userCnt=1;
open c1;
CheckId: loop
fetch c1 into userId;
if userCnt=1
then
leave CheckId;
end if;
Select pl.pl_minTarget into target from pl_planlist pl inner join ap_affiliateplan ap inner join us_uxusermaster us on Find_in_set(pl.pl_id,us.us_planListId) and ap.ap_id =us.us_taskPlanId where us_id=userId and pl.pl_serviceName=2;
Begin
DECLARE taskId int default 0;
Declare taskCnt int default 0;
Declare t1 cursor for select tk.tk_id from tk_taskmaster tk where tk.tk_activeTime=AddDate(Current_date(),1) and tk_actStatus=0 and tk_status=1 limit target;
Declare continue handler for not found set taskCnt=1;
open t1;
CheckTask: loop
fetch t1 into taskId;
if taskCnt=1
then
leave CheckTask;
end if;

insert into ut_userstask(ut_tk_id,ut_us_id,ut_edtm,ut_eby) values (taskId,userId,current_timestamp,'Via-Event');
end loop checkTask;
close t1;
End;
end loop CheckId;
close c1;

end;

While 循环:

    begin
declare taskName,taskCode,description,url,userLevel,TaskStatus,TaskActStatus,Steps,taskId,userId varchar(50);

declare activationTime,deActivationTime datetime;

Declare flag,flag2,counts,counts2 int default 0;
Drop Temporary Table if exists temptrigg;
Set @rownumber=0;
Set @rownumber2=0;
create temporary table temptrigg as(select * from (select (@rownumber := @rownumber + 1) AS newrow, us_id from us_uxusermaster) AS xst);
select count(*) into counts from temptrigg;
while(flag<counts)
Do
Set flag=flag+1;
Select us_id into userId from temptrigg where newrow=flag;

Drop Temporary Table if exists temptrigg2;
Create temporary table temptrigg2 as(select * from(select (@rownumber2 := @rownumber2 + 1) as newrow2,tk.tk_id from tk_taskmaster tk where tk.tk_activeTime=Current_date() and tk_actStatus=0 and tk_status=1)as xst);
Select count(*) into Counts2 from temptrigg2;
While(flag2<Counts2)
Do
Set flag2=flag2+1;
Select tk_id into taskId from temptrigg2 where newrow2=flag2;

insert into ut_userstask(ut_tk_id,ut_us_id,ut_edtm,ut_eby) values (taskId,userId,current_timestamp,'Via-Event');

End While;
End While;

end

这里的问题是 while 循环花费的时间是光标的两倍。我对其 future 的结果感到困惑。通过替换嵌套的 while 循环来跟随光标会好吗?

插入 425 行时,游标耗时 23.05 秒,而 while 循环耗时 46 秒。这两个时间对我来说都太多了。有没有其他方法可以提高性能。

很高兴知道是否有。

最佳答案

我不确定我是否捕获了其中的每一项检查(尤其是限制),但如果您可以将其压缩到单个插入中,那么会节省很多。像这样选择:

Insert into ut_userstask(ut_tk_id,ut_us_id,ut_edtm,ut_eby)
Select
tk.tk_id,
us.us_id,
current_timestamp,
'Via-Event'
from pl_planlist pl
inner join ap_affiliateplan ap
inner join us_uxusermaster us on ap.ap_id = us.us_taskPlanId
inner join tk_taskmaster tk on tk.tk_activeTime=AddDate(Current_date(),1) and tk_actStatus=0 and tk_status=1
where
pl.pl_serviceName=2
and Find_in_set(pl.pl_id,us.us_planListId)

其他需要记住的事情:确保你有正确的索引并尽量避免像 FIND_IN_SET 这样的函数。这通常表明您的数据库没有足够规范化,并且使用速度非常慢,因为它绕过了列上可用的任何索引。

即使您不能将所有内容放入一个选择中,循环遍历主游标(例如获取用户)并为游标的每一行执行 insert..select 可能仍然更快。

关于mysql - 嵌套 while 循环比嵌套游标花费更多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46424276/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com