PostgreSQL数组类型应用
在使用 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数组类型应用的更多相关文章
- PostgreSQL 数组类型
PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...
- postgresql —— 数组类型
创建数组 CREATE TABLE sal_emp ( name text, pay_by_quarter integer[] --还可以定义为integer[4]或integer ARRAY[4] ...
- PostgreSQL Array 数组类型与 FreeSql 打出一套【组合拳】
前言 PostgreSQL 是世界公认的功能最强大的开源数据库,除了基础数据类型 int4/int8/varchar/numeric/timestamp 等数据类型,还支持 int4[]/int8[] ...
- mybatis 处理数组类型及使用Json格式保存数据 JsonTypeHandler and ArrayTypeHandler
mybatis 比 ibatis 改进了很多,特别是支持了注解,支持了plugin inteceptor,也给开发者带来了更多的灵活性,相比其他ORM,我还是挺喜欢mybatis的. 闲言碎语不要讲, ...
- java中用spring实现数组类型输出
java 中的几个数组类型 1.Department类 package com.yy.collection; import java.util.List; import java.util.Map; ...
- JS数组类型检测
在强类型语言,数组类型检测是非常容易的事情(typeof就可以解决),而在弱语言JS数据类型就很容易混淆了. JS中常见的数据类型有:number.string.boolean.undefined.f ...
- C语言 数组类型与数组指针类型
//数组类型与数组指针类型 #include<stdio.h> #include<stdlib.h> #include<string.h> void main(){ ...
- delphi 数组类型与数组指针的巧妙利用
{本例通过存取结构, 慢慢引入了数组类型与指针的一些使用方法; 其中六个小例子的测试内容和结果都是一样的. ---------------------------------------------- ...
- delphi 数组类型
数组类型 数组类型定义了一组指定类型的元素序列,在方括号中填入下标值就可访问数组中的元素.定义数组时,方括号也用来指定可能的下标值.例如,下面的代码中定义了一个有 24 个整数的数组:type ...
随机推荐
- 按钮改变和控制div的形状的html,JavaScript代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 第7篇scrum冲刺(5.27)
一.站立会议 1.照片 2.工作安排 成员 昨天已完成的工作 今天的工作安排 困难 陈芝敏 学习云开发,云函数调用以及数据的前后端传递 今天实现云词库搭建,随机获取并显示,对云开发有更深的认识 ...
- 第五篇 Scrum冲刺博客
一.会议图片 二.项目进展 成员 完成情况 今日任务 冯荣新 未完成 购物车列表,购物车工具栏 陈泽佳 未完成 静态结构 徐伟浩 商品信息录入 协助前端获取数据 谢佳余 未完成 搜索算法设计 邓帆涛 ...
- Shell编程—gawk进阶
1使用变量 awk编程语言支持两种不同类型的变量: 内建变量 自定义变量 1.1内建变量 1. 字段和记录分隔符变量 数据字段变量允许你使用美元符号($)和字段在该记录中的位置值来引用记录对应的字段. ...
- 力扣Leetcode 202. 快乐数 -快慢指针 快乐就完事了
快乐数 编写一个算法来判断一个数 n 是不是快乐数. 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不 ...
- 力扣Leetcode 33. 搜索旋转排序数组
33. 搜索旋转排序数组 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值, ...
- oa项目面试准备
熟悉项目在ssm框架下的编程流程,了解mysql html spring springmvc mybatis技术.了解过springboot编程. 在上个寒假跟着培训机构用springboot框架编写 ...
- wxmini
微信小游戏架构概览 https://www.jianshu.com/p/02199c35d749 微信小程序:工具配置 project.config.json https://www.cnblogs. ...
- mysql 8.0.11安装教程
安装环境:win7 1. 下载安装包 下载地址:https://dev.mysql.com/downloads/file/?id=476233 2. 解压zip包 3. 初始化my.ini 创建my. ...
- python学习笔记回忆录02
1.for循环 依次按顺序从列表中取出值,直到遍历完整个列表为止 the_count =[1,2,3,4,5] for number in the_count: print "this is ...