建立一個使用者和裝置關系映射表,使用者的裝置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
比較原查詢方式和新查詢方式的性能:
原查詢方式:

新查詢方式:
可以發現新查詢方式的性能有了巨大的提升!