Home  >  Article  >  Database  >  How to optimize oracle query?

How to optimize oracle query?

coldplay.xixi
coldplay.xixiOriginal
2020-07-17 15:43:123817browse

Oracle query optimization method: 1. UNION operator, which sorts the result set generated after table linking, deletes duplicate records and returns the results; 2. General situation of greater than or less than operator There is no need to adjust the following, because it will use index search because it has an index, but in some cases it can be optimized.

How to optimize oracle query?

Oracle query optimization method:

1. IN operator

The advantage of SQL written in IN is that it is easier to write and clear and understandable, which is more suitable for the style of modern software development.

However, the performance of SQL using IN is always relatively low. From the steps executed by ORACLE, there are the following differences between SQL using IN and SQL without IN:

ORACLE attempts to convert it into a connection of multiple tables. If the conversion is unsuccessful, the subquery in IN will be executed first, and then the outer table records will be queried. If the conversion is successful, the connection of multiple tables will be directly used. method query. It can be seen that SQL using IN has at least one more conversion process. General SQL can be converted successfully, but SQL containing group statistics and other aspects cannot be converted.

Related learning recommendations: oracle database learning tutorial

2. NOT IN operator

This operation is highly recommended Not used because it cannot apply table indexes.

Recommended solution: Use NOT EXISTS or (the outer connection is judged to be empty) solution instead of

3, a8093152e673feb7aba1828c43532094 operation Operator (not equal to)

The not equal to operator will never use the index, so its processing will only produce a full table scan.

Recommended solution: Use other operations with the same function instead, such as

aa8093152e673feb7aba1828c435320940 is changed to a>0 or a< 0

aa8093152e673feb7aba1828c43532094'' is changed to a>''

4, > and 01271c9ded37c84a23c4385661a95af02 and A>=3, because when A>2, ORACLE will first find the record index of 2 and then compare it, while when A>=3, ORACLE will directly find = 3 record index.

5. IS NULL or IS NOT NULL operation (determining whether the field is empty)

Generally, judging whether the field is empty will not be applied. Indexed, because B-tree indexes do not index null values.

Recommended solution:

Replace with other operations with the same function, such as

a is not null is changed to a>0 or a>'', etc.

The field is not allowed to be empty, and a default value is used to replace the empty value. For example, the status field in the industry expansion application is not allowed to be empty, and the default is application.

Create a bitmap index (partitioned tables cannot be built. Bitmap indexes are difficult to control. If the index has too many field values, the performance will decrease. Multi-person update operations will increase the number of data blocks. Locking phenomenon)

6. UNION operator

UNION will filter out duplicate records after table linking, so when linking tables The generated result set will then be sorted, duplicate records will be deleted and the results will be returned. In most actual applications, duplicate records will not be generated. The most common ones are UNION between the process table and the history table. For example:

select * from gc_dfys union select * fromls_jg_dfys

This SQL first takes out the results of the two tables when running, then uses the sorting space to sort and delete duplicate records, and finally returns the result set. If the table data is large, May cause sorting with disk.

Recommended solution: Use the UNION ALL operator instead of UNION, because the UNION ALL operation simply returns the two results after merging them.

7. The order of conditions after the WHERE clause will have a direct impact on the query of the large data scale.

Impact, such as

Select * from zl_yhjbqk where dy_dj =‘1KV以下‘ and xh_bz=1
 
Select * from zl_yhjbqk where xh_bz=1 and dy_dj =‘1KV以下‘

In the above two SQLs, the two fields dy_dj (voltage level) and xh_bz (cancellation flag) are not indexed, so the entire table is scanned during execution. , the ratio of dy_dj = 'below 1KV' condition in the first SQL in the record set is 99%, while the ratio of xh_bz=1 is only 0.5%. When the first SQL is executed, 99% of the records are dy_dj and xh_bz When comparing the second SQL, 0.5% of the records are compared with dy_dj and xh_bz. From this, it can be concluded that the CPU usage of the second SQL is significantly lower than that of the first.

8. Tips on goals:

  • ##COST (Optimize by cost)

  • RULE (Optimize by rules)

  • CHOOSE (Default) (ORACLE automatically selects costs or rules for optimization)

  • ALL_ROWS (所有的行尽快返回)

  • FIRST_ROWS (第一行数据尽快返回)

 

