Oracle SQL Performance Optimization series (5)

Source: Internet
Author: User

7.Reduce the number of database accesses

When each SQL statement is executed, ORACLE performs a lot of internal work: parsing SQL statements, estimating index utilization, binding variables, and reading data blocks. it can be seen that reducing the number of visits to the database can actually reduce the workload of ORACLE.

For example,

There are three ways to retrieve employees with employee numbers equal to 0342 or 0291.

Method 1 (most inefficient)

SELECT EMP_NAME, SALARY, GRADE

FROM EMP

 WHERE emp_no. = 342;

SELECT EMP_NAME, SALARY, GRADE

 FROM EMP

WHERE emp_no. = 291;

Method 2 (low efficiency)

DECLARE

CURSOR C1 (E_NO NUMBER) IS

SELECT EMP_NAME, SALARY, GRADE

FROM EMP

WHERE EMP_NO = E_NO;

 

BEGIN

OPEN C1 (342 );

 FETCH C1 ...,..,.. ;

.....

OPEN C1 (291 );

FETCH C1 ...,..,.. ;

CLOSE C1;

END;

Method 3 (efficient)

Select a. EMP_NAME, A. SALARY, A. GRADE,

B. EMP_NAME, B. SALARY, B. GRADE

From emp a, EMP B

Where a. EMP_NO = 342

And B. EMP_NO = 291;

Note::

You can reset the ARRAYSIZE parameter in SQL * Plus, SQL * Forms, and Pro * C to increase the retrieval data volume for each database access. The recommended value is 200.

8.UseDECODEFunction to reduce processing time

You can use the DECODE function to avoid repeated scan of the same record or join the same table.

For example:

Select count (*), SUM (SAL)

FROM EMP

WHERE DEPT_NO = 0020

And ename like 'Smith % ';

Select count (*), SUM (SAL)

FROM EMP

WHERE DEPT_NO = 0030

And ename like 'Smith % ';

You can use the DECODE function to efficiently get the same result.

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 % ';

Similarly, the DECODE function can also be used in the group by and order by clauses.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.