This control transfer between the PL/SQL engine and the SQL engine is called context-switching, and each time it is switched, there is additional overhead.
See the following picture:
However, FORALL and BULK COLLECT allow the PL/SQL engine to compress multiple contexts into one, which dramatically reduces the time spent on executing SQL statements that process multiple rows of records in PL/SQL.
Look at the following picture again:
Here's a detailed description of the two men
1. Accelerate queries through BULK COLLECT
Usage of BULK COLLECT
BULK COLLECT enables query results to be loaded into collections at once rather than processed one by one through cursor
BULK COLLECT can be used in select in, fetch in, returning into statements
Note that when using BULK COLLECT, all INTO variables must be collections
Give me a few simple examples:
Use bulk collect in select into statement
(2) Using bulk collect in fetch into
(3) Using bulk collect in returning into
BULK COLLECT's optimization of large data DELETE UPDATE
DELETE is the same as UPDATE.
Examples:
10 million rows of data need to be deleted from a large table with 100 million rows
Requirements are completed as quickly as possible with minimal impact on other applications of the database
If the business cannot be stopped, the following ideas can be consulted:
According to ROWID fragmentation, reuse Rowid sorting, batch processing, return table deletion
This is the best way to choose when the business can't stop.
Controlling submission within 10,000 lines generally does not cause too much pressure on rollback sections
When I do large DML, I usually choose to submit one or two thousand lines.
Choosing to do it at the peak of the business does not have much impact on the application.
The code is as follows:
(3) Restrict the number of records extracted by BULK COLLECT
Grammar:
FETCH cursor BULK COLLECT INTO ...[LIMIT rows];
Where rows can be constants, variables, or the result of evaluation is an expression of integers
Assuming you need to query and process 1W rows of data, you can use BULK COLLECT to fetch all rows at once and then fill them into a very large collection
However, this approach consumes a lot of PGA in the session, and APP may suffer from performance degradation due to PGA page changes.
At this point, the LIMIT clause is very useful to help us control how much memory the program uses to process data.
Examples:
(4) Batch extraction of multiple columns
Demand:
Extract all specific traffic information in the transportation table that consumes less than 20 km/RMB of fuel
The code is as follows:
(5) Use RETURNING clause for batch operations
With the returning clause, we can easily determine the results of the DML operation we just completed without additional query work.
For an example, see the third small point of the use of BULK COLLECT
2. Accelerating DML through FORALL
FORALL tells the PL/SQL engine to bind all members of one or more collections to an SQL statement before sending the statement to the SQL engine.
(1) Grammar
To be continued,
To: http://blog.csdn.net/dba_waterbin/article/details/8581448