delimiter //
drop PROCEDURE if exists backfill//
create PROCEDURE backfill()
BEGIN
declare id1 INT(10) UNSIGNED  default '0';   
declare im1 VARCHAR(20)  default NULL;
declare id2 INT(10) UNSIGNED  default '0';
declare im2 VARCHAR(20)  default NULL;
declare finished int unsigned default '0';declare cursor1 cursor for select id,im from flow_exfodm_0726_11 ORDER BY id asc,im desc ;
DECLARE CONTINUE HANDLER FOR NOT FOUND set finished = 1;          
open cursor1;                                                                    
fetch next from cursor1 into id1,im1;
fill_im_lable:loop
fetch next from cursor1 into id2,im2;
IF finished then
      LEAVE fill_im_lable;
END IF;
if id2 = id1 and im1 is not null and im2 is null then 
update  flow_exfodm_0726_2 set im = im1 where id = id2 and im is NULL;  #更新表的语句     
end if;
set id1 = id2;
set im1 = im2;             
end loop fill_im_lable;  
close cursor1;                
END //
deallocate cursor1;
delimiter ;
id都是不为空的,im可以有空。
以上是mysql写的存储过程,完成的功能是:遍历整张表,如果id1=id2,那么id1对于的im1值,就填到id2对于的im2的位置。但是执行的速率较慢,请大神帮助优化,感激不尽啊!!!!!