postgresql菜鸟教程|PostgreSQL中使用数组改进性能的实例

时间:2024-06-08  来源:PostgreSQL  阅读:

postgresql支持数组类型,可以是基本类型,也可以是用户自定义的类型。日常中使用数组类型的机会不多,但还是可以了解一下。不像C或JAVA高级语言的数组下标从0开始,postgresql数组下标从1开始,既可以指定长度,也可以不指定长度。且postgresql既支持一维数组,也支持多维数组,但是平时二维数组也就够用了。

本文将给大家介绍PostgreSQL通过数组改进性能的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧

PostgreSQL通过数组改进性能

创建一个用户和设备关系映射表,用户的设备ID存放在数组字段里面:

CREATE TABLE device.user_devices
(
user_id character varying(32) COLLATE pg_catalog."default" NOT NULL,
device_ids character varying[] COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT user_devices_pkey PRIMARY KEY (user_id)
)

将数据导入表:

insert into device.user_devices
 select device_owner, array_agg(device_id)
 from device.device_info
 where device_owner is not null
 and device_owner != ""
 group by device_owner

比较原查询方式和新查询方式的性能:

原查询方式:

word-spacing:0px;border-bottom:0px;text-transform:none;font-weight:400;color:#333333;padding-bottom:0px;font-style:normal;text-align:left;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;orphans:2;widows:2;letter-spacing:normal;padding-right:0px;background-color:#FFFFFF;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;text-decoration-style:initial;text-decoration-color:initial;" src="http://img.111cn.net/attachment/art/169017/91162cbf26.png?20181116115740" />

新查询方式:

word-spacing:0px;text-transform:none;font-weight:400;color:#333333;padding:0px;font-style:normal;text-align:left;margin:0px;letter-spacing:normal;background-color:#FFFFFF;text-indent:0px;font-variant-ligatures:normal;font-variant-caps:normal;-webkit-text-stroke-width:0px;text-decoration-style:initial;text-decoration-color:initial;width:650px;height:auto;" src="http://img.111cn.net/attachment/art/169017/0ec9a68dfa.png?20181116115756" />

可以发现新查询方式的性能有了巨大的提升!

postgresql菜鸟教程|PostgreSQL中使用数组改进性能的实例

http://m.bbyears.com/shujuku/159142.html

推荐访问:postgresql和mysql区别
相关阅读 猜你喜欢
本类排行 本类最新