AnalysisOracle Data Scanning
--Oracle SQL query optimization
(1)
Data Reading Method
1,Oracle Data Reading:
1.1 data read operation type:
In any database, when we initiate an SQL statement to query data, the SQL Execution engine scans data to return the data we need. This scan may occur directly in a specific memory space or on a disk where data is stored. However, data scanning can be classified into two types, namely sequential scanning and random reading.
Sequential scanning mainly occurs when reading a large range of data, while random scanning mainly occurs when locating a single record of data. In addition, sequential scanning is usually accompanied by disk I/O. That is to say, sequential scanning is usually not performed when data is directly read from the memory; however, random reading may occur when the memory data is read or when the disk data is read. When an SQL statement is executed to read data and the data can be hit from the data buffer, random memory data reading usually occurs. However, when data is accessed through an index, when the data is not cached in the data buffer, the data corresponding to the index will be read back to the table through the index, and random I/O reads will occur on the disk.
Generally, if the number of data reads is small, no matter whether it is data scanning or random reading, the Data Reading performance will not be too bad, and data can be quickly returned. However, as the range of read data expands, random read will result in significant damage to Data Reading performance. If all the random reads occur in the memory, the damage is not immediately apparent, but with the passage of time and the constant expansion of the Data Reading range, therefore, even Memory Data Reading will show a sharp decline in performance. Because data is managed by complex data structures in the memory, it is inevitable to use various complex data structure Operation algorithms when operating the data in the memory, at the same time, it may be accompanied by various concurrency control policies. Therefore, the random read cost of large data volumes in memory is actually very high. However, the above situations are usually not common. More often, a large number of random disk I/O reads occur. This usually happens when, this is caused by poor index selection or high index clustering factor. This situation can cause serious damage to the overall performance of the system. Because Random I/O reads and writes are non-consecutive data, a large number of random I/O operations are required, which will inevitably lead to the non-stop reading and writing of data on the physical disk, this is very likely to cause the overall I/O bottleneck of the system. In Oracle, if there is a data read performance problem caused by a large number of random I/O, there will usually be a high degree of problems, such as buffer_busy_wait, db_sequence_read wait event time. Therefore, if a large range of Data Reading is involved, it is best to drive the database's execution engine to scan and read data instead of random I/O reading, that is to say, when a large data range is read, continuous I/O operations should be driven to the database, rather than non-sequential I/O operations, because the scan operation is actually a continuous I/O operation.
Scan operations are divided into two types: full range scan and partial range scan. Full range scan is a one-time scan."Driving query conditions"All the data in the range, and then apply the query filter conditions to filter the data based on the scan results to obtain the final query results. Partial range scan means that data scanning does not meet the requirements of one-time scanning."Driving query conditions"All the data in the range, but only scan the data that meets the certain range of driver query conditions. This scan range is far smaller than the overall data range, which is called the batch array array_size in Oracle, when the scanned data is sufficient to fill up a batch of arrays, the query results are immediately returned, and the query is stopped before the user asks for data browsing again.
It can be seen that local range scanning can greatly reduce the amount of data scanned, and quickly return the query results, improving the responsiveness of the query application. Partial range scanning does not actually reduce the range of query result data, but uses the features that most users do not need to browse all data at once. Therefore, only some data is returned for users to view. Therefore, in an application system that has a large amount of data and requires high system responsiveness and most query operations involve a large range of data, the execution engine of the database should be guided to perform local data range scanning to improve data query performance and overall system responsiveness.
The process of full range scan and partial range scan is shown in:
Global data scanning
Local data scanning
For full range scanning, first scan the data that meets the full range of driver query conditions, and then apply the filter conditions in the range, then, store the filtered data in a temporary bucket (which may be stored in memory or in external storage), perform secondary data processing on the temporary data, and then fill in the batch array, after the array is filled up, return the result and end the query until the next request appears.
In local range scanning, data is first scanned according to the driver's query conditions. At the same time, data is filtered by applying Filter query conditions, and rows that meet the conditions are directly filled into the batch array, after the array is filled up, the system returns the result and terminates the query. Therefore, no matter how large the data range is, the local range scan will return the query results quickly.
1.2 conditions for local range scanning:
In some cases, if the SQL Execution Mode is changed from global scan to local scan, the query execution speed may be greatly improved. However, not all SQL statements can be directly scanned by local range. Therefore, we should know the conditions for SQL queries to directly perform range scanning, at the same time, we also need to know how to direct SQL queries that cannot be directly used for local range scanning and guide the local range scanning method.
What kind of queries can be directly used for local range scanning? Generally, range scan is performed through highly selective filtering conditions ), at the same time, the SQL Execution optimizer can guide the execution engine to use a local range scan if the query result does not need to be obtained after all the data within the specified range is seen. These operations include reading data through the primary key, reading data through the unique index, and scanning data through various indexes (with high selectivity, these operations can be directly used for data reading through local range scanning.
On the contrary, operations that require reading all data within a range cannot be directly performed using local range scanning. These operations are all run in global data scan mode. These operations include grouping functions (count, sum, Min, Max, etc.), order by, group by, and distinct) use having for grouping and filtering queries, Set Operations (Union, minus, intersect, etc.), merge Join Operations merge join, and trigger disk sorting hash join (non
In Memory hash join ). Generally, if you find the SQL statement execution plan"Sort"Execution Plan, then we can usually conclude that this statement contains non-local scans. It may be because the entire statement cannot perform local scans, or a part of the domain cannot perform local scans (for example: subquery or table join operation). In this case, a statement optimization method is to check whether the non-local scan part can be guided into a local data scan through the pilot method (specific pilot method, ).
Here we also want to emphasize that most of the Set Operations cannot perform partial range scanning, but one exception is the Union all operation, because the Union all operation does not need to sort the results, therefore, it eliminates the need to obtain or sort the data as a whole. Therefore, this operation can be performed by local range scanning.
1.3 Effect of optimizer mode on local range scanning:
For an SQL statement, even if the local range scan can be implemented logically, the optimizer mode may vary. In the first_rows mode of RBO or CBO, the execution plan is basically developed according to the local range scan. In the all_rows mode of CBO, the execution plan is basically developed according to the global range scan. The RBO model is an out-of-date optimizer technology. Today, we will only use it in some very special occasions to play some of its special roles, so we will not discuss it here.
The first_rows mode of CBO is to optimize the scope of SQL operations. all_rows is to optimize the overall scope of SQL operations, that is, to pursue "Best throughput ". Based on the description of the local range scan of the data, do we have to set the optimizer to first_rows mode and then everything will be fine? In fact, this is not the case.
Based on the development of processing capabilities of mainstream server hardware platforms and the development of technologies supporting software such as database systems and the pursuit of high availability of system architecture, the all_rows model is an optimizer technology that conforms to the development trend, because it is the pursuit of the overall optimization, the pursuit of the performance cost of the entire system balance. I think this is also an important reason why all_rows is set as the default mode of the optimizer after Oracle10g.
So what should we set the correct optimizer policy? I think the correct optimizer setting policy should be to use the all_rows mode, and then use the optimizer pilot technology (for example: hints, SQL statement adjustment and rewriting, etc.) to induce the optimizer to use the correct data scanning method for data processing to obtain satisfactory results.