[英]Better strategy to update mysql table on distinct column from another table
I have 2 mysql-tables (MariaDB) - games
and lineups
where the latter is filled from a third source.我有 2 个 mysql-tables (MariaDB) -
games
和lineups
,后者是从第三个来源填充的。
The logic behind them is: Each entry in the first table 'games' should have corresponding multiple entries of players participating in the respective game in the second table 'lineups'.它们背后的逻辑是:第一个表“游戏”中的每个条目都应该有对应的多个参与第二个表“阵容”中相应游戏的玩家条目。
So it looks like this:所以它看起来像这样:
games
matchid (Unique Key) some_data ... requestedPlayers
100 "foobar" "bar" 1
101 "foobar" "bar" 1
102 "foobar" "bar" 0
lineups
matchid playerid some_data ...
100 1 ... ...
100 2 ... ...
101 1 ... ...
101 2 ... ...
So when the players for a matchid are already in the lineups
table, the boolean column requestedPlayers
should be set to 1
.因此,当 matchid 的球员已经在
lineups
表中时,boolean 列requestedPlayers
应设置为1
。 My python code then reads the matchids into self.gameids
from the table matches with a simple: SELECT matchID FROM games WHERE requestedPlayers = 0
.我的 python 代码然后将 matchids 从表中读取到
self.gameids
中,使用一个简单的: SELECT matchID FROM games WHERE requestedPlayers = 0
。 Then it loops through the IDs, retrieves data from 2nd source, parses it, and collects in in self.lineupdata
.然后它遍历 ID,从第二个来源检索数据,解析它,并在
self.lineupdata
中收集。
The corresponding code looks like this:对应的代码如下所示:
def lineups(self,number:int=1000):
self._read_gameIds() # `SELECT matchID FROM games WHERE requestedPlayers = 0`
try:
for gid in self.gameids:
if number ==0: break
print(gid)
self._request_match(gid)
number -= 1
finally:
if len(self.lineupdata) >0:
self._write_lineups()
self._update_gameIds()
I do this in batches (batch size regulated by number
) and the code crashes regularily as the data is parsed and it isn't clean, so a lot of unexpected things happen.我分批执行此操作(批量大小由
number
调节),并且代码在解析数据并且不干净时会定期崩溃,因此会发生很多意想不到的事情。 By catching the exceptions I finally
write the retrieved data into the database ( self._write_lineups()
) and in a last step update the matches
table column requestedLineups
with this mySQL-statement:通过捕获异常,我
finally
将检索到的数据写入数据库( self._write_lineups()
),并在最后一步使用此 mySQL 语句更新matches
表列requestedLineups
:
'''UPDATE games g, lineups l
SET g.requestedLineUp = 1
WHERE g.matchID IN (SELECT DISTINCT matchID FROM lineups)'''
This worked fine for some time, but as lineups grows, this takes more and more time so I am looking for an alternative strategy now.这在一段时间内运行良好,但随着阵容的增长,这需要越来越多的时间,所以我现在正在寻找一种替代策略。 So my questions are:
所以我的问题是:
Is there efficient way to UPDATE
the requestedLineUp
-Column?是否有有效的方法来
UPDATE
requestedLineUp
-Column?
Or is there a different way the whole process needs to be done?或者整个过程需要以不同的方式完成? Maybe it would be better to write into
lineups
after each matchid and not collect them before writing.也许最好在每个 matchid 之后写入
lineups
,而不是在写入之前收集它们。 Then I could update the respective matchid-row in matches in the same step.然后我可以在同一步骤中更新匹配中的相应 matchid-row。 Is there a best parctice from a mysql-perspective?
从 mysql 的角度来看,有没有最好的方法?
UPDATE games
SET requestedLineUp = EXISTS ( SELECT NULL
FROM lineups
WHERE games.matchID = lineups.matchID
)
/* WHERE requestedPlayers = 0 -- ??? */;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.