2017-08-28 72 views
1

我必須從MySQL表中刪除40 mln行。MySQL加入和刪除只發生第一次發生

我必須找到與輸出的所有行 - >「靜態輸出」 然後刪除上面的輸出行和刪除其上面的輸出比其他產值「的同一臺主機 和服務的下一行STATIC OUTPUT「。

的樣本數據:

- > ID,主機,服務,輸出

1,"127.0.0.1","service1","STATIC OUTPUT" 
2,"127.0.0.2","service5","RANDOM OUTPUT X0" 
3,"127.0.0.2","service5","STATIC OUTPUT" 
4,"127.0.0.3","service1","RANDOM OUTPUT X1" 
5,"127.0.0.3","service10","RANDOM OUTPUT X2" 
6,"127.0.0.2","service5","RANDOM OUTPUT X3" 
7,"127.0.0.1","service2","RANDOM OUTPUT X4" 
8,"127.0.0.1","service1","RANDOM OUTPUT X5" 
9,"127.0.0.2","service4","RANDOM OUTPUT X6" 
10,"127.0.0.3","service10","RANDOM OUTPUT X7" 
11,"127.0.0.1","service1","RANDOM OUTPUT X7" 
12,"127.0.0.1","service1","RANDOM OUTPUT X8" 
13,"127.0.0.1","service1","RANDOM OUTPUT X9" 
14,"127.0.0.2","service5","RANDOM OUTPUT X10" 
15,"127.0.0.1","service1","STATIC OUTPUT" 
16,"127.0.0.1","service1","RANDOM OUTPUT X11" 
17,"127.0.0.1","service1","RANDOM OUTPUT X12"  
... 

例子:當我們發現

1,"127.0.0.1","service1","STATIC OUTPUT" 

我們應該刪除ID爲值的行1和8,

8,"127.0.0.1","service1","RANDOM OUTPUT X5" 

當我們發現

3,"127.0.0.2","service5","STATIC OUTPUT" 

我們應該刪除與ID值3和6行,

6,"127.0.0.2","service5","RANDOM OUTPUT X3" 

我寫了這樣的事情(選擇,因爲測試查詢的DELETE語句的這一翻譯),

SELECT * FROM data r1 INNER JOIN (SELECT id, host, service 
FROM data 
WHERE output = 'STATIC OUTPUT') r2 ON 
     r1.id>r2.id AND r1.service=r2.service 
     AND r1.host=r2.host 
     AND r1.output<>'STATIC OUTPUT' 
GROUP BY r1.host, r1.service 

但我認爲這是一種錯誤的方式。

MySQL 5.1.73

+0

你有重組的表,因爲它不覺得我的權利,你的存儲方式,並與數據工作的選項。你在那裏有數據的歷史。你爲什麼不讓主機/服務獨一無二? – DanFromGermany

+0

我無法更改表格的結構。我是應用/系統管理員而不是應用開發人員。該查詢只能使用一次。由於系統錯誤,我們有很多未連接的數據。 – Dream

+0

如果你沒有正確地構建你的數據庫,你將永遠有不時的錯誤數據;-) – DanFromGermany

回答

1

校正

現在,這應該這樣做!:

SELECT min(sp.id) as id FROM 
(SELECT hs.id, hs.host, hs.service, hs.output, so.id as soid 
FROM data hs 
INNER JOIN 
(SELECT id,host,service,output FROM data 
WHERE output = "STATIC OUTPUT") so 
ON so.host = hs.host and so.service = hs.service 
AND hs.id > so.id WHERE hs.output <> "STATIC OUTPUT") sp 
group by host,service, soid 
UNION 
SELECT id FROM data WHERE output = "STATIC OUTPUT"; 
+0

有什麼不對。在測試桌上,我得到了多個具有相同主機和服務的記錄。也許我沒有顯示足夠的數據。相同主機和服務的每個下一行的值輸出是不同的。我得到了全部,不僅是「STATIC OUTPUT」之後的第一排,同樣的主機和服務。 – Dream

+0

你能提供更多的測試數據嗎? –

+0

我又增加了4行。在你的查詢中,我也得到了11-14。 – Dream

-1

您可以使用LIMIT語句選擇第一個匹配項。 https://dev.mysql.com/doc/refman/5.7/en/select.html

SELECT * FROM data LIMIT 1;

編輯:

此示例會發現你的ID來擦除

 
    CREATE OR REPLACE VIEW v AS 
    SELECT r1.id as id1 , r2.id as id2 FROM data r1 
    INNER JOIN data r2 ON r1.host=r2.host AND r1.service = r2.service 
    WHERE LOWER(r1.output) LIKE "static output" AND r1.id < r2.id; 
 
    SELECT DISTINCT id1 FROM v 
    UNION 
    SELECT DISTINCT id2 FROM v; 

輸出:

ID:1 3 6 8

+0

限制在這裏沒有用,只是使用我給出的答案 – kenfire

+0

輸出(刪除)應該是:1 3 6 8.對 - > 1,8和3,6。在2之前沒有「靜態輸出」具有相同的IP和服務。 – Dream

+0

現在使用視圖,您可以更輕鬆地做到這一點(具有所需的輸出) – kenfire