Mysql优化:大表上的Update语句

时间:2013-06-06 13:46:41

标签: mysql

我有两张桌子:

球员稳定,可参赛(见下文)

playerstatstable有100,000行,且entrytable有1 mil行

我想要做的是更新playertatstable如下

UPDATE playerstatstable set totalScore = totalScore+1000 where PlayerID = (Select PlayerID from entrytable where score = 1 and entryState = 5 and playerstatstable.PlayerID = entrytable.PlayerID);

大约1/10的参赛作品将处于状态5,而大约1/5的作品将得分= 1;

所以我有大约20,000个条目与20,000个需要更新的playertatstable行相关联。

1)是否有更好的更新声明?
2)是否有任何指数/关键安排可以改善这一点?

CREATE TABLE `playerstatstable` (   
  `PlayerID` int(11) NOT NULL,  
  `totalScore` int(11) DEFAULT '0', 
  PRIMARY KEY (`PlayerID`), 
  UNIQUE KEY `PlayerID_UNIQUE` (`PlayerID`),    
  KEY `idx_m` (`monthTime`),    
  KEY `idx_w` (`weekTime`), 
  KEY `idx_d` (`dayTime`)   
) ENGINE=InnoDB DEFAULT CHARSET=utf8;   


CREATE TABLE `entrytable` (
  `EntryID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `PlayerID` int(10) unsigned DEFAULT '0',
  `entryType` bit(1) DEFAULT b'0',
  `entryState` int(11) DEFAULT '1',
  `score` int(11) DEFAULT '0',
  `rank` int(11) DEFAULT '0',
  PRIMARY KEY (`EntryID`),
  UNIQUE KEY `EntryID_UNIQUE` (`EntryID`)
) ENGINE=InnoDB

1 个答案:

答案 0 :(得分:1)

请试试这个。

UPDATE t1 set t1.totalScore = t1.totalScore+1000 
from playerstatstable t1  inner join entrytable t2
on t.PlayerID = t2.PlayerID 
and t2.score = 1 and t2.entryState = 5

对于MYSQL

UPDATE
playerstatstable t1, entrytable t2
set t1.totalScore = t1.totalScore+1000 
where t.PlayerID = t2.PlayerID 
and t2.score = 1 and t2.entryState = 5