cancel
Showing results for 
Search instead for 
Did you mean: 

Run HANA Stored procedure in parallel

Former Member
0 Kudos

Dear all,

I have created a stored procedure in HANA which will select the parameter "Property" into an array, iterate over this array and call a second stored procedure ("pattern_Identification") with the parameter "Property" as input parameter. As I have around 20 different "Property" parameters the procedure "pattern_Identification" is called several times sequential and makes the whole process slowly. That's why I would like to parallelize the process.

Please let me know how I can use HANA capability to call this stored procedure parallelly ?

Regards,

Matthias


PROCEDURE "MY_SYSTEM"."My_Project.procedures::run_Pattern_Identification" (

  IN personID ALPHANUM(10)

)

  LANGUAGE SQLSCRIPT

  SQL SECURITY INVOKER

  DEFAULT SCHEMA TEST_SYSTEM

  AS

BEGIN

  DECLARE property_Array VARCHAR(50) ARRAY;

  DECLARE a_Length INT;

  DECLARE a_Index INT;

  DECLARE property VARCHAR(50);

  property_Tab = SELECT DISTINCT "PROPERTY" FROM "ENVIRONMENTAL_DATA";

  property_Array := ARRAY_AGG(:property_Tab.PROPERTY);

  a_Length := CARDINALITY(:property_Array);

  FOR a_Index IN 2 .. a_Length DO

  property := :property_Array[:a_Index];

  CALL "test_Project.procedures::pattern_Identification"(:property);           -- Call could be parallelly

  END FOR;

END;

Accepted Solutions (1)

Accepted Solutions (1)

jyoti_senapati
Participant
0 Kudos

Hi Matthias,

You can follow below steps to run these procedures parallelize.

1> Combine the data of Array into a string by storing Property names as comma separated. Use below codes

DECLARE v_property VARCHAR(300);

FOR a_index IN 1..a_Length DO

    

     IF a_index<a_Length THEN

         

          v_property:= :v_property||:property_array[:a_index]||','

     ELSE

          v_property:= :v_property||:property_array[:a_index]

     ENDIF;

END FOR;

2>Now change the input parameter of inner procedure as IN <paramter_name >VARCHAR(300)

and call the procedure making v_property as input paremeter.

  CALL "test_Project.procedures::pattern_Identification"(:v_property);


3>Inside this pattern_identification procedure split this string into the array and use the data of the array in the logic so procedures will run just like parallel process.


Let me know in case any issue.


Regards,

Jyoti

Former Member
0 Kudos

Hi Jyoti,

Thank you for your help.

Sorry but I don't fully understand Step 3.

When I split the string into the array I have again a array with all Properties inside

and I must iterate over it and  run my logic.

Where is the difference between my current solution?

May I just didn't understood something correctly.

Regards,

Matthias

jyoti_senapati
Participant
0 Kudos

Hi Matthias,


No need to iterate it gain inside the pattern_identification procedure after making string into array. If you are using the values of property inside a SELECT Statement than just put below codes.


DECLARE  v_propert_array VARCHAR(100) ARRAY;

DECLARE  v_count_arry INTEGER;

DECLARE v_index INTEGER;


WHILE(LENGTH(:v_property)>0) DO

     v_index:=v_index+1;

    

     IF (SUBSTR_BEFORE(:v_property, ',') ='') THEN

            v_propert_array[:v_index] := :v_property;

       ELSE

                 v_propert_array[:v_index] := SUBSTR_BEFORE(:v_property, ',');

  END IF ;

            v_property := SUBSTR_AFTER(:v_property,',');



END WHILE;


v_count_arry := CARDINALITY(:v_propert_array);

v_sel_properties := UNNEST (:v_property_array) AS (SELECTED_PROP);


Now you can use statemet ("SELECT SELECTED_PROPS FROM :v_sel_properties") in any SELECT Statement for any type of operation without using any iteration.


