最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除

 更新時(shí)間:2023年05月05日 16:49:18   作者:大妮喲  
這篇文章主要介紹了postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

postgresql找到表中重復(fù)數(shù)據(jù)的行并刪除

創(chuàng)建測(cè)試表并插入數(shù)據(jù)

create table aaa(id bigserial,col1 varchar(255));
insert into aaa values(1,'b'),(2,'a'),(3,'b'),(4,'c');
select * from aaa;

找到重復(fù)行并刪除

方法1:ctid表示數(shù)據(jù)行在它所處的表內(nèi)的物理位置,ctid由兩個(gè)數(shù)字組成,第一個(gè)數(shù)字表示物理塊號(hào),第二個(gè)數(shù)字表示在物理塊中的行號(hào)。

select * from aaa where ctid not in(select max(ctid) from aaa group by col1);

刪除重復(fù)行

delete from aaa where ctid not in(select max(ctid) from aaa group by col1);

方法2:利用exists

找到重復(fù)行

select * from aaa t1 where  exists (select 1 from aaa t2 where t1.col1=t2.col1 and t1.id<t2.id )----exists后的意思是同一列相等,但是自增id不相等且id小的那一個(gè)

刪除重復(fù)行

delete from aaa t1 where  exists (select 1 from aaa t2 where t1.col1=t2.col1 and t1.id<t2.id )

postgresql常用的刪除重復(fù)數(shù)據(jù)方法

最高效方法

測(cè)試環(huán)境驗(yàn)證,6600萬(wàn)行大表,刪除2200萬(wàn)重復(fù)數(shù)據(jù)僅需3分鐘

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));

PG中三種刪除重復(fù)數(shù)據(jù)方法

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

create table deltest(id int, name varchar(255));
create table deltest_bk (like deltest);
insert into deltest select generate_series(1, 10000), 'ZhangSan';
insert into deltest select generate_series(1, 10000), 'ZhangSan';
insert into deltest_bk select * from deltest;

1. 常規(guī)刪除方法

最容易想到的方法就是判斷數(shù)據(jù)是否重復(fù),對(duì)于重復(fù)的數(shù)據(jù)只保留ctid最?。ɑ蜃畲螅┑臄?shù)據(jù),刪除其他的。

explain analyse delete from deltest a where a.ctid <> (select min(t.ctid) from deltest t where a.id=t.id);
-------------------------------------------------------------------------------------------
? ? 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
select count(*) from deltest;
count
-------
10000

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

2. group by刪除方法

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

explain analyse delete from deltest a where a.ctid not in (select min(ctid) from deltest group by id);
-------------------------------------------------------------------------------------------
? ? 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
select count(*) from deltest;
count
-------
10000

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

