[Oracle] efficient PL/SQL programming (2)-Standard Quantum Query

Source: Internet
Author: User

Scalar Query

Oracle allows a single-row subquery to be included in the select clause. Using scalar queries can effectively improve performance. When using external connections or using aggregate functions, we can consider the possibility of scalar quantum queries.

1. Cancel the use of external connections

External Connection practices:

Select a. username, count (*) from all_users A, all_objects B
Where a. Username = B. Owner (+)
Group by A. Username;

Practices for converting to scalar quantum queries:

Select a. username, (select count (*) from all_objects B where B. Owner = A. username) CNT
From all_users;

PS: the results of the two methods may be slightly different, mainly when all_objects does not meet the conditions, the external connection count (*) = 1, while the count (*) of the scalar quantum Query (*) result = 0

Select a. username, count (*), AVG (object_id) from all_users A, all_objects B
Where a. Username = B. Owner (+)
Group by A. Username;

2. Tips for using multiple Aggregate functions

When count (*)/AVG () occurs at the same time, it is not suitable to call two subqueries in the select clause. The performance will be affected. You can use the following two methods instead.

(1). Splice and split again

Select username, to_number (substr (data, 1, 10) CNT, to_number (substr (data, 11) AVG from
(
Select a. username, (select to_char (count (*), 'fm0000000009') | AVG (object_id) from all_objects B where B. Owner = A. username) data
From all_users
)

(2). Create an object type

Create or replace type mytype as object
(CNT number, AVG number );

Select username, A. Data. CNT, A. Data. AVG from
(
Select username, (select mytype (count (*), AVG (object_id) from all_objects B where B. Owner = A. username) data
From all_users
);

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.