sql statement Oracle database performance optimization

  In the early application development, database development because the data is relatively small, the query sql statement, the complexity of trying to write and so can not see the performance advantages and disadvantages of various written sql statement, but if the application submitted to the practical application of the system, along with the database increase the data, the response speed of the system has become one of the major problems of the current system need to be addressed. System optimization is a very important aspect is to optimize the sql statement. For the vast amounts of data, the speed difference between inferior and superior sql statement sql statement can reach a hundred times, visible for a system is not simply to achieve its function on the line, but to write high-quality sql statement, improve system availability.

  Oracle's sql tuning of a complex subject, and even need a lengthy introduction to introduce OracleSQL tuning nuances. However, there are some basic rules that every OracleDBA need to comply with these rules can improve their system performance.

  sql tuning goal is simple: Eliminate unnecessary large table full table search. Unnecessary search the entire table lead to a lot of unnecessary disk I / O, thus slowing down the performance of the entire database, search for unnecessary full table, the most common tuning is to increase the index, in the table can be added to standard B-tree index, an index may be added to the bitmap and function-based indexes. To decide whether to eliminate a full table search, you can double-check the cost of index search I / O overhead and the whole table search, read their overhead and data blocks and the possible parallel implementation of relevant and ready to compare the two.

  In addition, when the whole table search is one of the fastest access method, the small table full table search into the cache (memory), is also a very wise choice. We now find that the birth of a lot of memory-based database management system, the entire database will be placed in memory, performance will be a qualitative leap.

First, the index-related performance optimization

  In most cases, Oracle use the index to quickly traverse the table, the optimizer mainly based on an index defined to improve performance. However, if written in the where clause sql statement sql code is unreasonable, it will cause the optimizer use the index by deleting the full table scan, usually this is the so-called inferior sql statement sql statement. When writing sql statement we should be clear optimizer to remove the index based on what principles, which helps to write high-performance sql statement.

1.IS NULL 与 IS NOT NULL

  Do not use indexed null any column that contains null values ​​will not be included in the index, the index multiple columns even such a case, there is a column that contains null, the column will be excluded from the index. That there is a column a null value, even if the index of the column will not improve performance. Any use in the where clause is null or is nou null statement optimizer is not allowed to use the index.

2. Join column

  For the connection of the column, even if the connection as a static value, the optimizer will not use the index. Look at an example, suppose an employee table (employee), for a worker's first and last name into two storage (FIRST_NAME and LAST_NAME), now want to query the workers called Beill Cliton.

  The following is a join query using sql statement:

select * from employee where first_name ||''|| last_name = 'Beill Cliton';

  Above this statement can check out whether there is Beill Cliton the employee, but to note here, do not use the system optimizer to create an index based on LAST_NAME, when using the following sql statement written this, Oracle system can create based LAST_NAME index of:

select * from employee where first_name = 'Beill' and last_name = 'Cliton';

3. With wildcard (%) of the like statement

  Also take the above example, the current demand is such, require searchers name contains Cliton of workers in the table, you can use the following query sql statement:

select * from employee where last_name like '%Cliton%';

  Because here the wildcard (%) in the search word first appeared, so Oracle system does not use last_name index. In many cases may not be able to avoid this situation, but we must mind with bottom, wildcard queries such use will reduce the speed. However, when the wildcard in a position other string, the optimizer can use an index, the index in the following query is obtained using:

select * from employee where last_name like 'C%';

  The statement to query all names beginning with C, which fully meet the requirements of the index, because the index itself is a sort of column.

4.ORDER BY clause

  ORDER BY clause determines how Oracle will return to the sort query results. This clause no particular limitation on the column to be sorted, the function also may be added to the column (such as an additional coupling or the like). Any queries will reduce the rate of non-indexed items clause or have calculated expression.

  Double-check to find order by sub-statement or expression of non-indexed items, they can degrade performance. The solution to this problem is to rewrite the order by clause to use an index, the index can also create another column used, and should definitely be avoided in order by clause to use an expression.

5.NOT keyword

  We often used when some logic in the where clause query expressions, such as greater than, less than, equal to and not equal, etc., and may also be used (and), or (or) and not (non). NOT can be used for any arithmetic logic negated. The following is an example of a NOT clause:

