Question: I am using PostGIS to calculate length of interactions between two types of employees. I am performing the same set of operations for each area

I am using PostGIS to calculate length of interactions between two types of employees. I am performing the same set of operations for each area id. Here is my code:
CREATE OR REPLACE FUNCTION public.interactions_looped_ucmo_aic(areaid_array character varying[], d_date date, start_time__ timestamp without time zone, end_time__ timestamp without time zone)
RETURNS TABLE(area_id uuid, "createdAt_" timestamp without time zone, "updatedAt_" timestamp without time zone, "ID_" character varying, parent_imei character varying, child_imei character varying, parent_ping_time timestamp without time zone, child_ping_time timestamp without time zone, maxima timestamp without time zone, minima timestamp without time zone, "interactionID" character varying, interaction_type character varying)
LANGUAGE plpgsql
AS $function$
DECLARE
i integer :=1;
BEGIN
FOR i IN 1..array_length(areaid_array, 1) LOOP
RETURN QUERY
with pings as (
select
*
from
(--================= SELECTING Parent INFO
select
p.imei,
st_transform(p."geoJson", 32643) as geom,
"generatedAt" as t,
p."ID" as u_ping_id
from
campaign_pings p
where
"generatedAt" + interval '5 hours' between start_time__ and end_time__
and
p.imei in (
--multiple left joins
)
) u
left join (--================= SELECTING child INFO
SELECT
p.imei,
p."ID" as a_ping_id,
st_transform(p."geoJson", 32643) as geom,
"generatedAt" as t
FROM
campaign_pings p
where
"generatedAt"+ interval '5 hours' between start_time__ and end_time__
and
p.imei in (
--multiple left joins
)
) a on ST_DWithin(u."geom", a."geom", 30)
and a.t BETWEEN u.t - INTERVAL '30 seconds'
and u.t + INTERVAL '30 seconds'
where
a.imei is not null
)
--more code to calculate length of continuous interactions in minutes
;
END LOOP;
END
$function$
;
So essentially, I am performing the same set of operations for each area id. Is there a way to make use of caching here to reduce execution time? I mean to enforce the use of cache.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!