在使用 awk 脚本;数组是一大利器;在很多场景是用数组能处理。

在 python 中,数据类型list;相当于array类型。

在 Oracle 中,对 array 不够友好,感觉像是鸡肋。但是在 PostgreSQL 中,对array有很多支持,很多场景可以应用到。下面慢慢说

1、any(array) 替换 in(table)

-- 案例1
-- 创建表A;插入1000条记录;并每条记录重复4次
postgres=# create table A (id int, info text);
CREATE TABLE
postgres=# 
postgres=# insert into A select generate_series(1,1000), 'lottu';
INSERT 0 1000
postgres=# 
postgres=# insert into A select generate_series(1,1000), 'lottu';
INSERT 0 1000
postgres=# insert into A select * from A;
INSERT 0 2000
-- 用in的方式去处理重复数据
postgres=# begin;
BEGIN
postgres=# explain (analyze, costs, timing) delete from A where ctid not in (select min(ctid) from A group by id, info);
                                                        QUERY PLAN                                                         
---------------------------------------------------------------------------------------------------------------------------
 Delete on a  (cost=74.38..131.31 rows=1397 width=6) (actual time=12.619..12.619 rows=0 loops=1)
   ->  Seq Scan on a  (cost=74.38..131.31 rows=1397 width=6) (actual time=5.146..7.129 rows=3000 loops=1)
         Filter: (NOT (hashed SubPlan 1))
         Rows Removed by Filter: 1000
         SubPlan 1
           ->  HashAggregate  (cost=70.89..73.69 rows=279 width=42) (actual time=3.762..4.155 rows=1000 loops=1)
                 Group Key: a_1.id, a_1.info
                 ->  Seq Scan on a a_1  (cost=0.00..49.94 rows=2794 width=42) (actual time=0.017..1.158 rows=4000 loops=1)
 Planning Time: 1.923 ms
 Execution Time: 44.130 ms