... where not(status = 'VALID');

  To use NOT, should be negated phrases in parentheses, and NOT operator in front of the phrase. NOT operator is included in another logical operator, which is not equal to (<>) operator. In other words, even if not added to the query where clause NOT keyword, NOT operators are still in, see the example below:

... where status <> 'VALID';

  Look at the following example:

select * from employee where salary <> 3000;

  This query can be rewritten to not use NOT:

select * from employee where salary < 3000 or salary > 3000;

  Although the results of both queries the same, but the second inquiry program will be faster than the first query plan more. Oralce second query to allow for the use of the salary column index, and the first query can not use the index.

6.IN 和 EXISTS

  Sometimes a series will be one and compared to the value, the easiest way is to use a query in the where clause child. You can use two formats subquery in the where clause.

  The first format is to use the IN operator:

... where column in (select * from ... where ...);

  The second format is to use EXISTS operator:

... where exists (select 'X' from ... where ...);

  I believe most people will use the first format, because it is relatively easy to write, but actually the second format is far more efficient format first. Oracle can be in almost all of the IN operator subquery rewrite sub-queries using EXISTS.

  The second format, the sub-query to "select 'X'" began, the use of EXISTS clause, no matter what sub-queries to extract data from the table, only to see the where clause, so the optimizer does not have to traverse the entire table but only in accordance with the index can complete its work (assuming that the column used in the where clause in an index exists here). Relative to the IN clause is, EXISTS subquery using the linked construct it more difficult than the IN subquery.

  By using EXISTS, Oracle system will first check the main query, sub query and then run until it finds the first match, which saves time. Oralce system in the implementation of IN subquery, the first implementation of sub-queries, and a list of results obtained in a temporary increase of the index table. Before the implementation of sub-queries, the system hangs main query first, to be sub-query execution is completed, stored in a temporary table in the main query at a later time. This is the reason for using EXISTS IN faster than usual query speed.

  At the same time should be used as much as possible to replace NOT EXISTS NOT IN, although both use the NOT (can not use the index and reduce the speed), NOT EXISTS higher than NOT IN query efficiency.

7 <> Not equal symbol

  Is not equal operator is never used in the index, since it will only process a full table scan.

  Recommended Program: operator replaced with other operators of the same functions, such as

  a <> 0 to a> 0 or a <0, a <> '' to a> ''

8. avoid the use of columns in the index calculation

  WHERE clause, if the index column is a part of the function, the optimizer will not use the full table scan using the index

  Inefficient:

select ... from dept where SAL * 12 > 25000;

  Efficient:

select ... from dept where SAL > 25000/12;

9. Always use the index of the first column

  If the index is based on multiple columns, only the where clause refers in its first column (leading column), the optimizer will choose to use the index. This is a simple but important rules when referencing only the second column of the index, the optimizer uses a full table scan while ignoring the index.

10. The index of the column to avoid changing the type

  When comparing the different data types, Oralce automatically column simple type conversion.

  Suppose empno is a numeric index of the column type:

select ... from emp where empno = '123';

  In fact, after Oracle type conversion, the statement into:

select ... from emp where empno = TO_NUMBER('123');

  Fortunately, the conversion does not occur in the type of the index column, the use of the index is not changed.

  Now, suppose emp_type is a character type of index columns:

select ... from emp where emp_type = 123;

  This statement is converted to Oracle:

select ... from emp where TO_NUMBER(emp_type) = 123;

  Because the internal type conversion occurs, the index will not be used. In order to avoid Oracle sql implicit type conversions, the best type of display for converting manifested. Note that when the character and numeric comparison, Oracle will give priority to convert numeric type to a character type.

11. WHERE clause need to be careful

  Some select statement where clause does not use an index:

  • '! =' Will not use an index, the index can only tell you what exists in the table, but can not tell you what does not exist in the table
  • '||' character is connected to function just like any other function as disabled index
  • '+' Is a mathematical function, like other mathematical functions as disabled index
  • The same index column can not be compared with each other, this will start a full table scan

12. Some other rules

  • If the retrieved data amount more than 30% of the number of records in the table, the index will not significantly improve the efficiency of
  • Under certain circumstances, using an index scan might be slower than the overall, but this is the difference on the same order of magnitude. And under normal circumstances, use the index than the full table scan several times and even thousands of times faster
  • Avoid the use of IS NULL and IS NOT NULL index columns
  • Avoid using NOT in the index column
  • Alternatively IN with EXISTS, NOT EXISTS with alternative NOT IN
  • Improving efficiency through internal function sql
  • Select the most efficient sequence table name: Oracle parser in the order processing from right to left the table name in the from clause, the from clause written in the final table (base table) will be processed first, the child from the case of the sentence that contains multiple tables, you must select the least number of records as a basis for the table table. If more than three tables join query, it would need to select the cross-table as a base table, the table refers to a table is cross-referenced by other tables
  • WHERE clause join order: Oracle bottom-up parsing sequence where clause, according to this principle, the connection between the tables must be written before any other conditions where, conditions which can filter out the maximum number of records to be written the end of the where clause

Second, and related to the optimization of memory

1.UNION operator

  UNION making table will filter out duplicate links records, so the result set in the table will link the resulting sort operation to remove duplicate records and then returns the result. Most practical applications are no duplicate records. The most common is the process table and the history table UNION. Such as:

select * from A union select * from B;

  The sql at runtime to remove the results of the two tables, and then sort space to sort remove duplicate records, and returns the result set, if a large amount of data, then the table may cause sorted by disk.

  Recommended Program: The alternative operator UNION ALL UNION, UNION ALL operation as simply merge the two results returned after:

select * from A union all select * from B;

The impact 2.SQL writing

  The effects of different sql wording of the same features the same performance

  For example, in a sql A programmers to write:

select * from employee;

  B programmers to write:

SELECT  *  from scott.employee; (prefix table with owner)

  C programmers to write:

the SELECT  *  from the EMPLOYEE; (uppercase table name)

  D programmers to write:

SELECT  *    from Employee; (more intermediate spaces)

  More than four in Oracle sql result and execution time analysis generated after finishing the same, but from the principle of shared memory Oracle SGA can be drawn for each Oracle sql will be an analysis and take up shared memory, if the string sql written exactly the same format and then analyze Oracle only once, shared memory will only leave once the results of the analysis, which not only reduces the time of analysis sql, but also reduce the shared memory duplicate information, oracle statistics can be accurately perform frequency of sql.

3. avoid sorts on disk

  When combined with Oracle to establish a session, you will be assigned a private sort area in memory for the session. If the connection is a dedicated connection (dedicated connection), it will assign a Program Global Area (PGA) in the memory according to the size of the init.ora sort_area_size parameters. If the connection is established through multi-threaded server, then the sort of space is allocated in large_pool. Unfortunately, for all of the session, the amount of memory used to sort must be the same, we can not allocate additional sorting operation requires a larger area for the sort. Therefore, the designer must make a balance, disk sort (disksorts) appear in the allocation of sufficient sort area to avoid large sorting tasks occurring simultaneously, for tasks that do not require a great sort of, there will be some waste . Of course, when the space requirements sorted exceeds the size of sort_area_size, will be paged disk sort TEMP table space. Sort than the sort of memory disk about 14,000 times slower.

  We have already mentioned, the size of the private sort area is determined by the sort_area_size init.ora parameters. Each ordering determined by the size occupied sort_area_size init.ora parameters. When sorting can not be completed in the allotted space, it will use disk sort of way, that Oracle instance in the temporary table space.

  Disk sort of overhead is great, there are several reasons. First of all, and sort of memory compared to their particularly slow; and disk sort consumes resources in the temporary table space. Oracle must also allocate buffer pool block holding block temporary table space. No matter what time, are better than the sort of memory disk sorting, disk sorting will cause slow task, and the task will affect the execution of the current Oracle instance. Also, excessive disk sorting will cause freebufferwaits particularly high values, so that other tasks of the data block removed from the buffer.

4. Avoid the use of resource-intensive operations

  With DISTINCT, UNION, MINUS, INTERSECT, ORDER BY sql statement sql engine back to start the implementation of resource-intensive sorting (SORT) function. DISTINCT requires a sort operation, while others need to be performed at least twice sort. Typically, sql statement with UNION, MINUS, INTERSECT are can be rewritten in other manners. If your database deployment sort_area_size good use UNION, MINUS, INTERSECT can be considered, after all, they are very readable.

3. Other relevant performance optimization tips

1. Delete duplicate records

  The most efficient method to delete duplicate records (because of the use of ROWID) example:

delete from emp E where E.ROWID > (select MIN(X.ROWID) from emp X where X.emp_no = E.emp_no);

