1.针对PostgreSQL数据库表的去重复方法基本有三种,这是在网上查找的方法,在附录1给出。但是这些方法对GreenPlum来说都不管用。

2.数据表分布在不同的节点上,每个节点的ctid是唯一的,但是不同的节点就有ctid重复的可能,因此GreenPlum必须借助gp_segment_id来进行去重复处理。

3.在网上找到了一个相对繁琐的方法,在附录2给出:

4.最终的方法是:

delete from test where (gp_segment_id, ctid) not in (select gp_segment_id, min(ctid) from test group by x, gp_segment_id);

验证通过。

附录1:PostgreSQL数据表去重复的三种方法:

引用自:http://my.oschina.net/swuly302/blog/144933

采用PostgreSQL 9.2 官方文档例子为例:

CREATE TABLE weather (
city varchar(80),
temp_lo int, -- low temperature
temp_hi int, -- high temperature
prcp real, -- precipitation
date date
); INSERT INTO weather VALUES
('San Francisco', 46, 50, 0.25, '1994-11-27'),
('San Francisco', 43, 57, 0, '1994-11-29'),
('Hayward', 37, 54, NULL, '1994-11-29'),
('Hayward', 37, 54, NULL, '1994-11-29'); --- duplicated row

这里有3中方法:

第一种:替换法 

-- 剔除重复行的数据转存到新表weather_temp
SELECT DISTINCT city, temp_lo, temp_hi, prcp, date
INTO weather_temp
FROM weather;
-- 删除原表
DROP TABLE weather;
-- 将新表重命名为weather
ALTER TABLE weather_temp RENAME TO weather;
或者 -- 创建与weather一样的表weather_temp
CREATE TABLE weather_temp (LIKE weather INCLUDING CONSTRAINTS);
-- 用剔除重复行的数据填充到weather_temp中
INSERT INTO weather_temp SELECT DISTINCT * FROM weather;
-- 删除原表
DROP TABLE weather;
-- 将新重命名为weather.
ALTER TABLE weather_temp RENAME TO weather;
通俗易懂,有很多毁灭性的操作如DROP,而且当数据量大时,耗时耗空间。不推荐。 第二种: 添加字段法
-- 添加一个新字段,类型为serial
ALTER TABLE weather ADD COLUMN id SERIAL;
-- 删除重复行
DELETE FROM weather WHERE id
NOT IN (
SELECT max(id)
FROM weather
GROUP BY city, temp_lo, temp_hi, prcp, date
);
-- 删除添加的字段
ALTER TABLE weather DROP COLUMN id;
需要添加字段,「暂时不知道Postgres是如何处理添加字段的,是直接在原表追加呢,还是复制原表组成新表呢?」,如果是原表追加,可能就会因为新字段的加入而导致分页(一般block: 8k),如果是复制的话那就罪过了。不好。 第三种:系统字段[查看 System Columns] DELETE FROM weather
WHERE ctid
NOT IN (
SELECT max(ctid)
FROM weather
GROUP BY city, temp_lo, temp_hi, prcp, date
);
针对性强[Postgres独有],但是简单。

----------------但是对GreenPlum的表来说,表分割在各个节点上,不能单纯的用ctid来做去重复处理。

附录2:

https://discuss.pivotal.io/hc/zh-cn/community/posts/206428018-What-is-the-most-efficient-way-of-deleting-duplicate-records-from-a-table-

What is the most efficient way of deleting duplicate records from a table?

Currently we use Primary Keys to avoid loading duplicate data into our tables, but PK brings many restrictions. Since we can’t easily identify or prevent duplicates arriving from the variety of 3rd party upstream systems, we wanted to investigate the ‘load everything, remove duplicates afterwards’ approach.

In Postgres, you can use an efficient method such as:

DELETE FROM test
WHERE ctid NOT IN (
SELECT min(ctid)
FROM test
GROUP BY x);
(where 'x' is the unique column list)

However in Greenplum ‘ctid’ is only unique per segment.

One approach would be:

DELETE FROM test USING
(select gp_segment_id, ctid from
(select gp_segment_id, ctid, rank() over (partition by x order by gp_segment_id, ctid) as rk from test ) foo
WHERE rk <> 1) rows_to_delete
WHERE test.gp_segment_id=rows_to_delete.gp_segment_id
AND test.ctid=rows_to_delete.ctid;

