Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - creating table WITHOUT parallel option

for some reason when I'm trying to CTAS (create table as select) on a large table (40million~ records ) I see in the v$session 18 active sessions with my sql statement.

when i'm trying to hint the optimizer to use less CPUs

create table table_name parallel (degree 2) as 
       select * from large_table;

I see 6 active sessions.

with degree 3 I see 8 active sessions. I tryed degree default but it created 18 sessions as well.

previously in the code , before executing the CREATE TABLE statement I changed few attributes in my session :

alter session set workarea_size_policy = manual;
alter session set hash_area_size = 1048576000;

I want to create the table in 1 session , how to I do it ?

Thanks !

like image 743
planben Avatar asked Sep 21 '12 16:09

planben


1 Answers

Use the NO_PARALLEL hint in the select clause of the CTAS statement. See also Note on Parallel Hints

create table t as
select /*+ NO_PARALLEL */ * 
from large_table;

Investigation & testing:

SQL*Plus version and Oracle Database version:

SQL*Plus: Release 11.1.0.6.0 - Production on Fri Sep 21 11:56:58 2012

Copyright (c) 1982, 2007, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options

I used two different sqlplus processes to connect to the same database via different service names. One running copy of sqlplus will be used to perform the CTAS. The other copy of sqlplus will be used to to query the number of sessions.

The query to get session count and results prior to anything else:

SQL> select service_name, count(*)
  2  from v$session
  3  where username = 'ME'
  4  group by service_name
  5  order by service_name;

SERVICE_NAME                                                       COUNT(*)
---------------------------------------------------------------- ----------
aaaaaa2                                                                   1
aaaaaa3                                                                   1

Creating large_table:

SQL> create table LAO as select * from all_objects;

Table created.

SQL> exec dbms_stats.gather_table_stats(user, 'LAO');

PL/SQL procedure successfully completed.

SQL> create table large_table parallel 4 as
  2  select N.N, LAO.*
  3  from LAO
  4  cross join (select level as N from dual connect by level <= 400) N
  5  /

Table created.

SQL> select to_char(count(*), 'fm999,999,999')
  2  from large_table;

TO_CHAR(COUN
------------
42,695,200

Since large_table was created with parallel 4, a simple CTAS defaults to 4 worker processes, CTAS:

create table t as
select * from large_table;

Sessions:

SERVICE_NAME                                                       COUNT(*)
---------------------------------------------------------------- ----------
aaaaaa2                                                                   5
aaaaaa3                                                                   1

Now, the noparallel table option. (Creating a table that by default won't have parallel plans for DML, but the parallelism of large_table causes the CTAS to run in parallel.)

drop table t;
create table t noparallel as
select * from large_table;

Sessions (SYS$BACKGROUND showed up towards the end of the last query, then just stuck around. I believe it dies if not needed in a certain amount of time.):

SERVICE_NAME                                                       COUNT(*)
---------------------------------------------------------------- ----------
SYS$BACKGROUND                                                            1
aaaaaa2                                                                   5
aaaaaa3                                                                   1

The NO_PARALLEL hint in the select clause does work:

drop table t;
create table t as
select /*+ NO_PARALLEL */ * from large_table;

Sessions:

SERVICE_NAME                                                       COUNT(*)
---------------------------------------------------------------- ----------
SYS$BACKGROUND                                                            1
aaaaaa2                                                                   1
aaaaaa3                                                                   1

One last CTAS, where a table is created that will use multiple processes by default, but will not during the CTAS:

drop table t;
create table t parallel 4 as
select /*+ NO_PARALLEL */ * from large_table;

Sessions:

SERVICE_NAME                                                       COUNT(*)
---------------------------------------------------------------- ----------
SYS$BACKGROUND                                                            1
aaaaaa2                                                                   1
aaaaaa3                                                                   1
like image 179
Shannon Severance Avatar answered Sep 18 '22 15:09

Shannon Severance