建立一個用戶和設備關係映射表,用戶的設備ID存放在數組字段裏面:sql
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
比較原查詢方式和新查詢方式的性能:性能
原查詢方式:code
新查詢方式:blog
能夠發現新查詢方式的性能有了巨大的提高!it