Using Arrays in PostgreSQL to Improve Performance

Create a user and device relationship mapping table, the user's device ID is stored in the array field:

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)
)

Import data into the table:

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

Compare the performance of the original query method and the new query method:

Original query method:

original query method

New query method:

New query method

It can be found that the performance of the new query method has been greatly improved!

{{o.name}}
{{m.name}}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324085788&siteId=291194637