Three common causes of poor SQL query statement performance and Optimization Methods
1. Reasons for affecting SQL Performance
There are many factors that affect SQL performance, such as incorrect initialization parameter settings and inaccurate system statistics imported, thus affecting the proper judgment of the program. Starting from SQL statements, SQL Performance is affected by the following three major reasons:
(1) perform high-cost operations on a large record set, such as using predicates that cause sorting.
(2) If an appropriate index is not created, a full table scan is performed on the query table.
(3) Too many useless records are processed. For example, the intermediate result set contains too many useless records due to improper filtering conditions during multi-table join.
2. SQL statement Optimization
For the query statements provided by the relational database system, the query handler in the system can automatically optimize the query. However, for a complex query, several different query statements can be used to express the requirements. Although the results are the same, the execution process may be very different. Therefore, it is necessary to study the optimization of SQL statement query, write the optimal query statement for specific query operations, and optimize the query as much as possible when the system overhead is minimal. Query Optimization is optimized by logical methods and by physical methods.
2. 1. Optimize sorting operations
When sorting predicates (Clauses of order by and group by, distinct modifier, union, intersect, and minus set operator) are used in a query statement, the performance of SQL statements is affected, in actual applications, most of the constraints such as primary keys do not need to be sorted. When the data set to be sorted is not large, the server completes sorting in memory. If sorting requires more memory space, the server will flatter multiple small sets of data, sort each set, request temporary space from the disk, and write the intermediate results in the sorted order into the temporary segment, sort the other sets. After sorting all the sets, merge them to obtain the final result. Therefore, sorting operations are expensive. When a dataset is large, you need to avoid sorting operations as much as possible.
To avoid sorting, you need to create an appropriate index to index fields that are frequently sorted and joined. After an index is created, when the server sends a sorting request to these fields, the index is directly referenced without sorting.
2. Use the query conditions reasonably
(1) Avoid using negative Operators
In the query statement, we should avoid using negative operators such as <> or not, because these operators will cause sequential reading of the table. The sequential reading of tables is the cause of a sharp decline in system performance. When non-search parameters are used in expressions, the optimizer ignores the corresponding query statements or cannot optimize them, as a result, the table is read sequentially.
(2) Avoid using in statements
When the query condition contains the in keyword, the optimizer uses the or parallel condition. The database management system queries each or clause, merges all results, and removes duplicate items as the final result. When you can use in or exists statements, consider the following principles: exists is far more efficient than in. Similarly, the use of notexists to replace the not in statement queries adds restrictions, so as to reduce the number of full table scans, so as to speed up the query to improve the database operation efficiency.
(3) Avoid Calculation of index fields
The Calculation of index fields damages the indexing function. For the convenience of writing, ignoring the performance cost is huge. For example:
Select count (C1) FROM TBL_1 where substr (C1, 0.219) = '01' -- time consumed: 01% s select count (C1) FROM TBL_1 WHERE C1 LIKE '000000' -- time consumed: 0.015 S
Tomorrow's continuation ......