2012-01-26 51 views
7

我現在有一個有604 000行的表格。我想丟棄4000個隨機行,所以我的表只會包含600 000個條目。Mysql從表格中刪除多個隨機行

會有一個快速的方法來做到這一點?

非常感謝。

+1

'DELETE FROM表WHERE ID = RAND()LIMIT 4000'? :)或'RANDOM()'?也不確定;) – Vyktor

回答

13

理論上,這將是隨機的和快速的。在實踐中,這將是隻快:

DELETE FROM tableX 
LIMIT 4000 

這將是隨機的,但非常緩慢,600K行:

DELETE FROM tableX 
ORDER BY RAND() 
LIMIT 4000 

這會不會是真正隨機的(如通常在ID中有間隙),它甚至可能不會刪除正好4000行(但是當有很多間隙時少一些),但它可能比以前更快。

在子查詢中的一個額外的包裝是需要的,因爲從多個表中爲刪除的語法不允許LIMIT

DELETE td 
FROM 
     tableX AS td 
    JOIN 
     (SELECT t.id 
     FROM 
      tableX AS t 
      CROSS JOIN 
       (SELECT MAX(id) AS maxid 
       FROM tableX 
      ) AS m 
      JOIN 
       (SELECT RAND() AS rndm 
       FROM tableX AS tr 
       LIMIT 5000 
      ) AS r 
      ON 
       t.id = CEIL(rndm * maxid) 
     LIMIT 4000 
    ) AS x 
     ON 
     x.id = td.id 

解釋輸出(子查詢,從400K行表):

id   table  possible_keys key_len  rows 
select_type   type    key  ref   Extra 
1 PRIMARY <derived2> system        1 
1 PRIMARY <derived3> ALL        5000 
1 PRIMARY t   eq_ref PRIMARY PRIMARY 4 func  1 Using where;Using index 
3 DERIVED tr   index   PRIMARY 4  398681 Using index 
2 DERIVED           Select tables optimized away 
+0

釘住它!謝謝 – silkAdmin

+0

請注意,它可能會刪除少於4000行! –

1
delete from yourTable limit 4000 
+0

唉!感謝:) – silkAdmin

+1

他想隨機。) – Vyktor

+0

這是隨機的。您無法確定哪些行將被刪除。 –

0
DELETE FROM TABLE ORDER BY RAND() LIMIT 4000; 

這需要時間,但...

一個更快的w^AY執行(不寫代碼!)可能是在一個循環

DELETE FROM TABLE WHERE AssumedPKisInt = <ARandomNumber> 

當然4000項獨立刪除,你需要確保你不嘗試刪除不存在或已刪除的行。

0

如果我不得不冒昧猜測:

DELETE FROM table where id = (SELECT id FROM table ORDER BY rand() LIMIT 1) LIMIT 10