2014-03-13 73 views
0

有時我在表中有超過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

我隨機化'numbers'字段,因爲當前的數字按順序排序。一旦它被隨機化,我將每個批次導出爲CSV。 – user1246800

回答

1

如果您不需要有每批次正好10萬的記錄,你可以使用蘭特()來建立一個隨機字符串批,並進行統計,每批應該有〜10萬點的記錄

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