If you still face any issue then please paste that piece of code where you are putting your logic.


Regards,

Jyoti







Former Member
0 Kudos

Hi Jyoti

Thank you for your example. I still have problem to adopt it on my logic, that's why I will post the logic code also here.

My main problem is avoiding the use of cursor.

I have a table with dates of discomfort. And want to select from my environmental data for all properties the measured values from the days before a discomfort.

Regards

Matthias


PROCEDURE "MY_SYSTEM"."My_Project.procedures::pattern_Identification" (

        IN property VARCHAR(50)

)

  LANGUAGE SQLSCRIPT

  SQL SECURITY INVOKER

  DEFAULT SCHEMA TEST_SYSTEM

  AS

BEGIN

     DECLARE eventID INT;

     DECLARE sequenceID INT :=1;

     DECLARE prev_Day DATE;

     DECLARE discomfort_Day DATE;

     DECLARE item FLOAT;

     DECLARE timeframe_length INT = 5;

     DECLARE CURSOR c_cursor1 FOR SELECT "DATE" FROM "DISCOMFORT_T" ORDER BY "DATE";

     FOR cur_Row as c_cursor1 DO

          SELECT cur_Row."DATE" INTO discomfort_Day FROM DUMMY;

          FOR eventID IN 1 .. :timeframe_length DO   

              prev_Day := ADD_DAYS (:discomfort_Day , :eventID * (-1));

               SELECT "VALUE" INTO value FROM "ENVIRONMENTAL_DATA" WHERE "PROPERTY" = :property AND "DATE" = :prev_Day ;

               INSERT INTO "PATTERN_T" VALUES (:sequenceID, :eventID, :value, :property);

          END FOR;

          sequenceID := sequenceID +1;

     END FOR;

END;

jyoti_senapati
Participant
0 Kudos

Could you please try to re write the code removing Cursor.

Former Member
0 Kudos

Hi,

that's the problem where I stuck, as I don't know how I can remove Cursor.

Former Member
0 Kudos

Hi Matthais , I am also stuck with the same problem . were you  able to resolve your issue ? Appreciate if you can share your inputs / resolution here .

lbreddemann
Active Contributor
0 Kudos

Hi Matthias,

I know I am a bit late in this discussion, but it stuck to the back of my mind.

The main question is of course: why the procedure calls at all?

Looking at what happens in the procedures I figured it's this:

- find all different properties that had been collected so far

- for each of the properties

     - find all days with discomfort PLUS the 5 days before that day

     - for every day

           - read the date and the value of the current property (if available on this date)


- store the result in a table

That's definitively doable in a single SQL statement:


select row_number() over(partition by dates.date order by dates.off desc ) sequence_id

       ,   dates.date

       ,   dates.off as "eventID"

       ,   day_before

       ,   prop.property

       ,   prop."VALUE"

FROM        

    (select dc."DATE", date_range.off, add_days(dc."DATE", date_range.off) as day_before

     from discomfort_t dc

          cross join (select  0 as off from dummy union all

                     select -1 as off from dummy union all

                     select -2 as off from dummy union all

                     select -3 as off from dummy union all

                     select -4 as off from dummy union all

                     select -5 as off from dummy) date_range) dates

    inner join

            (select "DATE",  property, "VALUE" from environmental_data) prop

     on dates.day_before = prop."DATE"

    

order by   dates.date asc,  "eventID" desc

The dates "union"-sub-select generates the date offsets you are looking for.

This gets joined with the dates of days of discomfort.

We now have a list of dates of discomfort and the five days before that.

This then gets joined with all the measures properties and values.

Finally the row_number() produces the "sequence_id" starting new with every date of day of discomfort.

I don't have any test data of reasonable size but I don't see why this shouldn't be quicker then the two procedure approach...

Cheers,

Lars

So, now

Answers (1)

Answers (1)

Vlado
Advisor
Advisor
0 Kudos

Moved to proper space: