sql performance optimization (a)

First, the impact of the efficiency factor of
1, the large amount of data
2, retrieves Chinese
3, the system configuration database

Second, view the execution efficiency of the process
through a simple analysis of the implementation plan
1, to find the most resource-consuming point
2, to reduce the full table scan table_access
3, take the index

Third, the implementation of the steps sql
Here Insert Picture Description

/*
顺序为有1-6,6个大步骤,然后细分,5-1,5-2,5-3,由小变大顺序,1-J,1-A,1-P,1-U,为并行次序
*/
--查询组合字段
(5)select (5-2) distinct(5-3) top(<top_specification>)(5-1)<select_list>
--连表
(1)from (1-J)<left_table><join_type> join <right_table> on <on_predicate>
        (1-A)<left_table><apply_type> apply <right_table_expression> as <alias>
        (1-P)<left_table> pivot (<pivot_specification>) as <alias>
        (1-U)<left_table> unpivot (<unpivot_specification>) as <alias>
--查询条件
(2)where <where_pridicate>
--分组
(3)group by <group_by_specification>
--分组条件
(4)having<having_predicate>
--排序
(6)order by<order_by_list>

Fourth, the optimization strategy
1, drawn middle of the table, reducing the amount of data
2, advance assignments, there are some calculations and statistical work to do ahead of
3, try to avoid performing functions (any function) in conditions where the misplaced
4, Table related conditions take the index
5 , where the conditions to avoid the emergence of nested loops
6, further reduce the data amount conditional connection table

V. optimization measures
1, choose the least number of records as a table-driven table, from front to back is retrieved, so the minimum record on the front of the table
2, WHERE clause using the sequentially retrieved from the front, according to the principle, the connection between tables must be written before the other WHERE condition, that
can filter out the conditions for the maximum number of records must be written at the end of the WHERE clause, as well as in the linked list can be filtered should first be filtered
3, SELECT clause avoid the use '*'
4, replacing Where clause HAVING clause with
an alias 5, of table
6, with an alternative EXISTS iN, NOT EXISTS replace the iN the NOT
. 7, table connected Alternatively EXISTS
. 8, to avoid the index column calculated using as columns in the index calculation would result in an index failure
9, to avoid the use NOT in the index column (used not in the indexed columns causes index failure)
10, a UNION, iN Alternatively OR; UNION-ALL Alternatively the UNION
. 11, attention combination index, the index of the first column
12, and avoid the use of iS NULL iS NOT NULL in the index column
13, ORDER BY clause is used only in the two index stringent conditions.
14, to avoid changing the index column Type
15, avoiding the use of resource intensive operation (with DISTINCT, UNION, MINUS, INTERSECT, ORDER BY)

Case:
. 1, the IS and IS NOT NULL NULL
  can not be used as an index null any column that contains null values will not be included in the index. Even if the index multiple columns such a case, there is a column that contains null, the column will be excluded from the index. This means that if there is a column a null value, even if the construction of the index column will not improve performance.
  Any use where clause is null or is not null statement optimizer is not allowed to use the index.

'! =' 2, will not use the index in mind that the index can only tell you what exists in the table, but can not tell you what does not exist in the table.
Does not use index: select * from employee where salary < > 3000;
use index: select account_name from transaction where amount> 0;
using an index: select * from employee where salary < 3000 or salary> 3000;

3, the coupling column, '||' is connected to function as a character other functions as disabled index.
Without using an index: select account_name, amount from transaction where account_name || account_type = 'AMEXA';
Use Index: select account_name, amount from transaction where account_name = 'AMEX ' and account_type = 'A';

4, '+' is the mathematical function as other mathematical functions as disabled index.
Without using an index: select account_name, amount from transaction where amount + 3000> 5000;
Use Index: select account_name, amount from transaction where amount> 2000 ;

5, the same index column can not be compared with each other, which will enable a full table scan
without using an index: select account_name, amount from transaction where account_name = nvl (: acc_name, account_name);
Use Index: select account_name, amount from transaction where account_name like nvl (: acc_name, '%' );

6, like the statement with a wildcard (%) is
not using the index: select * from employee where last_name like '% cliton%';
use index: select * from employee where last_name like 'c%'

7, IN and EXISTS
without using an index: ... where column in (select * from ... where ...);
Use Index: ... where exists (select 'X ' from ... where ...);
at the same time as possible should be used to replace NOT EXISTS NOT IN , even though both use the nOT (can not use the index and reduce the speed), nOT EXISTS higher than nOT IN query efficiency.

Published 66 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/qq125281823/article/details/104447469