But the use of window functions, subqueries etc. feels pretty inefficient.

Is there a better form?

Note that in our use case our unique column list varies up to ~10 columns so we don’t have a single unique key field – hence the RANK in the example. I suppose adding a sequence column could be used, but how much overhead does this add when doing bulk data loading?

GreenPlum高效去除表重复数据的更多相关文章

  1. java 去除数组重复数据,并输出重复数据值

    /** * 去除重复数据 * @author Sunqinbo */ public class RemoveDuplicateData { public static void main(String ...

  2. c# 利用IEqualityComparer接口去除DataTable重复数据

    IEqualityComparer主要适用于定义方法以支持对象的相等比较.可以实现集合的自定义相等比较.即,您可以创建自己的相等定义,并指定此定义与接受 IEqualityComparer 接口的集合 ...

  3. 数据库删除数据表重复数据,只留下ID较小的行

    删除表中重复数据,留下ID比较小的行 delete from 表 where [重复字段] in (select [重复字段] from 表 group by 字段 having count([字段] ...

  4. 去除DataTable重复数据的三种方法

    业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需要对其进行一些处理(过滤一些为空,长度太短或太长,非法字符,重复数据)然后在进行入库. 其中要避 ...

  5. 去除DataTable重复数据的三种方法(转)

    转自:https://www.cnblogs.com/sunxi/p/4767577.html 业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需 ...

  6. Mysql如何将一张表重复数据删除

    MySQL无法select 和 delete,update同时进行 只有将group By 出来不重复的数据进行insert到一张和之前同样类型的新表里面 转换思路,解决问题!​​

  7. javascsript 去除数组重复数据

    function uniqid(arr){ var newArr = []; var c; for(var i = 0 ;i <= arr.length ;i++){ c = false; fo ...

  8. 【Oracle】去除表中重复的数据

    删除表重复数据 (t1表中有重复数据)1.使用distinct create table t2 as select * from t1;create table tmp_t2 as select di ...

  9. Java实现数组去除重复数据的方法详解

    一.用List集合实现 int[] str = {5, 6, 6, 6, 8, 8, 7,4}; List<Integer> list = new ArrayList<Integer ...

随机推荐

  1. Top Coder算法题目浏览器

    作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/top-code-offline-browser/ 关于 左耳朵耗子 ...

  2. ASP.net 内置对象

    .net初学者,有错误欢迎指正.大家共同进步 Response 输出数据 Reponse对象和Request对象组成了一对发送,接受数据的对象. 发送信息:Reponse.Write("字符 ...

  3. Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉

    Atitit 图像处理和计算机视觉的分类 三部分 图像处理 图像分析 计算机视觉 1.1. 按照当前流行的分类方法,可以分为以下三部分:三部分 图像处理 图像分析 计算机视觉1 1.2. 图像处理需要 ...

  4. Node.js:fs文件系统模块

    fs文件系统模块,这是一个非常重要的模块,对文件的操作都基于它.该模块的所有方法都有同步和异步两种方式,下面便介绍一下该模块的使用. 1.检测当前进程对文件的权限 使用fs.access(path[, ...

  5. RHEL 本地yum源配置

    1.创建挂载目录 # mkdir -p /media/cdrom   2.挂载对应系统版本的iso光盘镜像文件 # mount -o loop -t iso9660 /opt/rhel-server- ...

  6. ORA 各种oraclesql错误

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  7. ASP.NET Core 中文文档 第二章 指南(4.9)添加验证

    原文:Adding Validation 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘).娄宇(Lyrics).许登洋(Seay) 在本章节中你将为 Movie 模型 ...

  8. Http协议相关内容

    http协议概述 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义浏览器与WEB服务器之间交换数据的过程. 客户 ...

  9. 前端开发:css基础知识之盒模型以及浮动布局。

    前端开发:css基础知识之盒模型以及浮动布局 前言 楼主的蛮多朋友最近都在学习html5,他们都会问到同一个问题 浮动是什么东西?  为什么这个浮动没有效果?  这个问题楼主已经回答了n遍.今天则是把 ...

  10. 超越 JSON: Spearal 序列化协议简介

      Spearal 是一个新的开源的序列化协议,这个协议旨在初步替换JSON 将HTML和移动应用连接到Java的后端. Spearal的主要目的是提供一个序列协议,这个协议即使是在端点间传输的复杂的 ...