(10 rows)
-- 用any(array)的方式处理
postgres=# explain (analyze, costs, timing) delete from A
postgres-#  where ctid = any(array (select ctid
postgres(#                      from (select "row_number"() over(partition by id, info) as rn,
postgres(#                                   ctid
postgres(#                              from A) as ad
postgres(#                     where ad.rn > 1));
                                                           QUERY PLAN                                                            
---------------------------------------------------------------------------------------------------------------------------------
 Delete on a  (cost=300.69..340.79 rows=10 width=6) (actual time=17.686..17.686 rows=0 loops=1)
   InitPlan 1 (returns $0)
     ->  Subquery Scan on ad  (cost=209.87..300.68 rows=931 width=6) (actual time=3.995..9.503 rows=3000 loops=1)
           Filter: (ad.rn > 1)
           Rows Removed by Filter: 1000
           ->  WindowAgg  (cost=209.87..265.75 rows=2794 width=50) (actual time=3.986..8.570 rows=4000 loops=1)
                 ->  Sort  (cost=209.87..216.86 rows=2794 width=42) (actual time=3.974..4.577 rows=4000 loops=1)
                       Sort Key: a_1.id, a_1.info
                       Sort Method: quicksort  Memory: 284kB
                       ->  Seq Scan on a a_1  (cost=0.00..49.94 rows=2794 width=42) (actual time=0.015..1.486 rows=4000 loops=1)
   ->  Tid Scan on a  (cost=0.01..40.11 rows=10 width=6) (actual time=11.130..12.945 rows=3000 loops=1)
         TID Cond: (ctid = ANY ($0))
 Planning Time: 0.619 ms
 Execution Time: 17.808 ms
(14 rows)
结论:
1、效率大大提升;数据量越大提升效果越好;any(array) 的效果 >= in
2、判断 array 所含元素的方法,有 any / some (any) 还有 all两种方法

2、array 相关函数

-- string 转换 array
-- 函数 string_to_array
select array_to_string(array[1, 2, 3], '~^~');
array_to_string
-----------------
1~^~2~^~3
-- 函数 string_to_array
select string_to_array('1~^~2~^~3','~^~');
string_to_array
-----------------
{1,2,3}
-- 函数 regexp_split_to_array;跟string_to_array有点类似
select regexp_split_to_array('1~^~2~^~3','\~\^\~');
regexp_split_to_array
-----------------------
{1,2,3}
-- 函数 unnest
select unnest(array['a', 'b', 'c']);
unnest
--------
a
b
c
-- 还可以结合with ordinality;添加行号
select * from unnest(array['a', 'b', 'c']) with ordinality;
unnest | ordinality
--------+------------
a | 1
b | 2
c | 3

PostgreSQL数组类型应用的更多相关文章

  1. PostgreSQL 数组类型

    PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...

  2. postgresql —— 数组类型

    创建数组 CREATE TABLE sal_emp ( name text, pay_by_quarter integer[] --还可以定义为integer[4]或integer ARRAY[4] ...

  3. PostgreSQL Array 数组类型与 FreeSql 打出一套【组合拳】

    前言 PostgreSQL 是世界公认的功能最强大的开源数据库,除了基础数据类型 int4/int8/varchar/numeric/timestamp 等数据类型,还支持 int4[]/int8[] ...

  4. mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler

    mybatis 比 ibatis 改进了很多,特别是支持了注解,支持了plugin inteceptor,也给开发者带来了更多的灵活性,相比其他ORM,我还是挺喜欢mybatis的. 闲言碎语不要讲, ...

  5. java中用spring实现数组类型输出

    java 中的几个数组类型 1.Department类 package com.yy.collection; import java.util.List; import java.util.Map; ...

  6. JS数组类型检测

    在强类型语言,数组类型检测是非常容易的事情(typeof就可以解决),而在弱语言JS数据类型就很容易混淆了. JS中常见的数据类型有:number.string.boolean.undefined.f ...

  7. C语言 数组类型与数组指针类型

    //数组类型与数组指针类型 #include<stdio.h> #include<stdlib.h> #include<string.h> void main(){ ...

  8. delphi 数组类型与数组指针的巧妙利用

    {本例通过存取结构, 慢慢引入了数组类型与指针的一些使用方法; 其中六个小例子的测试内容和结果都是一样的. ---------------------------------------------- ...

  9. delphi 数组类型

    数组类型 数组类型定义了一组指定类型的元素序列,在方括号中填入下标值就可访问数组中的元素.定义数组时,方括号也用来指定可能的下标值.例如,下面的代码中定义了一个有 24 个整数的数组:type     ...

随机推荐

  1. Java 类初始化和实例初始化过程

    1.类初始化过程 2.实例初始化过程 3.方法的重写

  2. Windows下搭载虚拟机以及环境安装

    前言 最近回到家中进行赛前自主提升 模拟赛考虑到考试环境是NOI Linux 而大多数同学电脑环境为Windows 有同学想要模拟真实考试环境 但是NOI Linux的系统过于"阉割版&qu ...

  3. 多元线性回归检验t检验(P值),F检验,R方等参数的含义

    做线性回归的时候,检验回归方程和各变量对因变量的解释参数很容易搞混乱,下面对这些参数进行一下说明: 1.t检验:t检验是对单个变量系数的显著性检验   一般看p值:    如果p值小于0.05表示该自 ...

  4. Dubbo直连方式改造

    目录 一.dubbo 服务化最佳实践 1. 分包 2. 粒度 3. 版本 二.改造 dubbo 项目 三.link-interface 1. pom.xml 2. 实体类 3. 公共接口 四.提供者 ...

  5. Shell编程—数据展示

    1.标准文件描述符 Linux用文件描述符(file descriptor)来标识每个文件对象.文件描述符是一个非负整数,可以唯一标识会话中打开的文件.每个进程一次 多可以有九个文件描述符.出于特殊目 ...

  6. Ajax、XMLHttpRequest、JSONP的区别

    来自2020年搜狗的笔试题,第一题就不会

  7. 如何发挥微博客在seo营销中的作用

    http://www.wocaoseo.com/thread-158-1-1.html 我们知道,现在微博客已经发展的相当成熟,普通一个人,只要会上网,就能开通属于自己的博客,进而可以时时地通过微博客 ...

  8. 基于.NetCore3.1系列 —— 日志记录之初识Serilog

    一.前言 对内置日志系统的整体实现进行了介绍之后,可以通过使用内置记录器来实现日志的输出路径.而在实际项目开发中,使用第三方日志框架(如: Log4Net.NLog.Loggr.Serilog.Sen ...

  9. 揭秘日活千万腾讯会议全量云原生化上TKE技术实践

    腾讯会议,一款联合国都Pick的线上会议解决方案,提供完美会议品质和灵活协作空间,广泛应用在政府.医疗.教育.企业等各个行业.大家从文章8天扩容100万核,腾讯会议是如何做到的?都知道腾讯会议背后的计 ...

  10. jdk1.8 新增工具类

    目录 optional 时间API Instant localDateTime LocalDate LocalTime Duration TemporalAdjuster DateTimeFormat ...