Dear expert,
Could you please review and suggest to optimize performance of the PLSQL
procedure in PostgreSQL?
I have attached the same.
Thanks in advance
Regards,
Dinesh Chandra
DISCLAIMER:
This email message is for the sole use of the intended recipient(s) and may
contain confidential and privileged information. Any unauthorized review, use,
disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies of
the original message. Check all attachments for viruses before opening them.
All views or opinions presented in this e-mail are those of the author and may
not reflect the opinion of Cyient or those of our affiliates.
-Function to get weather forcast based on threshold value of weather
parameters
CREATE OR REPLACE FUNCTION dome.geteffectiveforcast4(
coldmaxtemp double precision,
coldmintemp double precision,
hotmaxtemp double precision,
hotmintemp double precision,
windmax double precision,
windmin double precision,
rainmax double precision,
rainmin double precision,
humiditymax double precision,
humiditymin double precision,
snowmax double precision,
snowmin double precision)
RETURNS SETOF "TABLE(lockey text, from_time timestamp without time zone,
to_time timestamp without time zone, weathertype text, flood_height text,
severity text)"
LANGUAGE 'plpgsql'
COST 100.0
VOLATILE
ROWS 1000.0
AS $function$
BEGIN
RETURN QUERY select s."LocKey",min(s.dt) from_time, max(s.dt) to_time,'Red
Cold Temperature' as WeatherType,'' as "Flood_height",'' as "Severity" from (
select "LocKey","Date" + "Time" as dt,
row_number() over (partition by "LocKey" order by
"Date" + "Time") as rn,
("Date" + "Time" - (concat(row_number() over
(partition by "LocKey" order by "Date" + "Time"),' hours')::interval)) as grp
from dome."AccuWeatherForcastHourly" where "Temperature"<0
order by "LocKey", "Date" + "Time") s
group by s."LocKey",s.grp
UNION
select s."LocKey",min(s.dt) from_time, max(s.dt)
to_time,'Amber Cold Temperature' as WeatherType,'' as "Flood_height",'' as
"Severity" from (
select "LocKey","Date" + "Time" as dt,row_number() over
(partition by "LocKey" order by "Date" + "Time") as rn,
("Date" + "Time" - (concat(row_number() over
(partition by "LocKey" order by "Date" + "Time"),' hours')::interval)) as grp
from dome."AccuWeatherForcastHourly"
where "Temperature">=coldmintemp and "Temperature"<= coldmaxtemp
order by "LocKey", "Date" + "Time") s
group by s."LocKey",s.grp
UNION
select s."LocKey",min(s.dt) from_time, max(s.dt)
to_time,'Red Hot Temperature' as WeatherType,'' as "Flood_height",'' as
"Severity" from (
select "LocKey","Date" + "Time" as dt,row_number() over
(partition by "LocKey" order by "Date" + "Time") as rn,
("Date" + "Time" - (concat(row_number() over (partition by
"LocKey" order by "Date" + "Time"),' hours')::interval)) as grp
from dome."AccuWeatherForcastHourly" where "Temperature" >
hotmaxtemp
order by "LocKey", "Date" + "Time") s
group by s."LocKey",s.grp
UNION
select s."LocKey",min(s.dt) from_time, max(s.dt)
to_time,'Amber Cold Temperature' as WeatherType,'' as "Flood_height",'' as
"Severity" from (
select "LocKey","Date" + "Time" as dt,
row_number() over (partition by "LocKey" order by "Date" +
"Time") as rn,
("Date" + "Time" - (concat(row_number() over (partition by
"LocKey" order by "Date" + "Time"),' hours')::interval)) as grp
from dome."AccuWeatherForcastHourly"
where "Temperature" between hotmintemp and hotmaxtemp
order by "LocKey", "Date" + "Time") s
group by s."LocKey",s.grp
UNION
select s."LocKey",min(s.dt) from_time, max(s.dt)
to_time,'Red Wind Speed' as WeatherType,'' as "Flood_height",'' as "Severity"
from (
select "LocKey","Date" + "Time" as dt,
row_number() over (partition by "LocKey" order by "Date" + "Time")
as rn,
("Date" + "Time" - (concat(row_number() over (partition by
"LocKey" order by "Date" + "Time"),' hours')::interval)) as grp
from dome."AccuWeatherForcastHourly"
where "Wind Speed" >windmax
order by "LocKey", "Date" + "Time") s
group by s."LocKey",s.grp
UNION
select s."LocKey",min(s.dt) from_time, max(s.dt)
to_time,'Amber Wind Speed' as WeatherType,'' as "Flood_height",'' as