
-- hint下之后就是非并行的
explain plan for select /*+ no_parallel */  count(status) from big_table;
select * from table(dbms_xplan.display(null,null, 'TYPICAL -ROWS -BYTES -COST'));

-- 修改big_table为并行的
alter table big_table parallel;

explain plan for select /*+ PARALLEL(4) */  count(status) from big_table;
select * from table(dbms_xplan.display(null,null, 'TYPICAL -ROWS -BYTES -COST'));

-- 在执行下句sql语句时，需要确认会话中不存在未提交的事务
alter session enable parallel dml;
select pdml_enabled from v$session where sid = sys_context('userenv','sid');
update big_table set status = 'done';

-- 在执行下句sql语句时，需要确认会话中不存在未提交的事务
explain plan for update big_table set status = 'done';
select * from table(dbms_xplan.display(null,null, 'BASIC +PARALLEL'));

-- 创建表user_info
create table user_info as select * from all_users;
alter table user_info parallel;
exec dbms_stats.gather_table_stats( user,'USER_INFO' );

-- select count(*) from all_users;
-- select * from all_users;

select * from table(dbms_xplan.display);

create table t1
as
select object_id id,object_name text
from
all_objects;

exec dbms_stats.set_table_stats( user,'T1',numrows=>10000000,numblks=>100000 );

create table t2
as
select t1.*,0 session_id
from t1
where 1=0;


create or replace type t2_type
as object (
	id           number,
	text         varchar2(30),
	session_id   number
)
/

create or replace type t2_tab_type
as table of t2_type
/

-- 并发处理
create or replace function
parallel_pipelined(l_cursor in sys_refcursor)
return t2_tab_type
pipelined
parallel_enable (partition l_cursor by any)
is
l_session_id  number;
l_rec         t1%rowtype;
begin
	select sid into l_session_id
	from v$mystat
	where rownum = 1;
	loop
		fetch l_cursor into l_rec;
		exit when l_cursor%notfound;
		-- complex process here
		pipe row(t2_type(l_rec.id,l_rec.text,l_session_id));
	end loop;
	close l_cursor;
	return;
end;
/

alter session enable parallel dml;
insert /*+ append */ into t2(id,text,session_id )
select * from table(parallel_pipelined (cursor (select /*+ parallel(t1) */ * from t1)));
