最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何在postgresql中刪除重復(fù)數(shù)據(jù)-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)如何在postgresql中刪除重復(fù)數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

天涯網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)公司成立于2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司

首先創(chuàng)建一張基礎(chǔ)表,并插入一定量的重復(fù)數(shù)據(jù)。


  test=# create table deltest(id int, name varchar(255));
  CREATE TABLE
  test=# create table deltest_bk (like deltest);
  CREATE TABLE
  test=# insert into deltest select generate_series(1, 10000), 'ZhangSan';
  INSERT 0 10000
  test=# insert into deltest select generate_series(1, 10000), 'ZhangSan';
  INSERT 0 10000
  test=# insert into deltest_bk select * from deltest;

常規(guī)刪除方法

最容易想到的方法就是判斷數(shù)據(jù)是否重復(fù),對(duì)于重復(fù)的數(shù)據(jù)只保留ctid最?。ɑ蜉^大)的那條數(shù)據(jù),刪除其他的數(shù)據(jù)。

test=# explain analyse delete from deltest a where a.ctid <> (select min(t.ctid) from deltest t where a.id=t.id);
                               QUERY PLAN
  -----------------------------------------------------------------------------------------------------------------------------
  Delete on deltest a (cost=0.00..195616.30 rows=1518 width=6) (actual time=67758.866..67758.866 rows=0 loops=1)
    -> Seq Scan on deltest a (cost=0.00..195616.30 rows=1518 width=6) (actual time=32896.517..67663.228 rows=10000 loops=1)
     Filter: (ctid <> (SubPlan 1))
     Rows Removed by Filter: 10000
     SubPlan 1
      -> Aggregate (cost=128.10..128.10 rows=1 width=6) (actual time=3.374..3.374 rows=1 loops=20000)
         -> Seq Scan on deltest t (cost=0.00..128.07 rows=8 width=6) (actual time=0.831..3.344 rows=2 loops=20000)
            Filter: (a.id = id)
            Rows Removed by Filter: 19998
  Total runtime: 67758.931 ms
  test=# select count(*) from deltest;
  count
  -------
  10000
  (1 行記錄)

可以看到,id相同的數(shù)據(jù),保留ctid最小的那條,其他的刪除。相當(dāng)于把deltest表中的數(shù)據(jù)刪掉一半,耗時(shí)達(dá)到67s多。相當(dāng)慢。

group by刪除方法

第二種方法為group by方法,通過分組找到ctid最小的數(shù)據(jù),然后刪除其他數(shù)據(jù)。

  test=# truncate table deltest;
  TRUNCATE TABLE
  test=# insert into deltest select * from deltest_bk;
  INSERT 0 20000
  test=# explain analyse delete from deltest a where a.ctid not in (select min(ctid) from deltest group by id);
                               QUERY PLAN
  ----------------------------------------------------------------------------------------------------------------------------------
  Delete on deltest a (cost=131.89..2930.46 rows=763 width=6) (actual time=30942.496..30942.496 rows=0 loops=1)
    -> Seq Scan on deltest a (cost=131.89..2930.46 rows=763 width=6) (actual time=10186.296..30814.366 rows=10000 loops=1)
     Filter: (NOT (SubPlan 1))
     Rows Removed by Filter: 10000
     SubPlan 1
      -> Materialize (cost=131.89..134.89 rows=200 width=10) (actual time=0.001..0.471 rows=7500 loops=20000)
         -> HashAggregate (cost=131.89..133.89 rows=200 width=10) (actual time=10.568..13.584 rows=10000 loops=1)
            -> Seq Scan on deltest (cost=0.00..124.26 rows=1526 width=10) (actual time=0.006..3.829 rows=20000 loops=1)
   Total runtime: 30942.819 ms
  (9 行記錄)
  test=# select count(*) from deltest;
   count
  -------
  10000
  (1 行記錄)

可以看到同樣是刪除一半的數(shù)據(jù),使用group by的方式,時(shí)間節(jié)省了一半。但仍含需要30s,下面試一下第三種刪除操作。

新的刪除方法

在postgres修煉之道這本書中,作者提到一種效率較高的刪除方法, 在這里驗(yàn)證一下,具體如下:

  test=# truncate table deltest;
  TRUNCATE TABLE
  test=# insert into deltest select * from deltest_bk;
  INSERT 0 20000                             
  test=# explain analyze delete from deltest a where a.ctid = any(array (select ctid from (select row_number() over (partition by id), ctid from deltest) t where t.row_number > 1));
                               QUERY PLAN
  ----------------------------------------------------------------------------------------------------------------------------------
  Delete on deltest a (cost=250.74..270.84 rows=10 width=6) (actual time=98.363..98.363 rows=0 loops=1)
  InitPlan 1 (returns $0)
   -> Subquery Scan on t (cost=204.95..250.73 rows=509 width=6) (actual time=29.446..47.867 rows=10000 loops=1)
      Filter: (t.row_number > 1)
      Rows Removed by Filter: 10000
      -> WindowAgg (cost=204.95..231.66 rows=1526 width=10) (actual time=29.436..44.790 rows=20000 loops=1)
         -> Sort (cost=204.95..208.77 rows=1526 width=10) (actual time=12.466..13.754 rows=20000 loops=1)
            Sort Key: deltest.id
            Sort Method: quicksort Memory: 1294kB
            -> Seq Scan on deltest (cost=0.00..124.26 rows=1526 width=10) (actual time=0.021..5.110 rows=20000 loops=1)
  -> Tid Scan on deltest a (cost=0.01..20.11 rows=10 width=6) (actual time=82.983..88.751 rows=10000 loops=1)
     TID Cond: (ctid = ANY ($0))
  Total runtime: 98.912 ms
  (13 行記錄)
  test=# select count(*) from deltest;
  count
  -------
  10000
  (1 行記錄)

看到上述結(jié)果,真讓我吃驚了一把,這么快的刪除方法還是首次看到,自己真實(shí)孤陋寡聞,在這里要膜拜一下修煉之道這本書的大神作者了。

補(bǔ)充:pgsql 刪除表中重復(fù)數(shù)據(jù)保留其中的一條

1.在表中(表名:table 主鍵:id)增加一個(gè)字段rownum,類型為serial

2.執(zhí)行語句:

delete from table where rownum not in( 
select max(rownum) from table group by id 
)

3.最后刪除rownum

關(guān)于如何在postgresql中刪除重復(fù)數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。


分享文章:如何在postgresql中刪除重復(fù)數(shù)據(jù)-創(chuàng)新互聯(lián)
文章分享:http://fisionsoft.com.cn/article/decgdj.html