Oracle并行计算

【前言】现在CPU的发展已不仅朝着单个性能更好的方向了,而且还朝着多核数多核心的方向发展了。Oracle数据库大部分也都是利用单线程的串行方式在运行。通过并行(Parallel)操作特性,充分应用CPU的多核心特点,提高对数据的操作效率,满足在特定场景下对海量数据操作的需求。

OLTP系统最主要的核心还是数据的录入操作,而这些应用的场景并不适合于并行计算的方式。对于OLAP业务场景更适合并行的操作,并行也成为数据库仓库调优利器;

【使用类型和场景】

Oracle并行处理(Parallel Processing)特性主要是针对SQL语句处理的并行。目前Oracle提供支持并行的操作包括如下类型:

  • 并行查询操作;
  • 并行DDL,对数据对象的DDL操作;
  • 并行DML,进行并行的数据更新修改;

在具体的应用场景上,有如下场景:

  • 数据表、索引范围扫描的查询;
  • 批量增加、修改和删除操作;
  • 数据表、索引对象的创建;
  • "DBMS_STATS"收集统计量;
  • 备份恢复管理器Recovery Manager的使用;


【设置并行和并行度】

  • Alter session force parallel query parallel n;(ALTER SESSION enable parallel query;) 单个SESSION里面
  • Alter table tab1 parallel n; 单个TABLE
  • Select /*+parallel(tab n)*/ from tab; Hint设置单个SQL

优先级:Hint > session > object


【并行度设置】并行度的设置是以多核CPU为核心的,所以并行度不能超过CPU的逻辑数量,Oracle官方文档介绍如下

If the PARALLEL clause is specified but no degree of parallelism is listed, the object gets the default DOP. Default parallelism uses a formula to determine the DOP based on the system configuration, as in the following:

  • For a single instance, DOP = PARALLEL_THREADS_PER_CPU x CPU_COUNT
  • For an Oracle RAC configuration, DOP = PARALLEL_THREADS_PER_CPU x CPU_COUNT x INSTANCE_COUNT



【实验测试】

环境说明:

  • 数据库信息:

点击(此处)折叠或打开

  1. SQL> select * from v$version where rownum<2;
  2. BANNER
  3. -----------------------------------------------------------------------------------------
  4. Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production


  • CPU信息

点击(此处)折叠或打开

  1. SQL> show parameter cpu
  2. NAME TYPE VALUE
  3. ------------------------------------ ----------- -------------
  4. cpu_count integer 8
  5. parallel_threads_per_cpu integer 1
  6. resource_manager_cpu_allocation integer 8

【实验步骤】

正常情况下的执行计划


点击(此处)折叠或打开

  1. SQL> alter session set STATISTICS_LEVEL=ALL;
  2. Session altered.
  3. SQL> select count(1) from edidc;
  4. COUNT(1)
  5. ----------
  6. 47262191

  7. SQL> select * from table(dbms_xplan.DISPLAY_CURSOR(null, null, 'ALLSTATS'));

  8. PLAN_TABLE_OUTPUT
  9. --------------------------------------------------------------------------------
  10. SQL_ID 8h0snkxwm7x0w, child number 0
  11. -------------------------------------
  12. select count(1) from sapsr3.edidc

  13. Plan hash value: 2138151876

  14. ----------------------------------------------------------------------------------------------------------------------
  15. Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
  16. -------------------------------------------------------------------------------------------------------------------
  17. | 0 | SELECT STATEMENT | | 1 | | 1 |00:00:15.52| 351K|
  18. | 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:15.52| 351K|
  19. | 2 | INDEX FAST FULL SCAN | EDIDC~1 | 1 | 46M | 47M |00:00:09.95| 351K|
  20. --------------------------------------------------------------------------------------------------------------------


修改session的方式


点击(此处)折叠或打开

  1. SQL> alter session set STATISTICS_LEVEL=ALL;
  2. SQL> Alter session force parallel query;
  3. SQL> select count(1) from edidc;
  4. COUNT(1)
  5. ----------
  6. 47262191
  7. SQL> select * from table(dbms_xplan.DISPLAY_CURSOR(null, null, 'ALLSTATS'));
  8. -------------------------------------------------------------------------
  9. | Id | Operation | Name | E-Rows |
  10. -------------------------------------------------------------------------
  11. | 0 | SELECT STATEMENT | | |
  12. | 1 | SORT AGGREGATE | | 1 |
  13. | 2 | PX COORDINATOR | | |
  14. | 3 | PX SEND QC (RANDOM) | :TQ10000 | 1 |
  15. | 4 | SORT AGGREGATE | | 1 |
  16. | 5 | PX BLOCK ITERATOR | | 46M |
  17. |* 6 | INDEX FAST FULL SCAN| EDIDC~1 | 46M |
  18. ----------------------------------------------------------------------------
  19. Predicate Information (identified by operation id):
  20. ----------------------------------------------------------------------------
  21. 6 - access(:Z>=:Z AND :Z<=:Z)
  22. ------------------------------------------------------


