在項(xiàng)目中我們常會(huì)對(duì)數(shù)據(jù)進(jìn)行去重處理,有時(shí)候會(huì)用in或者EXISTS函數(shù)。或者通過(guò)group by也是可以實(shí)現(xiàn)查重
不過(guò)Postgresql還有自帶去重函數(shù):distinct
下面是distinct 的實(shí)例:
1、創(chuàng)建表:user
CREATE TABLE `user` (
`name` varchar(30) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('張三', 20);
INSERT INTO `user` VALUES ('李四', 22);
INSERT INTO `user` VALUES ('李四', 20);
INSERT INTO `user` VALUES ('張三', 22);
INSERT INTO `user` VALUES ('張三', 20);
查詢結(jié)果:
SELECT * FROM user
張三 20
李四 22
李四 20
張三 22
張三 20
2、根據(jù) name 查詢?nèi)ブ睾蟮臄?shù)據(jù):
SELECT distinct name FROM user
張三
李四
3、根據(jù)name 和 age 查詢?nèi)ブ睾蟮臄?shù)據(jù):
SELECT distinct name,age FROM user
張三 20
李四 22
李四 20
張三 22
4、根據(jù)name,age查詢重復(fù)數(shù)據(jù)數(shù):
SELECT distinct name,age,count(*) 數(shù)據(jù)條數(shù) FROM user GROUP BY name,age
張三 20 2
張三 22 1
李四 20 1
李四 22 1
二、查出重復(fù)數(shù)據(jù)后,我們需要?jiǎng)h除重復(fù)數(shù)據(jù)
刪除重復(fù)數(shù)據(jù)一般幾種方式,一般采用 臨時(shí)表 或者根據(jù) 某個(gè)字段,例如id等,通過(guò)max或者min函數(shù)去重。
補(bǔ)充:基于postgresql ctid實(shí)現(xiàn)數(shù)據(jù)的差異同步
項(xiàng)目背景:
最近在做異構(gòu)數(shù)據(jù)同步方面(非實(shí)時(shí))的工作,從oracle,gbase,postgresql向mysql數(shù)據(jù)庫(kù)中同步,對(duì)于沒(méi)有自增字段(自增ID或時(shí)間字段)的業(yè)務(wù)表,做差異同步是一件非常麻煩的事情,主要體現(xiàn)在記錄的新增、更新與刪除上
備注:源庫(kù)只提供一個(gè)只讀權(quán)限的用戶
ctid在pg中的作用
ctid是用來(lái)指向自身或新元組的元組標(biāo)識(shí)符,怎么理解呢?下面能過(guò)幾個(gè)實(shí)驗(yàn)來(lái)測(cè)試一下
satdb=# create table test_ctid(id int,name varchar(100));
satdb=# insert into test_ctid values(1,‘a(chǎn)'),(1,‘a(chǎn)');
satdb=# insert into test_ctid values(2,‘a(chǎn)'),(3,‘a(chǎn)');
查看記錄的ctid值
satdb=# select id,name,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
2 | a | (0,3)
3 | a | (0,4)
(4 rows)
對(duì)id為2的記錄進(jìn)行更新
satdb=# update test_ctid set name=‘b' where id=2;
UPDATE 1
這里可以看到id=2的記錄指向了新的元組標(biāo)識(shí)符 (0,5)
satdb=# select id,name,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
3 | a | (0,4)
2 | b | (0,5)
(4 rows)
satdb=# select * from test_ctid where ctid='(0,1)';
id | name
----±-----
1 | a
(1 row)
刪除 id=3的記錄后,對(duì)應(yīng)的ctid(0,4)不存在了
satdb=# delete from test_ctid where id=3;
DELETE 1
satdb=# select *,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
2 | b | (0,5)
(3 rows)
再插入一條記錄時(shí),看看會(huì)不會(huì)使用(0,4)這個(gè)標(biāo)識(shí)符
satdb=# insert into test_ctid values(3,‘d');
INSERT 0 1
satdb=# select *,ctid from test_ctid;
id | name | ctid
----±-----±------
1 | a | (0,1)
1 | a | (0,2)
2 | b | (0,5)
3 | d | (0,6)
這里新插入的記錄不會(huì)使用(0,4),而是直接分配新的標(biāo)識(shí)符(0,6)
總結(jié):
1、ctid的作用與oracle rowid類(lèi)似,可以唯一標(biāo)識(shí)一條記錄
2、記錄的更新后,后生產(chǎn)新的ctid
3、記錄刪除后,新插入的記錄不會(huì)使用已經(jīng)刪除記錄的ctid
4、基于ctid可以實(shí)現(xiàn)記錄的去重操作
5、基于ctid可以實(shí)現(xiàn)差異增量同步(新增、刪除、更新)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- postgresql rank() over, dense_rank(), row_number()用法區(qū)別
- PostgreSQL 實(shí)現(xiàn)distinct關(guān)鍵字給單獨(dú)的幾列去重
- postgreSQL中的row_number() 與distinct用法說(shuō)明