使用SQL語(yǔ)句去掉重復(fù)記錄的兩種方法

2019-01-01 23:21:54 來(lái)源:互聯(lián)網(wǎng)作者:deng214 人氣:2709 次閱讀 394 條評(píng)論

文章主要介紹了用SQL語(yǔ)句去掉重復(fù)的記錄的兩種方式,兩種方式都可以使用臨時(shí)表操作,具體實(shí)現(xiàn)方法大家跟隨小編一起通過(guò)本文學(xué)習(xí)吧海量數(shù)據(jù)(百萬(wàn)以上),其中有些全部字段都相同,有些...

文章主要介紹了用SQL語(yǔ)句去掉重復(fù)的記錄的兩種方式,兩種方式都可以使用臨時(shí)表操作,具體實(shí)現(xiàn)方法大家跟隨小編一起通過(guò)本文學(xué)習(xí)吧

海量數(shù)據(jù)(百萬(wàn)以上),其中有些全部字段都相同,有些部分字段相同,怎樣高效去除重復(fù)?

如果要?jiǎng)h除手機(jī)(mobilePhone),電話(huà)(officePhone),郵件(email)同時(shí)都相同的數(shù)據(jù),以前一直使用這條語(yǔ)句進(jìn)行去重:

  1. delete from where id not in 
  2. (select max(id) from group by mobilePhone,officePhone,email ) 
  3. or
  4. delete from where id not in 
  5.  (select min(id) from group by mobilePhone,officePhone,email )
  6.  
  7. delete from where id not in
  8. (select max(id) from group by mobilePhone,officePhone,email )
  9. or
  10. delete from where id not in
  11.  (select min(id) from group by mobilePhone,officePhone,email )

其中下面這條會(huì)稍快些。上面這條數(shù)據(jù)對(duì)于100萬(wàn)以?xún)?nèi)的數(shù)據(jù)效率還可以,重復(fù)數(shù)1/5的情況下幾分鐘到幾十分鐘不等,但是如果數(shù)據(jù)量達(dá)到300萬(wàn)以上,效率驟降,如果重復(fù)數(shù)據(jù)再多點(diǎn)的話(huà),常常會(huì)幾十小時(shí)跑不完,有時(shí)候會(huì)鎖表跑一夜都跑不完。無(wú)奈只得重新尋找新的可行方法,今天終于有所收獲:

  1. //查詢(xún)出唯一數(shù)據(jù)的ID,并把他們導(dǎo)入臨時(shí)表tmp中 
  2. select min(id) as mid into tmp from group by mobilePhone,officePhone,email 
  3.  //查詢(xún)出去重后的數(shù)據(jù)并插入finally表中 
  4. insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)
  5.  
  6. //查詢(xún)出唯一數(shù)據(jù)的ID,并把他們導(dǎo)入臨時(shí)表tmp中
  7. select min(id) as mid into tmp from group by mobilePhone,officePhone,email
  8.  //查詢(xún)出去重后的數(shù)據(jù)并插入finally表中
  9. insert into finally select (除ID以外的字段) from customers_1 where id in (select mid from tmp)

效率對(duì)比:用delete方法對(duì)500萬(wàn)數(shù)據(jù)去重(1/2重復(fù))約4小時(shí)。4小時(shí),很長(zhǎng)的時(shí)間。

用臨時(shí)表插入對(duì)500萬(wàn)數(shù)據(jù)去重(1/2重復(fù))不到10分鐘。

其實(shí)用刪除方式是比較慢的,可能是邊找邊刪除的原因吧,而使用臨時(shí)表,可以將沒(méi)有重復(fù)的數(shù)據(jù)ID選出來(lái)放在臨時(shí)表里,再將表的信息按臨時(shí)表的選擇出來(lái)的ID,將它們找出來(lái)插入到新的表,然后將原表刪除,這樣就可以快速去重啦。

SQL語(yǔ)句去掉重復(fù)記錄,獲取重復(fù)記錄

按照某幾個(gè)字段名稱(chēng)查找表中存在這幾個(gè)字段的重復(fù)數(shù)據(jù)并按照插入的時(shí)間先后進(jìn)行刪除,條件取決于order by 和row_num。

方法一按照多條件重復(fù)處理:

  1. delete tmp from
  2. select row_num = row_number() over(partition by 字段,字段 order by 時(shí)間 desc
  3.  from where 時(shí)間> getdate()-1 
  4.  ) tmp 
  5.  where row_num > 1
  6. delete tmp from(
  7. select row_num = row_number() over(partition by 字段,字段 order by 時(shí)間 desc)
  8.  from where 時(shí)間> getdate()-1
  9.  ) tmp
  10.  where row_num > 1

方法二按照單一條件進(jìn)行去重:

  1. delete from where 主鍵ID not in
  2. select max(主鍵ID) from group by 需要去重的字段 having count(需要去重的字段)>=1 
  3.  )
  4. delete from where 主鍵ID not in(
  5. select max(主鍵ID) from group by 需要去重的字段 having count(需要去重的字段)>=1
  6.  )

注意:為提高效率如上兩個(gè)方法都可以使用臨時(shí)表, not in 中的表可以先提取臨時(shí)表#tmp,

然后采用not exists來(lái)執(zhí)行,為避免數(shù)量過(guò)大,可批量用Top控制刪除量

  1. delete top(2) from 表 
  2.    where not exists (select 主鍵ID 
  3.  from #tmp where #tmp.主鍵ID=表.主鍵ID)

總結(jié)

以上所述是小編給大家介紹的使用SQL語(yǔ)句去掉重復(fù)的記錄,希望對(duì)大家有所幫助,

您可能感興趣的文章

相關(guān)文章