3. 高效刪除方法

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));
-----------------------------------------------------------------------------------------
? ? 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)?>SubqueryScanont(cost=204.95..250.73rows=509width=6)(actualtime=29.446..47.867rows=10000loops=1)Filter:(t.rownumber>1)RowsRemovedbyFilter:10000?>WindowAgg(cost=204.95..231.66rows=1526width=10)(actualtime=29.436..44.790rows=20000loops=1)?>Sort(cost=204.95..208.77rows=1526width=10)(actualtime=12.466..13.754rows=20000loops=1)SortKey:deltest.idSortMethod:quicksortMemory:1294kB?>SeqScanondeltest(cost=0.00..124.26rows=1526width=10)(actualtime=0.021..5.110rows=20000loops=1)?>TidScanondeltesta(cost=0.01..20.11rows=10width=6)(actualtime=82.983..88.751rows=10000loops=1)TIDCond:(ctid=ANY(0)?>SubqueryScanont(cost=204.95..250.73rows=509width=6)(actualtime=29.446..47.867rows=10000loops=1)Filter:(t.rownumber>1)RowsRemovedbyFilter:10000?>WindowAgg(cost=204.95..231.66rows=1526width=10)(actualtime=29.436..44.790rows=20000loops=1)?>Sort(cost=204.95..208.77rows=1526width=10)(actualtime=12.466..13.754rows=20000loops=1)SortKey:deltest.idSortMethod:quicksortMemory:1294kB?>SeqScanondeltest(cost=0.00..124.26rows=1526width=10)(actualtime=0.021..5.110rows=20000loops=1)?>TidScanondeltesta(cost=0.01..20.11rows=10width=6)(actualtime=82.983..88.751rows=10000loops=1)TIDCond:(ctid=ANY(0))
? ? Total runtime: 98.912 ms
select count(*) from deltest;
count
-------
10000

可以看到,居然只要98ms

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 修改postgresql存儲(chǔ)目錄的操作方式

    修改postgresql存儲(chǔ)目錄的操作方式

    這篇文章主要介紹了修改postgresql存儲(chǔ)目錄的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL+Pgpool實(shí)現(xiàn)HA主備切換的操作

    PostgreSQL+Pgpool實(shí)現(xiàn)HA主備切換的操作

    這篇文章主要介紹了PostgreSQL+Pgpool實(shí)現(xiàn)HA主備切換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • PostgreSQL15.x安裝的詳細(xì)教程

    PostgreSQL15.x安裝的詳細(xì)教程

    PostgreSQL 是一個(gè)功能強(qiáng)大的開(kāi)源關(guān)系型數(shù)據(jù)庫(kù)系統(tǒng),基于 C 語(yǔ)言實(shí)現(xiàn),采用 PostgreSQL 許可證,這是一種自由軟件許可證,允許用戶(hù)自由使用、修改和分發(fā)源代碼,所以本文將給大家介紹PostgreSQL15.x安裝的詳細(xì)教程,需要的朋友可以參考下
    2024-09-09
  • postgresql 性能參數(shù)配置方式

    postgresql 性能參數(shù)配置方式

    這篇文章主要介紹了postgresql 性能參數(shù)配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • postgresql 索引之 hash的使用詳解

    postgresql 索引之 hash的使用詳解

    這篇文章主要介紹了postgresql 索引之 hash的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • 用一整天的時(shí)間安裝postgreSQL  NTFS權(quán)限

    用一整天的時(shí)間安裝postgreSQL NTFS權(quán)限

    看標(biāo)題貌似一天的收獲不小,但實(shí)際上是被一個(gè)問(wèn)題搞的要死,啥問(wèn)題?額,又是NTFS權(quán)限的問(wèn)題。
    2009-08-08
  • 使用python-slim鏡像遇到無(wú)法使用PostgreSQL的問(wèn)題及解決方法

    使用python-slim鏡像遇到無(wú)法使用PostgreSQL的問(wèn)題及解決方法

    這篇文章主要介紹了使用python-slim鏡像遇到無(wú)法使用PostgreSQL的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • 解析PostgreSQL中Oid和Relfilenode的映射問(wèn)題

    解析PostgreSQL中Oid和Relfilenode的映射問(wèn)題

    這篇文章主要介紹了PostgreSQL中Oid和Relfilenode的映射問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 查看postgresql數(shù)據(jù)庫(kù)用戶(hù)系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法

    查看postgresql數(shù)據(jù)庫(kù)用戶(hù)系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法

    這篇文章主要介紹了查看postgresql數(shù)據(jù)庫(kù)用戶(hù)系統(tǒng)權(quán)限、對(duì)象權(quán)限的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • PostgreSQL的整型、浮點(diǎn)型、固定精度數(shù)值和序列等數(shù)字類(lèi)型

    PostgreSQL的整型、浮點(diǎn)型、固定精度數(shù)值和序列等數(shù)字類(lèi)型

    PostgreSQL(簡(jiǎn)稱(chēng)PGSQL)是一種開(kāi)源關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),廣泛應(yīng)用于企業(yè)級(jí)應(yīng)用,文章詳細(xì)介紹了PostgreSQL的數(shù)字類(lèi)型,包括整型、浮點(diǎn)型、固定精度數(shù)值型和序列類(lèi)型,強(qiáng)調(diào)了選擇合適的數(shù)字類(lèi)型對(duì)于數(shù)據(jù)庫(kù)的存儲(chǔ)效率、查詢(xún)性能和數(shù)據(jù)準(zhǔn)確性的重要性
    2024-09-09

最新評(píng)論

顺平县| 张掖市| 怀仁县| 宁乡县| 祥云县| 清水河县| 浠水县| 丽江市| 含山县| 平阴县| 根河市| 新密市| 沂南县| 木兰县| 墨江| 阿克陶县| 博野县| 隆安县| 万宁市| 青龙| 昂仁县| 青岛市| 弥勒县| 林甸县| 开江县| 娱乐| 永顺县| 若尔盖县| 西吉县| 繁昌县| 治县。| 绩溪县| 江孜县| 新密市| 赤峰市| 曲阜市| 宁化县| 皮山县| 桓仁| 天水市| 当阳市|