欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

oracle 并行之并行度篇

程序员文章站 2022-05-10 08:57:54
...

操作内并行使用的slave process数量就是并行度dop,indextable都有dop 作为默认操作并行度default 1表示不使用并行处理 SQL create table t1 (a int) parallel 6; Table created. SQL select degree from user_tables where table_name=’T1′; DEGREE ——

操作内并行使用的slave process数量就是并行度dop,index&table都有dop 作为默认操作并行度default 1表示不使用并行处理

SQL> create table t1 (a int) parallel 6;

Table created.

SQL> select degree from user_tables where table_name=’T1′;

DEGREE
———-
6

SQL> alter table t1 parallel 3;

Table altered.

SQL> select degree from user_tables where table_name=’T1′;

DEGREE
———-
3
*禁用alter table(index) parallel 1 (noprallel)
#create 时候使用parallel不仅会在创建table&index时使,后续的操作ddl,dml也会使用(如果只想建表时使用,建好后修改)

SQL> create table t2 (a int) parallel;(未指定并行度)

Table created.

SQL> select degree from user_tables where table_name=’T2′;

DEGREE
———-
DEFAULT ~~~这样使用default并行度=(cpu_count*parallel_threads_per_cpu)

SQL> show parameter cpu_count

NAME TYPE VALUE
———————————— ———– ——————————
cpu_count integer 2
SQL> show parameter parallel_thread

NAME TYPE VALUE
———————————— ———– ——————————
parallel_threads_per_cpu integer 2
SQL>

SQL> insert into t2 values (1);

1 row created.

SQL> commit;

Commit complete.

SQL> set autotrace trace exp
SQL> select * from t2;

Execution Plan
———————————————————-
Plan hash value: 1216610266

——————————————————————————–
——————————

| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
| TQ |IN-OUT| PQ Distrib |

——————————————————————————–
——————————

| 0 | SELECT STATEMENT | | 1 | 13 | 2 (0)| 00:00:01
| | | |

| 1 | PX COORDINATOR | | | | |
| | | |

| 2 | PX SEND QC (RANDOM)| :TQ10000 | 1 | 13 | 2 (0)| 00:00:01
| Q1,00 | P->S | QC (RAND) |

| 3 | PX BLOCK ITERATOR | | 1 | 13 | 2 (0)| 00:00:01
| Q1,00 | PCWC | |

| 4 | TABLE ACCESS FULL| T2 | 1 | 13 | 2 (0)| 00:00:01
| Q1,00 | PCWP | |

——————————————————————————–
——————————

Note
—–
– dynamic sampling used for this statement

SQL>
SQL> set autotrace off
SQL> select process from v$pq_tqstat;

no rows selected

可以看到 用set autotrace 后 查v$pq_tqstat norows ,这是2个原因造成的
1.set autotrace的原理
开启autotrace时候 一个process对应 2个 session
通常情况下是一个 session对应一个 server processs,但SErVER PORCESSS 可以对应多个session

SQL> conn xh/a831115
已连接。
SQL> select distinct sid from v$mystat;

SID
———-
144

SQL> select username, sid, serial#, server, paddr, status from v$session where s
id=144;

USERNAME SID SERIAL# SERVER PADDR STATUS
—————————— ———- ———- ——— ——– ——–

XH 144 27 DEDICATED 20E4CC3C INACTIVE

SQL> select program ,addr from v$process where addr=(select paddr from v$session
where sid=144);

PROGRAM ADDR
—————————————————————- ——–
ORACLE.EXE (SHAD) 20E4CC3C

SQL> select sid from v$session where paddr=’20E4CC3C’;

SID
———-
144

SQL> set autotrace on
SQL> select sid from v$session where paddr=’20E4CC3C’;

SID
———-
144
154

2.v$pq_tqstat只提供当前session最后一次并行执行sql语句的信息
综合2点可以看出set autotrace 最后一个session 是 执行计划的,所以v$pq_tqstat为no rows

所以 直接执行
SQL> select * from t2;

A
———-
1

SQL> SELECT dfo_number, tq_id, server_type, process, num_rows, bytes
2 FROM v$pq_tqstat
3 ORDER BY dfo_number, tq_id, server_type DESC, process;

DFO_NUMBER TQ_ID SERVER_TYP PROCESS NUM_ROWS BYTES
———- ———- ———- ———- ———- ———-
1 0 Producer P000 1 24
1 0 Producer P001 0 20
1 0 Producer P002 0 20
1 0 Producer P003 0 20
1 0 Consumer QC 1 84

可以看到启动了4个slave process,这4个process=cpu_count*parallel_threads_per_cpu(2*2)

相关的hint:
parallel(10g,11g),no_parallel(10g,11g),parallel_index(10g,11g,9i),no_parallel_index(10g,11g)
noparallel(9i),noparallel_index(9i)
##需要知道并行hint只是告诉优化器可以使用并行,但不是强制使用并行一切还是从cost出发(重点)
declare
begin
for i in 1..1000 loop
insert into t2 values(i);
end loop;
commit;
end;

SQL> explain plan set statement_id=’t2_pp’ for select /*+parallel(t2 2)*/* from t2 where a>900;

Explained.

SQL> create index t2_id on t2 (a);

Index created.

SQL> explain plan set statement_id=’t2_id’ for select /*+parallel(t2 2)*/* from t2 where a>900;

Explained.

SQL> set linesize 1000
SQL> select * from table (dbms_xplan.display(null,’t2_pp’));

PLAN_TABLE_OUTPUT
————————————————————————————————————————————————————————————————————————————————————————————————————
Plan hash value: 1216610266

————————————————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | TQ |IN-OUT| PQ Distrib |
————————————————————————————————————–
| 0 | SELECT STATEMENT | | 100 | 1300 | 2 (0)| 00:00:01 | | | |
| 1 | PX COORDINATOR | | | | | | | | |
| 2 | PX SEND QC (RANDOM)| :TQ10000 | 100 | 1300 | 2 (0)| 00:00:01 | Q1,00 | P->S | QC (RAND) |
| 3 | PX BLOCK ITERATOR | | 100 | 1300 | 2 (0)| 00:00:01 | Q1,00 | PCWC | |
|* 4 | TABLE ACCESS FULL| T2 | 100 | 1300 | 2 (0)| 00:00:01 | Q1,00 | PCWP | |
————————————————————————————————————–

PLAN_TABLE_OUTPUT
————————————————————————————————————————————————————————————————————————————————————————————————————

Predicate Information (identified by operation id):
—————————————————

4 – filter(“A”>900)

Note
—–
– dynamic sampling used for this statement

20 rows selected.

SQL> select * from table (dbms_xplan.display(null,’t2_id’));

PLAN_TABLE_OUTPUT
————————————————————————————————————————————————————————————————————————————————————————————————————
Plan hash value: 523330294

————————————————————————–
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
————————————————————————–
| 0 | SELECT STATEMENT | | 100 | 1300 | 2 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| T2_ID | 100 | 1300 | 2 (0)| 00:00:01 |
————————————————————————–

Predicate Information (identified by operation id):
—————————————————

PLAN_TABLE_OUTPUT
————————————————————————————————————————————————————————————————————————————————————————————————————

1 – access(“A”>900)

Note
—–
– dynamic sampling used for this statement

17 rows selected.

可以看到 即使在使用并行与走index时cost一样时候还是选择了走index(cost表面一样,若使用trace alter session set events ’10053 trace name context forever’;看的更详细,并行cpu cost要高些,所以选择了走index,所以hint parallel,parallel_index只是告诉query optimizer
可以考虑并行 不是强制使用)