To manage asynchronous child jobs, use the AWAIT and
CANCEL statements:
The AWAIT statement waits for all asynchronous child jobs that are running to finish or for a specific child job that is
running for a RESULTSET to finish, then returns when the all jobs have finished or the specific job has finished, respectively.
CANCEL 语句会取消正在为 RESULTSET 运行的异步子作业。
You can check the status of an asynchronous child job that is running for a RESULTSET by calling the
SYSTEM$GET_RESULTSET_STATUS function.
When multiple asynchronous child jobs run concurrently in the same session, the job completion order isn’t
known until the jobs have finished running. Since the completion order can vary, using the
LAST_QUERY_ID function with asynchronous child jobs is
non-deterministic.
Queries both tables for the order_amount values in all rows and returns the results to
different RESULTSETs (one for each table).
使用 ASYNC 关键字指定查询作为并发子作业来运行。
Executes the AWAIT statement for each RESULTSET so
that the procedure waits for the queries to finish before proceeding. Query results for a
RESULTSET can’t be accessed until AWAIT is run for the RESULTSET.
Uses a cursor to calculate the sum of the order_amount rows for each table.
添加表的总计并返回值。
CREATEORREPLACEPROCEDURE test_sp_async_child_jobs_query()RETURNSINTEGERLANGUAGESQLASDECLARE
accumulator1 INTEGERDEFAULT0;
accumulator2 INTEGERDEFAULT0;
res1 RESULTSETDEFAULTASYNC(SELECT order_amount FROM orders_q1_2024);
res2 RESULTSETDEFAULTASYNC(SELECT order_amount FROM orders_q2_2024);BEGINAWAIT res1;LET cur1 CURSORFOR res1;OPEN cur1;AWAIT res2;LET cur2 CURSORFOR res2;OPEN cur2;FOR row_variable IN cur1 DO
accumulator1 := accumulator1 + row_variable.order_amount;ENDFOR;FOR row_variable IN cur2 DO
accumulator2 := accumulator2 + row_variable.order_amount;ENDFOR;RETURN accumulator1 + accumulator2;END;
CREATEORREPLACEPROCEDURE test_sp_async_child_jobs_query()RETURNSINTEGERLANGUAGESQLAS$$
DECLARE
accumulator1 INTEGER DEFAULT 0;
accumulator2 INTEGER DEFAULT 0;
res1 RESULTSET DEFAULT ASYNC (SELECT order_amount FROM orders_q1_2024);
res2 RESULTSET DEFAULT ASYNC (SELECT order_amount FROM orders_q2_2024);
BEGIN
AWAIT res1;
LET cur1 CURSOR FOR res1;
OPEN cur1;
AWAIT res2;
LET cur2 CURSOR FOR res2;
OPEN cur2;
FOR row_variable IN cur1 DO
accumulator1 := accumulator1 + row_variable.order_amount;
END FOR;
FOR row_variable IN cur2 DO
accumulator2 := accumulator2 + row_variable.order_amount;
END FOR;
RETURN accumulator1 + accumulator2;
END;
$$;
Creates the orders_q3_2024 table if it doesn’t exist.
Creates two RESULTSETs, insert_1 and insert_2, that hold the results of inserts into the table.
The stored procedure arguments specify the values that are inserted into the table.
指定使用 ASYNC 关键字将插入作为并发子作业来运行。
Executes the AWAIT statement for each RESULTSET so
that the procedure waits for the inserts to finish before proceeding. The results of a
RESULTSET can’t be accessed until AWAIT is run for the RESULTSET.
Creates a new RESULTSET res that holds the results of a query on the orders_q3_2024 table.
CREATEORREPLACEPROCEDURE test_async_child_job_inserts()RETURNSVARCHARLANGUAGESQLAS$$
BEGIN
CREATE OR REPLACE TABLE test_child_job_queries1 (col1 INT);
ASYNC (INSERT INTO test_child_job_queries1(col1) VALUES(1));
ASYNC (INSERT INTO test_child_job_queries1(col1) VALUES(2));
ASYNC (INSERT INTO test_child_job_queries1(col1) VALUES(3));
AWAIT ALL;
END;
$$;
创建可并发更新值的存储过程
以下存储过程使用 ASYNC 关键字运行多个子作业,并发地更新表中的行。示例为 UPDATE 语句指定了 ASYNC 关键字。该示例还使用了 AWAIT ALL 语句,以便存储过程等待所有异步子作业完成。
创建表并插入数据:
CREATEORREPLACETABLE test_child_job_queries2 (id INT, cola INT);INSERTINTO test_child_job_queries2 VALUES(1,100),(2,101),(3,102);
创建存储过程:
CREATEORREPLACEPROCEDURE test_async_child_job_updates()RETURNSVARCHARLANGUAGESQLASBEGINASYNC(UPDATE test_child_job_queries2 SET cola=200WHERE id=1);ASYNC(UPDATE test_child_job_queries2 SET cola=201WHERE id=2);ASYNC(UPDATE test_child_job_queries2 SET cola=202WHERE id=3);AWAITALL;END;
CREATEORREPLACEPROCEDURE test_async_child_job_updates()RETURNSVARCHARLANGUAGESQLAS$$
BEGIN
ASYNC (UPDATE test_child_job_queries2 SET cola=200 WHERE id=1);
ASYNC (UPDATE test_child_job_queries2 SET cola=201 WHERE id=2);
ASYNC (UPDATE test_child_job_queries2 SET cola=202 WHERE id=3);
AWAIT ALL;
END;
$$;
Create a stored procedure that inserts values from async_loop_test1, concatenated with the text
async_ into async_loop_test2 using asynchronous child jobs in a FOR loop. The loop creates a
separate asynchronous child job on each iteration. The AWAIT ALL statement blocks progress in the
stored procedure until all of the child jobs are done.
CREATEORREPLACEPROCEDURE async_insert()RETURNSVARCHARLANGUAGESQLEXECUTEASCALLERASbeginLET res RESULTSET:=(SELECT*FROM async_loop_test1 ORDERBY1);FOR record IN res DOLET v VARCHAR:= record.col1;LET x INT:= record.col2;ASYNC(INSERTINTO async_loop_test2(col1, col2)VALUES(:x,(SELECT'async_'||:v)));ENDFOR;AWAITALL;RETURN'Success';END;
CREATEORREPLACEPROCEDURE async_insert()RETURNSVARCHARLANGUAGESQLEXECUTEASCALLERAS$$
begin
LET res RESULTSET := (SELECT * FROM async_loop_test1 ORDER BY 1);
FOR record IN res DO
LET v VARCHAR := record.col1;
LET x INT := record.col2;
ASYNC (INSERT INTO async_loop_test2(col1, col2) VALUES (:x, (SELECT 'async_' || :v)));
END FOR;
AWAIT ALL;
RETURN 'Success';
END;
$$;