9、执行方法的提示:

  • USE_NL (使用 NESTED LOOPS 方式联合)

  • USE_MERGE (使用 MERGE JOIN 方式联合)

  • USE_HASH (使用 HASH JOIN 方式联合)

 

10、索引提示:

INDEX ( TABLE INDEX)(使用提示的表索引进行查询)

 

11、其它高级提示(如并行处理等等)

ORACLE 的提示功能是比较强的功能,也是比较复杂的应用,并且提示只是给ORACLE执行的一个建议,有时如果出于成本方面的考虑 ORACLE也可能不会按提示进行。根据实践应用,一般不建议开发人员应用ORACLE提示,因为各个数据库及服务器性能情况不一样,很可能一个地方性能提升了,但另一个地方却下降了,ORACLE 在 SQL执行分析方面已经比较成熟,如果分析执行的路径不对首先应在数据库结构(主要是索引)、服务器当前性能(共享内存、磁盘文件碎片)、数据库对象(表、索引)统计信息是否正确这几方面分析。

 

12、IN和EXISTS

有时候会将一列和一系列值相比较。最简单的办法就是在where子句中使用子查询。在where子句中可以使用两种格式的子查询。

 

第一种格式是使用IN操作符:

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

 

第二种格式是使用EXIST操作符:

... where exists (select &#39;X&#39; from ...where...);

 

我相信绝大多数人会使用第一种格式,因为它比较容易编写,而实际上第二种格式要远比第一种格式的效率高。在Oracle中可以几乎将所有的IN操作符子查询改写为使用EXISTS的子查询。

 

第二种格式中,子查询以'select 'X'开始。运用EXISTS子句不管子查询从表中抽取什么数据它只查看where子句。这样优化器就不必遍历整个表而仅根据索引就可完成工作(这里假定在where语句中使用的列存在索引)。相对于IN子句来说,EXISTS使用相连子查询,构造起来要比IN子查询困难一些。

 

通过使用EXIST,Oracle系统会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle系统在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。

 

同时应尽可能使用NOT EXISTS来代替NOT IN,尽管二者都使用了NOT(不能使用索引而降低速度),NOT EXISTS要比NOT IN查询效率更高。

 

任何在where子句中使用is null或is notnull的语句优化器是不允许使用索引的。

 

13、order by语句

ORDER BY语句决定了Oracle如何将返回的查询结果排序。Orderby语句对要排序的列没有什么特别的限制,也可以将函数加入列中(象联接或者附加等)。任何在Orderby语句的非索引项或者有计算表达式都将降低查询速度。

仔细检查orderby语句以找出非索引项或者表达式,它们会降低性能。解决这个问题的办法就是重写orderby语句以使用索引,也可以为所使用的列建立另外一个索引,同时应绝对避免在orderby子句中使用表达式。

14、NOT

我们在查询时经常在where子句使用一些逻辑表达式,如大于、小于、等于以及不等于等等,也可以使用and(与)、or(或)以及not(非)。NOT可用来对任何逻辑运算符号取反。下面是一个NOT子句的例子:

... where not (status =&#39;VALID&#39;)

如果要使用NOT,则应在取反的短语前面加上括号,并在短语前面加上NOT运算符。NOT运算符包含在另外一个逻辑运算符中,这就是不等于(a8093152e673feb7aba1828c43532094)运算符。换句话说,即使不在查询where子句中显式地加入NOT词,NOT仍在运算符中,见下例:

... where status <>&#39;INVALID&#39;;

再看下面这个例子:

select * from employee where salary<>3000;

对这个查询,可以改写为不使用NOT:

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

虽然这两种查询的结果一样,但是第二种查询方案会比第一种查询方案更快些。第二种查询允许Oracle对salary列使用索引,而第一种查询则不能使用索引。

全表扫描就是顺序地访问表中每条记录.ORACLE采用一次读入多个数据块(databaseblock)的方式优化全表扫描。

15、使用DECODE函数来减少处理时间

使用DECODE函数可以避免重复扫描相同记录或重复连接相同的表。例如:

SELECT COUNT(*),SUM(SAL)
FROM EMP
WHERE DEPT_NO = 0020
AND ENAME LIKE ‘SMITH%’;

你可以用DECODE函数高效地得到相同结果.

SELECT COUNT(DECODE(DEPT_NO,0020,’X’,NULL)) D0020_COUNT,
COUNT(DECODE(DEPT_NO,0030,’X’,NULL)) D0030_COUNT,
SUM(DECODE(DEPT_NO,0020,SAL,NULL)) D0020_SAL,
SUM(DECODE(DEPT_NO,0030,SAL,NULL)) D0030_SAL
FROM EMP WHERE ENAME LIKE ‘SMITH%’;

类似的,DECODE函数也可以运用于GROUP BY 和ORDER BY子句中.

16、用Where子句替换HAVING子句

避免使用HAVING子句, HAVING只会在检索出所有记录之后才对结果集进行过滤.这个处理需要排序,总计等操作.如果能通过WHERE子句限制记录的数目,那就能减少这方面的开销.例如:

  低效:

SELECT REGION,AVG(LOG_SIZE)
FROM LOCATION
GROUP BY REGION
HAVING REGION REGION != ‘SYDNEY’
AND REGION != ‘PERTH’

  高效:

SELECT REGION,AVG(LOG_SIZE)
FROM LOCATION
WHERE REGION REGION != ‘SYDNEY’
AND REGION != ‘PERTH’
GROUP BY REGION

17、减少对表的查询

在含有子查询的SQL语句中,要特别注意减少对表的查询.例如:

  低效:

SELECT TAB_NAME
FROM TABLES
WHERE TAB_NAME = ( SELECT TAB_NAME
FROM TAB_COLUMNS
WHERE VERSION = 604)
AND DB_VER= ( SELECT DB_VER
FROM TAB_COLUMNS
WHERE VERSION = 604)

  高效:

SELECT TAB_NAME
FROM TABLES
WHERE (TAB_NAME,DB_VER)
= ( SELECT TAB_NAME,DB_VER)
FROM TAB_COLUMNS
WHERE VERSION = 604)
Update 多个Column 例子:

  低效:

UPDATE EMP
SET EMP_CAT = (SELECT MAX(CATEGORY) FROM EMP_CATEGORIES),
SAL_RANGE = (SELECT MAX(SAL_RANGE) FROM EMP_CATEGORIES)
WHERE EMP_DEPT = 0020;

  高效:

UPDATE EMP
SET (EMP_CAT, SAL_RANGE)
= (SELECT MAX(CATEGORY) , MAX(SAL_RANGE)
FROM EMP_CATEGORIES)
WHERE EMP_DEPT = 0020;

18、通过内部函数提高SQL效率.

SELECT H.EMPNO,E.ENAME,H.HIST_TYPE,T.TYPE_DESC,COUNT(*)
FROM HISTORY_TYPE T,EMP E,EMP_HISTORY H
WHERE H.EMPNO = E.EMPNO
AND H.HIST_TYPE = T.HIST_TYPE
GROUP BY H.EMPNO,E.ENAME,H.HIST_TYPE,T.TYPE_DESC;

通过调用下面的函数可以提高效率.

FUNCTION LOOKUP_HIST_TYPE(TYP IN NUMBER) RETURN VARCHAR2
AS
TDESC VARCHAR2(30);
CURSOR C1 IS
SELECT TYPE_DESC
FROM HISTORY_TYPE
WHERE HIST_TYPE = TYP;
BEGIN
OPEN C1;
FETCH C1 INTO TDESC;
CLOSE C1;
RETURN (NVL(TDESC,’?’));
END;
FUNCTION LOOKUP_EMP(EMP IN NUMBER) RETURN VARCHAR2
AS
ENAME VARCHAR2(30);
CURSOR C1 IS
SELECT ENAME
FROM EMP
WHERE EMPNO=EMP;
BEGIN
OPEN C1;
FETCH C1 INTO ENAME;
CLOSE C1;
RETURN (NVL(ENAME,’?’));
END;
SELECT H.EMPNO,LOOKUP_EMP(H.EMPNO),
H.HIST_TYPE,LOOKUP_HIST_TYPE(H.HIST_TYPE),COUNT(*)
FROM EMP_HISTORY H
GROUP BY H.EMPNO , H.HIST_TYPE;

The above is the detailed content of How to optimize oracle query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn