Skip to Main Content
  • Questions
  • DBMS_PARALLEL_EXECUTE getting chunks to work in special order

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Oleg.

Asked: February 07, 2017 - 11:04 am UTC

Last updated: February 07, 2017 - 7:27 pm UTC

Version: Oracle Database 11g Release 11.2.0.3.0

Viewed 1000+ times

You Asked

Hi Tom,

I'm using DBMS_PARALLEL_EXECUTE package to run in parallel my PL/SQL procedure's work.

The chunks are generated by my own SQL on table which contains numeric field "priority" like this

   v_sql := '
    select rowid, rowid
    from tmp_table
    order by priority
  ';

  DBMS_PARALLEL_EXECUTE.CREATE_CHUNKS_BY_SQL(
    task_name   => v_task_name,
    sql_stmt => v_sql,
    by_rowid  => true
  );


And then I run task with parallel_level = 2


      v_sql_stmt :=  '
      declare
        v_schema_name varchar2(100);
        v_sql varchar2(1000);
      begin

        select schema_name into v_schema_name
        from tmp_table
        where rowid between :start_id and :end_id;
        
        v_sql := 
        ........

        execute immediate v_sql;
      end;
    ';

   DBMS_PARALLEL_EXECUTE.run_task (
      task_name => v_task_name,
      sql_stmt => v_sql_stmt,
      language_flag => DBMS_SQL.NATIVE,
      parallel_level => 2
    );


Next SQL gets result

  SELECT  *
  FROM    user_parallel_execute_chunks c
  WHERE   c.TASK_NAME=v_task_name;

CHUNK_ID           TASK_NAME              STATUS           START_ROWID             END_ROWID                              JOB_NAME
23645805 RUNCR_TASK$_23645803 PROCESSED ABVN77AAIAAOFnbAAB ABVN77AAIAAOFnbAAB   TASK$_17757_1 
23645806 RUNCR_TASK$_23645803 ASSIGNED ABVN77AAIAAOFnbAAC ABVN77AAIAAOFnbAAC   TASK$_17757_2 
23645807 RUNCR_TASK$_23645803 ASSIGNED ABVN77AAIAAOFnbAAD ABVN77AAIAAOFnbAAD   TASK$_17757_1         
23645808 RUNCR_TASK$_23645803 UNASSIGNED ABVN77AAIAAOFnbAAE ABVN77AAIAAOFnbAAE   null 
23645809 RUNCR_TASK$_23645803 UNASSIGNED ABVN77AAIAAOFnbAAF ABVN77AAIAAOFnbAAF   null 
23645810 RUNCR_TASK$_23645803 UNASSIGNED ABVN77AAIAAOFnbAAG ABVN77AAIAAOFnbAAG   null 
23645811 RUNCR_TASK$_23645803 UNASSIGNED ABVN77AAIAAOFnbAAH ABVN77AAIAAOFnbAAH   null 
23645804 RUNCR_TASK$_23645803 UNASSIGNED ABVN77AAIAAOFnbAAA ABVN77AAIAAOFnbAAA   null 


So, "order by" expression in SQL to creating chunks work correct because CHUNK_ID in user_parallel_execute_chunks increases in accordance with field "priority" in table tmp_table, but first chunks which were started have IDs greater than the minimum (23645804).

Is there a way to force to take in the work chunks in special order? I expected than chunks will work in ascending CHUNK_ID order (23645804, 23645805, 23645806...)

Thanks in advance.

and Connor said...

Thats not how parallel execution works. Under the covers, we're submitting scheduler jobs to run things concurrently.

So even if we *submit* jobs in chunk order, then it is simply the "first available" scheduler job that will grab a submitted job and process it. You're *likely* to have a rough ordering assuming every job takes the same amount of time to run, but there are *no* guarantees.

You *could* code this up in your procedure itself (ie, "I am chunk 7, make sure chunks 1-6 are done before I start doing my work")...but that's a slippery slope to get into. You could easily get yourself into a deadlock style scenarios, where jobs cannot start and are waiting on each other etc.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Oleg Kholodov, February 08, 2017 - 8:33 am UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library