[英]update a random row from a table
I've got this code here: 我在这里有此代码:
UPDATE starinformation SET starOwner = -1 WHERE (
SELECT starinformation.starID
ORDER BY RAND( )
LIMIT 1
)
I want to update a random row from a table with information pulled from that table and only effect 1 row. 我想使用从该表中提取的信息更新表中的随机行,并且仅影响1行。
There are a lot of problems with this approach: 这种方法有很多问题:
ORDER BY RAND() LIMIT 1
is a major performance killer: It will create an intermediate result of ALL rows - ie if your table contains 1 megarows, you will touch every single one of them. ORDER BY RAND() LIMIT 1
是主要的性能杀手:它将创建所有行的中间结果-即,如果表包含1兆行,则将触摸其中的每一个行。 My recommendation is to 我的建议是
SELECT ROUND(RAND()*COUNT(*)) AS num FROM starinformation
, returning $num SELECT ROUND(RAND()*COUNT(*)) AS num FROM starinformation
运行SELECT ROUND(RAND()*COUNT(*)) AS num FROM starinformation
,返回$ num UPDATE starinformation SET starOwner = -1 ORDER BY [pk-field] LIMIT $num,1
UPDATE starinformation SET starOwner = -1 ORDER BY [pk-field] LIMIT $num,1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.