日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PostgreSQL 使用citd删除重复行

發(fā)布時間:2024/5/14 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PostgreSQL 使用citd删除重复行 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. ctid的簡單介紹:
  ctid是PostgreSQL表中的系統(tǒng)字段,表示數(shù)據(jù)行在它所在表內(nèi)的物理位置。ctid的字段類型是oid。但是VACUUM FULL操作之后,經(jīng)過回收數(shù)據(jù)塊內(nèi)的空閑空間,數(shù)據(jù)行在塊內(nèi)的物理位置會發(fā)生移動,即ctid會發(fā)生變化。

2. 使用ctid刪除重復(fù)行

數(shù)據(jù)

postgres=# select ctid,* from tb20;ctid | id --------+----(0,1) | 1(0,2) | 1(0,3) | 1(0,4) | 1(0,5) | 2(0,6) | 2(0,7) | 2(0,8) | 3(0,9) | 3(0,10) | 4 (10 rows
  • 簡單方式
postgres=# delete from tb20 a where ctid<>(select min(ctid) from tb20 b where a.id=b.id); DELETE 6 postgres=# select ctid,* from tb20;ctid | id --------+----(0,1) | 1(0,5) | 2(0,8) | 3(0,10) | 4 (4 rows)
  • 稍復(fù)雜的方式
postgres=# select ctid,row_number() over(partition by id) from tb20 ;ctid | row_number --------+------------(0,1) | 1(0,2) | 2(0,3) | 3(0,4) | 4(0,5) | 1(0,6) | 2(0,7) | 3(0,8) | 1(0,9) | 2(0,10) | 1 (10 rows)postgres=# select * from(select ctid,row_number() over(partition by id) from tb20)sn where sn.row_number>1 ;ctid | row_number -------+------------(0,2) | 2(0,3) | 3(0,4) | 4(0,6) | 2(0,7) | 3(0,9) | 2 (6 rows)postgres=# delete from tb20 where ctid =any(array((select ctid from(select ctid,row_number() over(partition by id) from tb20)sn where sn.row_number>1))) ; DELETE 6 postgres=# select ctid,row_number() over(partition by id) from tb20 ;ctid | row_number --------+------------(0,1) | 1(0,5) | 1(0,8) | 1(0,10) | 1 (4 rows)

  第一種方法在表tb20記錄比較多的情況下,效率比較差,第二種方法更高效。

總結(jié)

以上是生活随笔為你收集整理的PostgreSQL 使用citd删除重复行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。