Oracle Query Optimization 1

Steps of Oracle query optimization
SQL execution
 Analysis: security check, syntax check;
 Create: evaluate multiple execution plans, and select an optimal execution plan;
 Execute: bind variables, execute the execution plan that has been created;
 Get : Get the result set, convert, sort, etc. Common
operations that may lead to full table scans '%x';  Queries with not equals conditions: <> , !=, not in, etc.  Built-in functions invalidate indexes: substr(), to_char(), etc.;  Use all_rows hints;  Use parallel hints; SQL Adjustment goals  Remove unnecessary full table scans of large tables;  Cache full table scans of small tables;  Try to use host variables instead of direct variables to reduce the parsing time of SQL statements;  Optimize the use of indexes;  Optimize table join methods;  Optimize sub-queries; adjust SQL sub-queries  Standard sub-queries: in and exists;  Anti-join sub-queries: not in and not exists;

















 Associated subquery: Refers to referencing an external data table within a subquery; …from table1 a where … (select…from table2 b Where a.f1 = b.f1…); for each record in the external data set, the Execute the inner subquery once;
 Non-associative subquery: refers to the data table outside the subquery will not be referenced; …from table1 a where … (select …from table2 b …); the inner subquery is executed only once;
 Adjust the SQL Subqueries
 Avoid using subqueries as much as possible, and use standard join operations instead, so that hints can be used to change the execution plan;
 Consider the validity of subqueries first, and then consider rewriting them;
 Use a When a correlated subquery is used, the execution plan of the subquery in and exists clause is basically the same;
 When the outer query returns relatively few records, the correlated subquery executes faster than the non-correlated subquery;
adjust the SQL subquery
 In When the inner subquery has only a few records, the non-correlated subquery executes faster than the correlated subquery;
 It is redundant to use the in clause for the correlated subquery; it is inappropriate to use the exists clause for the noncorrelated subquery.
 Non-associative subqueries using the in clause can be converted to standard join operations and select distinct to remove duplicate records;
 Associated subqueries that use the exists clause can be converted to standard joins, but it is best to return only one subquery  The
non-correlated subquery using the not in clause can be converted to the sql minus clause, and the performance will be relatively higher;
 The correlated subquery using the not in clause can be rewritten using the outer join operation with the select distinct clause ;
ORACLE Tips
 http://ymy131931.iteye.com/blog/1998888
Common optimization examples
 select num from a where num in (select num from b where b.num/2 > 100)
union
select num from c where num = 10 or num = 20 or num =30;
 MERGE:
 MERGE INTO TABLE_NAME A
 USING TABLE_NAME B
 ON (A.XXX = B.XXX) /*Association conditions of the two tables*/
 WHEN MATCHED THEN
 UPDATE SET A. XXX=…… (WHERE ……)
 DELETE WHERE ……
 WHEN NOT MATCHED THEN
 INSERT ……;
 If UPDATE is followed by DELETE, there must be no WHERE statement after UPDATE, otherwise DELETE will be invalid
 MERGE
 CREATE TABLE TEAT_A ( NUM NUMBER(3),A_ID VARCHAR2 (6));
 CREATE TABLE TEAT_B (NUM NUMBER(3),B_ID VARCHAR2 (6));
 INSERT INTO TEST_A VALUES (1,'001');
 INSERT INTO TEST_A VALUES (2,'001');
 INSERT INTO TEST_A VALUES (3,'001');
 INSERT INTO TEST_A VALUES (3,'002');
 INSERT INTO TEST_A VALUES (4,'001');
 INSERT INTO TEST_A VALUES (5,'002');
 MERGE
 SQL> MERGE INTO TEST_A A
   2  USING (SELECT NUM,B_ID FROM TEST_B) B
   3  ON (A.A_ID = B.B_ID)
   4  WHEN MATCHED THEN
   5  UPDATE SET A.NUM = B.NUM
   6  DELETE WHERE B.B_ID = '002'
   7  WHEN NOT MATCHED THEN
   8  INSERT VALUES
   9  (B.NUM,B.B_ID);
行列转换——列转行
 OARCLE 用户WMSYS下的WM_CONCAT();
例:SELECT WMSYS.WM_CONCAT(NUM) FROM TEST_A;
 CASE WHEN END
例: SELECT JOB ,
  SUM(CASE DEPTNO WHEN 10 THEN SAL END) SAL_10,
  SUM(CASE DEPTNO WHEN 20 THEN SAL END) SAL_20,
  SUM(CASE DEPTNO WHEN 30 THEN SAL END) SAL_30,
  SUM(SAL) HJ
  FROM EMP
  GROUP BY JOB;
 select * from emp pivot(sum(sal) as sum_sal for(DEPTNO) in (10,20,30));
行列转换——列转行
 SELECT * FROM (SELECT SAL, DEPTNO FROM EMP) PIVOT(SUM(SAL) AS PI_SAL FOR DEPTNO IN('10','20','30'));
 SELECT "备件名" AS A,
LISTAGG("仓库名", '--') WITHIN GROUP(ORDER BY "备件名") AS B
FROM TEST_5
GROUP BY "备件名"
行列转行——行转列
SELECT *
FROM VIEW_1 UNPIVOT(A FOR B IN("'10'_PI_SAL",
"'20'_PI_SAL",
"'30'_PI_SAL"));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326524930&siteId=291194637