# Re: scalar subquery costing

From: Jonathan Lewis <jonathan_at_jlcomp.demon.co.uk>
Date: Tue, 28 May 2019 06:30:20 +0000

The "select with/without replacement" was just another guess about the formula. An argument for "select with replacement" being relevant somewhere is that you might need to consider the other end of the subquery where you have 1,000 distinct values, and you want to estimate how many of them might have been selected by the time 100,000 executions of the subquery have executed. (Because that's how many of them could have been cached).

Regards
Jonathan Lewis

From: Noveljic Nenad <nenad.noveljic_at_vontobel.com> Sent: 27 May 2019 22:00
To: Jonathan Lewis; ORACLE-L (oracle-l_at_freelists.org) Subject: RE: scalar subquery costing

Yes, you're right (as always). The cost indeed depends on the number of distinct values in t_100k.

The cost for two boundary cases are:
1. NDV=100k: 262K
2. NDV=1: 72 It should be "select without replacement", shouldn't it? That's because the t_100k values from the step 3 "are not returned into the urn" once they are matched with t_1k. But I'll do more tests and try to figure out which formula is exactly used.

Would you happen to know why the optimizer calculated 262K for NDV=100k? I mean there shouldn't be any caching there as the values don't repeat.

The tables are created as follows:

create table t_1k ( n1 integer ) ;

create table t_100k ( n1 integer ) ;

insert into t_1k
select level
from dual
connect by level <= 1000;

insert into t_100k
select level
from dual
connect by level <= 100000;

commit ;

begin
dbms_stats.gather_table_stats ( null, 'T_1K') ;   dbms_stats.gather_table_stats ( null, 'T_100K') ; end ;
/

Best regards,

Nenad

-----Original Message-----
From: Jonathan Lewis <jonathan_at_jlcomp.demon.co.uk> Sent: Montag, 27. Mai 2019 21:36
To: ORACLE-L (oracle-l_at_freelists.org) <oracle-l_at_freelists.org>; Noveljic Nenad <nenad.noveljic_at_vontobel.com> Subject: Re: scalar subquery costing

Just a quick initial guess - the optimizer may be trying to include an estimate of the number of times it will have to execute the query based on the number of distinct values in the t_100K table, and allowing for some caching benefit. Possibly using a "selection with/without replacement" formula for the caching effect.

Regards
Jonathan Lewis

From: oracle-l-bounce_at_freelists.org <oracle-l-bounce_at_freelists.org> on behalf of Noveljic Nenad <nenad.noveljic_at_vontobel.com> Sent: 24 May 2019 16:46:30
To: ORACLE-L (oracle-l_at_freelists.org) Subject: scalar subquery costing

The calculated cost for the following query is 262K (on Oracle 12.2):

select /*+ qb_name(QB_MAIN) */
(
select /*+ qb_name(QB_SUBQ) */ count(*)

```              from t_1k
where t_1k.n1 = t_100k.n1
```

)
from t_100k ;

Plan Table
```--------------------------------------+-----------------------------------+

| Id  | Operation           | Name    | Rows  | Bytes | Cost  | Time      |

--------------------------------------+-----------------------------------+

| 0   | SELECT STATEMENT    |         |       |       |  262K |           |
| 1   |  SORT AGGREGATE     |         |     1 |     4 |       |           |
| 2   |   TABLE ACCESS FULL | T_1K    |     1 |     4 |     3 |  00:00:01 |

```
| 3 | TABLE ACCESS FULL | T_100K | 98K | 488K | 69 | 00:00:01 |
```--------------------------------------+-----------------------------------+
```
Predicate Information:

2 - filter("T_1K"."N1"=:B1)

From the optimizer trace we can get more precise cardinalities and costs:

Final cost for query block QB_SUBQ (#0) - All Rows Plan:   Best join order: 1
Cost: 3.009811 Degree: 1 Card: 1.000000 Bytes: 4.000000

Final cost for query block QB_MAIN (#0) - All Rows Plan:   Best join order: 1
Cost: 268174.664510 Degree: 1 Card: 100000.000000 Bytes: 500000.000000

Table: T_100K Alias: T_100K
Card: Original: 100000.000000 Rounded: 100000 Computed: 100000.000000 Non Adjusted: 100000.000000 ...

Access Path: TableScan
Cost: 68.697001 Resp: 68.697001 Degree: 0       Cost_io: 68.000000 Cost_cpu: 16737631

In theory, the total cost should be caculated as follows. cardinality(QB_MAIN) * cost(QB_SUBQ) + cost(full table scan T_100K) = 100000 * 3.009811 + 68.69701 = 301049.79701

How the optimizer came up with 268174.664510?

Best regards,

Nenad

Please consider the environment before printing this e-mail. Bitte denken Sie an die Umwelt, bevor Sie dieses E-Mail drucken.

Important Notice

This message is intended only for the individual named. It may contain confidential or privileged information. If you are not the named addressee you should in particular not disseminate, distribute, modify or copy this e-mail. Please notify the sender immediately by e-mail, if you have received this message by mistake and delete it from your system. Without prejudice to any contractual agreements between you and us which shall prevail in any case, we take it as your authorization to correspond with you by e-mail if you send us messages by e-mail. However, we reserve the right not to execute orders and instructions transmitted by e-mail at any time and without further explanation. E-mail transmission may not be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete. Also processing of incoming e-mails cannot be guaranteed. All liability of Vontobel Holding Ltd. and any of its affiliates (hereinafter collectively referred to as "Vontobel Group") for any damages resulting from e-mail use is excluded. You are advised that urgent and time sensitive messages should not be sent by e-mail and if verification is required please request a printed version. Please note that all e-mail communications to and from the Vontobel Group are subject to electronic storage and review by Vontobel Group. Unless stated to the contrary and without prejudice to any contractual agreements between you and Vontobel Group which shall prevail in any case, e-mail-communication is for informational purposes only and is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. The legal basis for the processing of your personal data is the legitimate interest to develop a commercial relationship with you, as well as your consent to forward you commercial communications. You can exercise, at any time and under the terms established under current regulation, your rights. If you prefer not to receive any further communications, please contact your client relationship manager if you are a client of Vontobel Group or notify the sender. Please note for an exact reference to the affected group entity the corporate e-mail signature. For further information about data privacy at Vontobel Group please consult www.vontobel.com<https://www.vontobel.com>.

Important Notice

This message is intended only for the individual named. It may contain confidential or privileged information. If you are not the named addressee you should in particular not disseminate, distribute, modify or copy this e-mail. Please notify the sender immediately by e-mail, if you have received this message by mistake and delete it from your system.  Without prejudice to any contractual agreements between you and us which shall prevail in any case, we take it as your authorization to correspond with you by e-mail if you send us messages by e-mail. However, we reserve the right not to execute orders and instructions transmitted by e-mail at any time and without further explanation.  E-mail transmission may not be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete. Also processing of incoming e-mails cannot be guaranteed. All liability of Vontobel Holding Ltd. and any of its affiliates (hereinafter collectively referred to as "Vontobel Group") for any damages resulting from e-mail use is excluded. You are advised that urgent and time sensitive messages should not be sent by e-mail and if verification is required please request a printed version.  Please note that all e-mail communications to and from the Vontobel Group are subject to electronic storage and review by Vontobel Group. Unless stated to the contrary and without prejudice to any contractual agreements between you and Vontobel Group which shall prevail in any case, e-mail-communication is for informational purposes only and is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction.  The legal basis for the processing of your personal data is the legitimate interest to develop a commercial relationship with you, as well as your consent to forward you commercial communications. You can exercise, at any time and under the terms established under current regulation, your rights. If you prefer not to receive any further communications, please contact your client relationship manager if you are a client of Vontobel Group or notify the sender.  Please note for an exact reference to the affected group entity the corporate e-mail signature.  For further information about data privacy at Vontobel Group please consult www.vontobel.com.

```--
http://www.freelists.org/webpage/oracle-l
```
Received on Tue May 28 2019 - 08:30:20 CEST

Original text of this message