午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    刪除MySQL重復(fù)數(shù)據(jù)的方法
    來源:易賢網(wǎng) 閱讀:855 次 日期:2015-01-20 11:00:06
    溫馨提示:易賢網(wǎng)小編為您整理了“刪除MySQL重復(fù)數(shù)據(jù)的方法”,方便廣大網(wǎng)友查閱!

    本文實(shí)例講述了刪除MySQL重復(fù)數(shù)據(jù)的方法。分享給大家供大家參考。具體方法如下:

    項(xiàng)目背景

    在最近做的一個linux性能采集項(xiàng)目中,發(fā)現(xiàn)線程的程序入庫很慢,再仔細(xì)定位,發(fā)現(xiàn)數(shù)據(jù)庫里面很多冗余數(shù)據(jù)。因?yàn)樵诓杉?,對于同一臺設(shè)備,同一個時間點(diǎn)應(yīng)該只有一個數(shù)據(jù),然而,數(shù)據(jù)庫中存入了多個數(shù)據(jù)。對于如何造成了這個結(jié)果,一時沒有想清楚,但為了解決入庫慢的問題,首先要刪除冗余數(shù)據(jù)。

    問題描述

    數(shù)據(jù)庫的表結(jié)構(gòu)很簡單,如下:

    復(fù)制代碼 代碼如下:+----------------+--------------+------+-----+---------+-------+

    | Field | Type | Null | Key | Default | Extra |

    +----------------+--------------+------+-----+---------+-------+

    | id | varchar(255) | NO | PRI | NULL | |

    | conf_id | varchar(255) | NO | MUL | NULL | |

    | insert_time | datetime | YES | | NULL | |

    | cpu_usage | float(11,2) | YES | | NULL | |

    | memory_usage | float(11,2) | YES | | NULL | |

    | io_usage_write | float(11,2) | YES | | NULL | |

    | io_usage_read | float(11,2) | YES | | NULL | |

    +----------------+--------------+------+-----+---------+-------+

    查詢所有數(shù)據(jù)量

    復(fù)制代碼 代碼如下:select count(*) from perf_linux;

    輸出 427366

    查詢所有時間點(diǎn)不同設(shè)備的數(shù)據(jù)量

    復(fù)制代碼 代碼如下:select count(distinct conf_id, insert_time) from perf_linux ;

    輸出42387

    由上面的數(shù)據(jù)可以看出,數(shù)據(jù)冗余了10倍左右。

    再按時間分組看一下:

    復(fù)制代碼 代碼如下:select id, conf_id ,insert_time from perf_linux order by insert_time, conf_id;

    輸出:

    復(fù)制代碼 代碼如下:| 2a79f7cd-43a9-4c7b-adb2-316b6c04283e | 1 | 2014-12-09 15:09:14 |

    | 50d6f6c2-9c8b-45fd-98fd-2be211221cfd | 1 | 2014-12-09 15:09:14 |

    | 740b52e1-e868-4074-ba36-74e2634401b3 | 1 | 2014-12-09 15:09:14 |

    | 8b0096a4-9e85-417b-a131-e3505ca79a9c | 1 | 2014-12-09 15:09:14 |

    | 90a9e882-5220-4508-a56f-8d4ab4a7929b | 1 | 2014-12-09 15:09:14 |

    | d17403ed-24a4-45e8-b51b-2a95118383d9 | 1 | 2014-12-09 15:09:14 |

    | 0c2da917-579b-4080-857d-7159f38b44ac | 2 | 2014-12-09 15:09:14 |

    | 263083eb-8f63-4d2b-a03f-3320aa678735 | 2 | 2014-12-09 15:09:14 |

    | d6c57a38-080b-465a-a55a-beafd9daf32d | 2 | 2014-12-09 15:09:14 |

    | f672227b-1fb8-4b85-880d-2cc34b02880d | 2 | 2014-12-09 15:09:14 |

    | f80020fe-6cb5-48ec-beb0-4e8ebeb0ca57 | 2 | 2014-12-09 15:09:14 |

    | ff633a35-824d-49ba-b78c-5bcc5df8d1cc | 2 | 2014-12-09 15:09:14 |

    | 5c41e48a-abfc-4108-a00e-ca7def7d5a5a | 3 | 2014-12-09 15:09:14 |

    | 60b7ab9e-c91a-4020-a6d3-7bceb1dc47c5 | 3 | 2014-12-09 15:09:14 |

    | 7b6cd2b8-ac6d-43eb-8858-e15885e676c8 | 3 | 2014-12-09 15:09:14 |

    | d53a3df5-08c4-4604-8fac-cb51077935f6 | 3 | 2014-12-09 15:09:14 |

    | d9e4ba14-f98d-42a8-b3bc-2879d58aa797 | 3 | 2014-12-09 15:09:14 |

    | f56f82f6-32a7-47f7-ae07-b13168743884 | 3 | 2014-12-09 15:09:14 |

    | 076c4c1b-0028-4a9c-a8c4-de655bd6ab6b | 4 | 2014-12-09 15:09:14 |

    | 2a90ad9e-11a5-4707-95e8-78491da658ad | 4 | 2014-12-09 15:09:14 |

    | 3b17ad1d-e589-4b65-93a7-d61fc99b4071 | 4 | 2014-12-09 15:09:14 |

    | 6988d6cf-44ef-47f7-808d-09791caf2d90 | 4 | 2014-12-09 15:09:14 |

    | 8404d281-f9e5-4153-a47e-128c05386758 | 4 | 2014-12-09 15:09:14 |

    | e042e310-7ff2-4e4d-8c98-71e3e4d57828 | 4 | 2014-12-09 15:09:14 |

    +--------------------------------------+---------+---------------------+

    由上圖可見,同一個時間點(diǎn)的同一個設(shè)備的數(shù)據(jù)有冗余,現(xiàn)在我們要把這些冗余數(shù)據(jù)去掉。

    解決方法

    思路是這樣的:首先應(yīng)該按照conf_id和時間點(diǎn)來判斷,進(jìn)行分組(group by)查詢,每組中再取一個就可以。分組是很簡單,但是分組怎么取一個呢?我采用了中間表的形式。

    創(chuàng)建中間表,并把數(shù)據(jù)導(dǎo)入中間表

    復(fù)制代碼 代碼如下:create table perf_linux_t like perf_linux;

    insert into perf_linux_t select * from perf_linux;

    在中間表中增加一個字段,此字段是自增長的。

    復(fù)制代碼 代碼如下:ALTER TABLE `perf_linux_t`

    ADD COLUMN `auto_id` INT NOT NULL AUTO_INCREMENT ,

    DROP PRIMARY KEY,

    ADD PRIMARY KEY (`auto_id`);

    刪除無用數(shù)據(jù)

    先查詢一下

    復(fù)制代碼 代碼如下:select min(auto_id) as auto_id from perf_linux_t group by insert_time ;

    刪除不對的數(shù)據(jù)

    復(fù)制代碼 代碼如下:delete from perf_linux_t where auto_id not in (select min(auto_id) as auto_id from perf_linux_t group by insert_time);

    慢著,輸出錯誤:

    You can't specify target table 'perf_linux_t' for update in FROM clause

    不能刪除啊,那只能再建一個中間表了。

    再建中間表

    復(fù)制代碼 代碼如下:create table tmp like perf_linux_t;

    轉(zhuǎn)變思路,不刪除不符合的數(shù)據(jù),而是把符合的數(shù)據(jù)存到這張新表中。

    復(fù)制代碼 代碼如下:insert into tmp select * from perf_linux_t where auto_id in (select min(auto_id) as auto_id from perf_linux_t group by insert_time,conf_id );

    把這張表中的無用列刪除

    復(fù)制代碼 代碼如下:ALTER TABLE `tmp`

    DROP COLUMN `auto_id`,

    DROP PRIMARY KEY;

    導(dǎo)回?cái)?shù)據(jù)

    刪除原來的數(shù)據(jù)

    復(fù)制代碼 代碼如下:truncate table perf_linux;

    插入數(shù)據(jù)

    復(fù)制代碼 代碼如下:insert into perf_linux select * from tmp;

    刪除中間表

    復(fù)制代碼 代碼如下:drop table tmp;

    drop table perf_linux_t;

    總結(jié)

    通過這個方法,數(shù)據(jù)變?yōu)榱?2387條,刪除了冗余的數(shù)據(jù)。但實(shí)際上程序的問題并沒有完全定位,還需要觀察才能定位問題。

    希望本文所述對大家的mysql數(shù)據(jù)庫程序設(shè)計(jì)有所幫助。

    更多信息請查看IT技術(shù)專欄

    更多信息請查看數(shù)據(jù)庫
    易賢網(wǎng)手機(jī)網(wǎng)站地址:刪除MySQL重復(fù)數(shù)據(jù)的方法
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)