In Chapter 3, we introduced Oracleâs parallel execution features and talked about the concepts behind how they work. These features include:
Parallel data loading
Parallel replication propagation
In this chapter, weâll describe how to use each of these features and show you how to take advantage of them. Most of these features come with a list of restrictions and caveats that may affect your ability to use them in a given situation. Weâll describe those as well.
Throughout this chapter, we assume that youâre familiar with the basic Oracle features being discussed. This isnât, for example, a tutorial on recovery or replication. The focus is always on the parallel features that can be applied to the task at hand. If youâre familiar with DML and DDL, for example, you wonât have any problem following the discussion of how to parallelize those types of statements. The same is true for the other types of operations covered in this chapter.
Parallel query is the most commonly used of Oracleâs parallel execution features. It was the first parallel execution feature to be developed by Oracle and was introduced in Oracle7 (release 7.1) as the Oracle Parallel Query Option (PQO). Parallel execution can significantly reduce the elapsed time for large queries, but it doesnât apply to every query. To parallelize a SELECT statement, the following conditions must be met:
At least one of the tables is accessed through a full table scan, or an index is accessed through a range scan involving multiple partitions.
If the execution involves a full table scan, the statement must contain a PARALLEL hint specifying the corresponding table, or the corresponding table must have a parallel declaration in its definition.
If the execution involves an index range scan spanning multiple partitions, the statement must contain a PARALLEL_INDEX hint specifying the corresponding index, or the corresponding index must have a parallel declaration in its definition.
The following two sections explain how the degree of parallelism is chosen for a SELECT statement and discuss restrictions on the use of the parallel query feature.
Oracle retrieves the DEGREE and INSTANCES specifications from the definition of all tables and indexes involved in the query and chooses the highest values found for those settings.
Oracle checks the statement for a parallel hint. If such a hint is found, the hint overrides the degree of parallelism obtained as a result of the previous step.
You can use the PARALLEL and PARALLEL_INDEX hints to specify the degree of parallelism for a SELECT statement. You can use the NOPARALLEL and NOPARALLEL_INDEX hints to ensure that parallel execution is not performed for the SELECT.
The PARALLEL hint controls the degree of parallelism applied to tables accessed by a query and takes this form:
/*+ PARALLEL (
Is the name of the table on which to parallelize operations
Specifies the number of parallel slave processes that each instance should use when performing operations on this table
Is the number of instances to be used for parallel operations on the table
The scope of the PARALLEL hint is the SELECT statement in which it appears. Subsequent SQL statements are not affected.
The PARALLEL_INDEX hint does for indexes what the PARALLEL hint does for tables. It can be used with SELECT statements to enable parallelized index range scans for partitioned indexes. The hint takes this form:
/*+ PARALLEL_INDEX (
Is the name of the table on which to parallelize operations
Is the name of the partitioned index on which to parallelize an index range scan
Specifies the number of parallel slave processes that each instance should use when performing operations on this index
Is the number of instances to be used for parallel operations on the index
The values for degree and instances can be either integers or the keyword DEFAULT. DEFAULT tells Oracle to use the default value for the instance.
Letâs look at some examples that demonstrate how hints are used to specify the degree of parallelism. The following example shows a hint that tells Oracle to scan the orders table in parallel using four parallel slave processes on one instance only. The hint will override any DEGREE and INSTANCES specifications in the table definition and also will override the default degree of parallelism for the instance:
SELECT /*+ PARALLEL(orders,4,1) */ COUNT(*) FROM orders;
The next example uses a PARALLEL_INDEX hint that calls for a parallel index range scan of the partitioned index named orders_uk. This parallel range scan will be performed with four parallel slave processes, with two instances each running two parallel slave processes:
SELECT /*+ PARALLEL_INDEX(orders,orders_uk,2,2) */ COUNT(*) FROM orders;
Hereâs an example that specifies only a degree value in the hint. Since no value is specified for instances, the default value from the table definition will be used. If the table definition does not have an INSTANCES specification or if the value is DEFAULT, then instances will pick up the instance-level default value. (See Section 3.2.2 in Chapter 3 for a discussion of statement-level, table-level, and instance-level default values.) For example:
SELECT /*+ PARALLEL(orders,4) */ COUNT(*) FROM orders;
In the following example, the PARALLEL hint tells Oracle to scan the orders table in parallel. Since no degree and instances values are specified, Oracle will pick up those values from the tableâs definition. If they werenât defined at the table level, Oracle will use the instance-level default values:
SELECT /*+ PARALLEL(orders) */ COUNT(*) FROM orders;
In the following example, the hint tells Oracle to scan the orders table in parallel using the default number of instances and using six slave processes on each instance. When you specify DEFAULT for either degree or instances in a hint, Oracle ignores the values in the table definition and uses the instance-level values. Since DEFAULT is specified in the example for instances, Oracle will use the instance-level value here:
SELECT /*+ PARALLEL(orders, 6, DEFAULT) */ COUNT(*) FROM orders;
You can turn off parallel operations for a statement using the NOPARALLEL hint. The NOPARALLEL hint takes this form:
/*+ NOPARALLEL (
table_name is the name of the table.
The following example includes the NOPARALLEL hint, ensuring that the customers table will not be accessed in parallel:
SELECT /*+ NOPARALLEL(customers) */ customer_id, location, name FROM customers;
If an index has a parallel specification in its definition, and you want to avoid a parallel index scan operation, you can use a NOPARALLEL_INDEX hint to achieve this goal. The NOPARALLEL_INDEX hint takes this form:
/*+ NOPARALLEL_INDEX (
Is the name of the table
Is the name of the index
Oracle can parallelize individual SELECT statements, and it can parallelize subqueries that appear in SELECT statements, other DML statements, or DDL statements. Parallelizing a SELECT statement has the following restrictions:
If the DDL or DML statement references a remote object, the query part canât be parallelized.
If a query involves joins and sorts on tables having object types, the query will not be parallelized if the object definitions do not have a MAP function.
Queries on nested tables canât be parallelized.
If you ask Oracle to execute a query in parallel, and it canât run in parallel because of these restrictions, Oracle will run the query serially without raising any error or warning.