我必須從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
你有重組的表,因爲它不覺得我的權利,你的存儲方式,並與數據工作的選項。你在那裏有數據的歷史。你爲什麼不讓主機/服務獨一無二? – DanFromGermany
我無法更改表格的結構。我是應用/系統管理員而不是應用開發人員。該查詢只能使用一次。由於系統錯誤,我們有很多未連接的數據。 – Dream
如果你沒有正確地構建你的數據庫,你將永遠有不時的錯誤數據;-) – DanFromGermany