随机化行更新

时间:2014-03-13 12:31:47

标签: mysql

有时我表中有超过1百万行。

每批次将有100,000个随机化行。

随机化行的最佳方法是什么?

我执行以下操作:

UPDATE  temp1 SET  `batch` =  '';
UPDATE  temp1 SET  `batch` =  'B1' WHERE batch = '' order by rand(number) limit 100000;
UPDATE  temp1 SET  `batch` =  'B2' WHERE batch = '' order by rand(number) limit 100000;
UPDATE  temp1 SET  `batch` =  'B3' WHERE batch = '' order by rand(number) limit 100000;
UPDATE  temp1 SET  `batch` =  'B4' WHERE batch = '' order by rand(number) limit 100000;
UPDATE  temp1 SET  `batch` =  'B5' WHERE batch = '' order by rand(number) limit 100000;

1 个答案:

答案 0 :(得分:1)

如果您不需要每批拥有100000条记录,则可以使用rand()构建随机批处理字符串,统计上,每批应该有~100000条记录

UPDATE temp1
JOIN   (
    SELECT number, CONCAT('B',CONVERT( FLOOR( RAND() * (nrows/10) ) + 1, CHAR(19) )) AS randBatch
    FROM   temp1
    CROSS JOIN ( SELECT COUNT(*) AS nrows FROM temp1 ) as x
   ) AS y
ON     y.number = temp1.number
SET    batch = randBatch

SQL小提琴:http://sqlfiddle.com/#!9/eb161/1