天天看點

mysql 循環插入 插入sql語句 time_mysql循環插入資料

需要在資料庫中根據product表中的自增id,如果存在則插入對應的資料到pageview表,資料有幾萬條

drop procedure if exists autoInsert;

create procedure autoInsert()

begin

declare i int;

declare j int;

set i=1;

while(i<40001) do

if exists(select auto_id from product where auto_id=i ) then

set j=i;

if (select count(game_id) from pageview where game_id=j)=0  then

insert into pageview(game_id,week_pv,last_week_pv,month_pv,total_pv,admin_time) values(i,100+i,50+i,500+i,2000+i,now());

end if;

end if;

set i=i+1;

end while;

end

(1)其中循環嵌套時,如果直接嵌套發現總是執行不成功,是以加上了set j = i;

(2)執行效率問題,由于資料量巨大,是以需要将表product和表pageview對應的字段都建立索引。

(3)if語句中選擇的東西要保證是索引值,這樣效率就會提升很多

最終插入4W條資料花費時間:

影響的資料欄: 1  //其中1并不是記錄數

時間: 10.266ms

分享到:

mysql 循環插入 插入sql語句 time_mysql循環插入資料
mysql 循環插入 插入sql語句 time_mysql循環插入資料

2011-09-17 16:00

浏覽 685

分類:資料庫

評論