05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

642<br />

CHAPTER 14 ■ PARALLEL EXECUTION<br />

Here, we just want the data split up. How the data is split up is not relevant to our processing,<br />

so our definition looks like this:<br />

ops$tkyte-ORA10G> create or replace<br />

2 function parallel_pipelined( l_cursor in sys_refcursor )<br />

3 return t2_tab_type<br />

4 pipelined<br />

5 parallel_enable ( partition l_cursor by any )<br />

We’d like to be able to see what rows were processed by which parallel execution servers,<br />

so we’ll declare a local variable L_SESSION_ID <strong>and</strong> initialize it from V$MYSTAT:<br />

6<br />

7 is<br />

8 l_session_id number;<br />

9 l_rec t1%rowtype;<br />

10 begin<br />

11 select sid into l_session_id<br />

12 from v$mystat<br />

13 where rownum =1;<br />

Now we are ready to process the data. We simply fetch out a row (or rows, as we could certainly<br />

use BULK COLLECT here to array process the ref cursor), perform our complex process on<br />

it, <strong>and</strong> pipe it out. When the ref cursor is exhausted of data, we close the cursor <strong>and</strong> return:<br />

14 loop<br />

15 fetch l_cursor into l_rec;<br />

16 exit when l_cursor%notfound;<br />

17 -- complex process here<br />

18 pipe row(t2_type(l_rec.id,l_rec.text,l_session_id));<br />

19 end loop;<br />

20 close l_cursor;<br />

21 return;<br />

22 end;<br />

23 /<br />

Function created.<br />

And that’s it. We’re ready to process the data in parallel, letting <strong>Oracle</strong> figure out based on<br />

the resources available what the most appropriate degree of parallelism is:<br />

ops$tkyte-ORA10G> alter session enable parallel dml;<br />

Session altered.<br />

ops$tkyte-ORA10G> insert /*+ append */<br />

2 into t2(id,text,session_id)<br />

3 select *<br />

4 from table(parallel_pipelined<br />

5 (CURSOR(select /*+ parallel(t1) */ *<br />

6 from t1 )<br />

7 ))

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!