mysql - A more efficient way to do this select and update loop -
this table votes
.
"id" "votedelm" "votetype" "voteprocessed" "country" "3" "6" "1" "0" "us"//1-1=0 "4" "8" "0" "0" "us"//2+0-1=1 "9" "8" "1" "0" "us" "5" "9" "0" "0" "us"//2+0-1=1 "10" "9" "1" "0" "us"
and table likes
"id" "type" "parent" "country" "votes" 6 10 3 1 8 10 7 2 9 10 7 2
i update table likes
doing in event:
//pseudocode - inside mysql scheduled event //select votes select id, votedelm, votetype, country votes if votetype = 0 update likes set votes=votes+1 id=votedelm , country=country update votes set voteprocessed = 1 id = id elseif votetype = 1 update likes set votes=votes-1 id=votedelm , country=country update votes set voteprocessed = 1 id = id end if
this whole things takes 1 row @ time. see better , more efficient way sql here?
heres event:
begin declare vid int(10) default '0'; declare velm int(10) default '0'; declare vtype tinyint(1) default '0'; declare vprocessed tinyint(1) default '0'; declare vcountry varchar(2) default ""; declare updatedone int default false; -- declare cursor employee email declare updater cursor select id, votedelm, votetype, voteprocessed, country votes; -- declare not found handler declare continue handler not found set updatedone = true; open updater; doupdate: loop fetch updater vid, velm, vtype, vprocessed, vcountry; if updatedone leave doupdate; end if; -- update likes update likes inner join votes on votes.velm = likes.id , votes.vcountry = likes.country set likes.votes = if(votes.vtype = 0,likes.votes+1,likes.votes-1), votes.vid = 1; end loop doupdate; close updater; end
you can try this:
update likes inner join votes on votes.votedelm = likes.id , votes.country = likes.country set likes.votes = if(votes.vote_type = 0,likes.votes+1,likes.votes-1), votes.voteprocessed = 1 votes.voteprocessed = 0
you can read more multiple updates here
Comments
Post a Comment