Reputation: 4642
I have a select SQL query which use parallelism, something like this
INSERT/*+ APPEND PARALLEL (tst, 6) */ INTO test_table tst
(
**************
**************
**************
)
SELECT /*+ PARALLEL (a, 6) */ DISTINCT
**************
**************
**************
FROM src_table a;
As you can see here, i have hard-coded the degree but, i don't want to do that, since the number of CPUs are not same across all the DB where this code is executed.
My requirement:
I need to query V$PARAMETER
for the available CPU count and use the value as result-2
in my query. Something like this...
DECLARE
degree varchar2(1);
BEGIN
select value-2 INTO degree from v$parameter where name='cpu_count';
INSERT/*+ APPEND PARALLEL (tst, degree) */ INTO test_table tst
(
**************
**************
**************
)
SELECT /*+ PARALLEL (a, degree) */ DISTINCT
**************
**************
**************
FROM src_table a;
END;
But, it is not working as i expected it to be and i see 32 parallel threads, irrespective of the available CPUs. Is this a right way to do? If not, is there any other solution for my requirement ?
Upvotes: 5
Views: 964
Reputation: 71
Why not force the degree for the session with:
alter session force parallel dml parallel <dop>;
alter session force parallel query parallel <dop>;
Without hinting you can fine granular define the degree you like.
Upvotes: 1
Reputation: 17548
Vivek,
You could use dynamic SQL to build up your INSERT statement within a PL/SQL function or procedure. That way you could utilise the variable "degree" that you have already retrieved.
Something like:
DECLARE
degree varchar2(1);
BEGIN
select value-2
INTO degree
from v$parameter
where name='cpu_count';
EXECUTE IMMEDIATE('INSERT /*+ APPEND PARALLEL (tst, '||degree||') */ '||
' INTO test_table tst ( '||
' ************** '||
' ************** '||
' ************** '||
' ) '||
'SELECT /*+ PARALLEL (a, '||degree||') */ '||
' DISTINCT '||
' ************** '||
' ************** '||
' ************** '||
' FROM src_table a');
END;
Upvotes: 3