2. Alternatively DELETE with TRUNCATE

  When deleting records in the table, under normal circumstances, rollback (rollback segments) used to store the information may be recovered. If you do not COMMIT transaction, Oracle will return to the state before the data is deleted (precisely to restore to the state before the deletion), and when using TRUNCATE, rollback no longer store any information that can be restored. When the command is run, the data can not be restored, so few resources is called, the execution time will be very short (TRUNCATE to empty the whole table only applies, TRUNCATE is DDL rather than DML).

* Avoid using clause in 3.SELECT

  Oracle in the process of resolution, the * will in turn be converted into all the column names, this work is done by querying the data dictionary, which means that the more time-consuming.

4. Alternatively WHERE clause HAVING clause

  Avoid using the having clause, HAVING result set will only filter fishes After retrieving all records, the process requires sorting, the total operation. If you can limit the number of records where clause, it will be able to reduce this overhead. sql statement on, where, having these three conditions can be added to the clause, is the first on the implementation, where second place, having finally, because on that first record does not meet the conditions of statistics after filtration, it intermediate computation can reduce the data to be processed, and normally should be the fastest, WHERE should quickly than having, as filter data, sum after it, when employed on the two tables, one table so when you compare with having a rest where. In the case of single-table query statistics, if you want to filter conditions are not related to the field you want to calculate, and that their results are the same, but where you can use rushmore technology, but having it can not, on the latter slower speed if you want to fields related to computing, says that before did not calculate the value of this field is uncertain, where the role of time before the calculation is complete, and having calculated that after only works, so in this case under the results of the two will be different. When multi-table join query, function ON earlier than where, according to the first system connection condition between the respective table, the tables after the synthesis a plurality of temporary table, and then filtered by the where, then calculate, and then the calculations for having filter. Thus, in order to play a proper role in the filter conditions, we must first understand that this condition should be at what time to work, and then decide where to put.

5. alias table (the Alias)

  When connecting a plurality of tables in the sql statement, the alias table and use the alias prefix on each column. As a result, it can reduce the time to resolve and reduce those grammatical errors caused by column ambiguity.

6. Alternatively IN with EXISTS, NOT EXISTS with alternative NOT IN

  In many queries based on the underlying table in order to satisfy a condition, often you need to join another table. In this case, use EXISTS (or NOT EXISTS) usually will improve the efficiency of the query. In sub-query NOT IN clause will perform an internal sort and merge. In either case, NOT IN is the least efficient (because of its sub-tables in the query to perform a full table traverse). To avoid using NOT IN, we can rewrite it into the outer coupling (OUTER JOIN) or NOT EXISTS.

  Efficient:

select * from emp where empno > 0 and EXISTS (select 'X' from dept where dept.deptno = emp.deptno and loc = 'MELB');

  Inefficient:

select * from emp where empno > 0 and deptno IN (select deptno from dept where loc = 'MELB');

7. Replace with EXISTS DISTINCT

  When submitting a multi-table information (such as department table and an employee table) query statement, avoid the use of distinct in the select clause. Generally consider replaced with EXISTS, EXISTS make queries more quickly, because the RDBMS kernel module after a subquery conditions but meet immediately return a result.

  Efficient:

select dept_no, dept_name from dept D where EXISTS (select 'X' from emp E where E.deptno = D.deptno);

  Inefficient:

select DESTINCT dept_no, dept_name from dept D, emp E where D.deptno = E.deptno;

8.SQL statement uses uppercase

  Because Oracle is always the first to resolve the sql statement, the lowercase letters to uppercase re-execution

9. Use> = alternate>

  Efficient:

select * from emp where deptno >= 4;

  Inefficient:

select * from emp where deptno > 3;

  The difference is that the former DBMS directly jump to the first record equal deptno 4, which is positioned to the first recording deptno = 3 forward scan and to record the first deptno greater than 3.

10. GROUP BY Optimization

  Improve the efficiency of group by statement, you can filter out unwanted records before the group by. The following two queries return the same result, but the second obviously a lot faster.

  Inefficient:

select job, AVG(SAL) from emp group by job having job = 'PRESIDENT' or job = 'MANAGER';

  Efficient:

select job, AVG(SAL) from emp where job = 'PRESIDENT' or job = 'MANAGER' group by job;

 

Guess you like

Origin www.cnblogs.com/xiaogongjin/p/11828283.html