用hint的方式进行


点击(此处)折叠或打开

  1. SQL> select /*+ PARALLEL(4) */ count(1) from edidc;
  2. COUNT(1)
  3. ----------
  4. 47262191

  5. SQL> select * from table(dbms_xplan.DISPLAY_CURSOR(null, null, 'ALLSTATS'));
  6. PLAN_TABLE_OUTPUT
  7. --------------------------------------------------------------------------------
  8. SQL_ID gr4rp3q9c4qu3, child number 0
  9. -------------------------------------
  10. select /*+ PARALLEL(4) */ count(1) from sapsr3.edidc

  11. Plan hash value: 152749150

  12. -------------------------------------------------------------------------------------------------------------------------------------
  13. | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads |
  14. -------------------------------------------------------------------------------------------------------------------------------------------
  15. | 0 | SELECT STATEMENT | | 1 | | 1 |00:00:13.86 | 25 | 0 |
  16. | 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:13.86 | 25 | 0 |
  17. | 2 | PX COORDINATOR | | 1 | | 4 |00:00:13.86 | 25 | 0 |
  18. | 3 | PX SEND QC (RANDOM) | :TQ10000 | 0 | 1 | 0 |00:00:00.01 | 0 | 0 |
  19. | 4 | SORT AGGREGATE | | 4 | 1 | 4 |00:00:52.76 | 357K | 351K |
  20. | 5 | PX BLOCK ITERATOR | | 4 | 46M | 47M |00:00:41.69 | 357K | 351K |
  21. |* 6 | INDEX FAST FULL SCAN | EDIDC~1 | 136 | 46M | 47M |00:00:20.42 | 357K | 351K |
  22. --------------------------------------------------------------------------------------------------------------------------------------
  23. Predicate Information (identified by operation id):
  24. ---------------------------------------------------
  25. 6 - access(:Z>=:Z AND :Z<=:Z)
  26. PLAN_TABLE_OUTPUT
  27. --------------------------------------------------------------------------------


修改table的方式


点击(此处)折叠或打开

  1. SQL> ALTER TABLE edidc PARALLEL 2; #设置完成后需要更新统计信息

  2. SQL> SELECT TABLE_NAME, degree FROM dba_tables WHERE TABLE_NAME='EDIDC';
  3. TABLE_NAME DEGREE
  4. ------------------------------ ------------------------------
  5. EDIDC 2
  6. SQL> alter session set STATISTICS_LEVEL=ALL;
  7. Session altered.
  8. SQL> select count(1) from edidc;
  9. COUNT(1)
  10. ----------
  11. 47262191

  12. SQL> select * from table(dbms_xplan.DISPLAY_CURSOR(null, null, 'ALLSTATS'));
  13. --------------------------------------------------------------------------------------------------------------------------
  14. | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads |
  15. -------------------------------------------------------------------------------------------------------------------------
  16. | 0 | SELECT STATEMENT | | 1 | | 1 |00:00:12.93 | 25 | 0 |
  17. | 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:12.93 | 25 | 0 |
  18. | 2 | PX COORDINATOR | | 1 | | 8 |00:00:12.93 | 25 | 0 |
  19. | 3 | PX SEND QC (RANDOM) | :TQ10000 | 0 | 1 | 0 |00:00:00.01 | 0 | 0 |
  20. | 4 | SORT AGGREGATE | | 8 | 1 | 8 |00:01:39.17 | 359K| 351K|
  21. | 5 | PX BLOCK ITERATOR | | 8 | 46M | 47M|00:01:23.50 | 359K| 351K|
  22. |* 6 | INDEX FAST FULL SCAN | EDIDC~1 | 164 | 46M| 47M|00:00:54.01 | 359K| 351K|
  23. ------------------------------------------------------------------------------------------------------------------------
  24. Predicate Information (identified by operation id):
  25. ---------------------------------------------------
  26. 6 - access(:Z>=:Z AND :Z<=:Z)
  27. PLAN_TABLE_OUTPUT
  28. ---------------------------------------------------

  29. SQL>ALTER TABLE TABLE_NAME NOPARALLEL; 取消并行


以上是针对查询的操作,同样也是适用于DML操作的;

并行的使用并是简单的以上的几个语句的套用,在OLTP系统中使用需谨慎,用好了是一把调优的利器,用不好可能反而会被利器所伤。本文档也是是针对并行的一篇基础文章,后续会针对更深入的应用继续说明。

上一篇:五分钟带你玩转SpringSecurity(四)配置全解析,带你掌握security核心要点


下一篇:面试官:JavaScript 原始数据类型 Symbol 有什么用?