Home » SQL & PL/SQL » SQL & PL/SQL » Use of All with equal (=) (Oracle 10gR1 and upper)
Use of All with equal (=) [message #600879] Wed, 13 November 2013 08:37 Go to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Hi,

I was thinking about use case, where I can use

select ... from ... where .. = all (..,..,..)

Do you have one in mind.

Thanks,
Manu
Re: Use of All with equal (=) [message #600882 is a reply to message #600879] Wed, 13 November 2013 08:52 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
manubatham20 wrote on Wed, 13 November 2013 06:37
Hi,

I was thinking about use case, where I can use

select ... from ... where .. = all (..,..,..)

Do you have one in mind.

Thanks,
Manu


how can a scalar equal more than 1 value?
Re: Use of All with equal (=) [message #600883 is a reply to message #600882] Wed, 13 November 2013 08:56 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Hmmm, I also think so, that why I posted here. Maybe someone else have any other idea, but not like in case if all the values are same.

Manu
Re: Use of All with equal (=) [message #600884 is a reply to message #600883] Wed, 13 November 2013 09:04 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
just because some syntax is "valid" & does not throw any error (WHERE ID = NULL), it does not mean should ever be used.
Re: Use of All with equal (=) [message #600885 is a reply to message #600879] Wed, 13 November 2013 09:04 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
manubatham20 wrote on Wed, 13 November 2013 09:37
Hi,

I was thinking about use case, where I can use

select ... from ... where .. = all (..,..,..)

Do you have one in mind.


Sure. Instead of:

select  dname,
        ename
  from  emp e,
        dept d
  where e.deptno = d.deptno
    and d.deptno = 10
/

DNAME          ENAME
-------------- ----------
ACCOUNTING     CLARK
ACCOUNTING     KING
ACCOUNTING     MILLER

SCOTT@orcl > 


You could use:

select  dname,
        ename
  from  emp e,
        dept d
  where 10 = all(d.deptno,e.deptno)
/

DNAME          ENAME
-------------- ----------
ACCOUNTING     CLARK
ACCOUNTING     KING
ACCOUNTING     MILLER

SCOTT@orcl > 


SY.
Re: Use of All with equal (=) [message #600890 is a reply to message #600885] Wed, 13 November 2013 09:49 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Solomon,

great example. Seriously, I never thought of this tricky way. Very Happy

I don't have an environment to test, can you please post explain plan as well.

Manu
Re: Use of All with equal (=) [message #600891 is a reply to message #600890] Wed, 13 November 2013 10:14 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
SCOTT@orcl > explain plan for
  2          select  dname,
  3                  ename
  4            from  emp e,
  5                  dept d
  6            where e.deptno = d.deptno
  7              and d.deptno = 10
  8  /

Explained.

SCOTT@orcl > select * from table(dbms_xplan.display)
  2  /

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------
Plan hash value: 568005898

----------------------------------------------------------------------------------------
| Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |         |     3 |    66 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |         |     3 |    66 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    13 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | EMP     |     3 |    27 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("D"."DEPTNO"=10)
   4 - filter("E"."DEPTNO"=10)

17 rows selected.

SCOTT@orcl > explain plan for
  2          select  dname,
  3          ename
  4    from  emp e,
  5          dept d
  6    where 10 = all(d.deptno,e.deptno)
  7  /

Explained.

SCOTT@orcl > select * from table(dbms_xplan.display)
  2  /

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------
Plan hash value: 568005898

----------------------------------------------------------------------------------------
| Id  | Operation                    | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |         |     3 |    66 |     4   (0)| 00:00:01 |
|   1 |  NESTED LOOPS                |         |     3 |    66 |     4   (0)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| DEPT    |     1 |    13 |     1   (0)| 00:00:01 |
|*  3 |    INDEX UNIQUE SCAN         | PK_DEPT |     1 |       |     0   (0)| 00:00:01 |
|*  4 |   TABLE ACCESS FULL          | EMP     |     3 |    27 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("D"."DEPTNO"=10)
   4 - filter("E"."DEPTNO"=10)

17 rows selected.

SCOTT@orcl >


But it doesn't mean plan will be same in all cases.

SY.
Re: Use of All with equal (=) [message #600900 is a reply to message #600891] Wed, 13 November 2013 12:09 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Hi Solomon,
I was just contemplating if this method is advisable in recent versions, let's say 10g and above?
Re: Use of All with equal (=) [message #600916 is a reply to message #600900] Wed, 13 November 2013 17:46 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
I am not aware of any issues using ANY/ALL/SOME. As usual, checking execution plan is advisable.

SY.

[Updated on: Wed, 13 November 2013 17:46]

Report message to a moderator

Re: Use of All with equal (=) [message #600918 is a reply to message #600900] Wed, 13 November 2013 17:54 Go to previous messageGo to next message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Lalit Kumar B wrote on Wed, 13 November 2013 18:09
Hi Solomon,
I was just contemplating if this method is advisable in recent versions


I am thinking what may be the reason behind your doubt... have you came across any document, any bug discovered regarding it?

Manu
Re: Use of All with equal (=) [message #600927 is a reply to message #600918] Thu, 14 November 2013 01:53 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I am not commenting on any of the approach being correct or incorrect. It ultimately gives the same results, however, it is the way CBO evaluates the ALL operator.

C > ALL (A, B)
will be evaluated as
C> A and C>B
This is a simple comparison, with a subquery, it will be evaluated differently.

Let's say, I have an Emp and Dept table. I am not having any constraints. So, I expect a FTS on both the tables. Let's compare the execution plans :

SQL> EXPLAIN PLAN FOR
  2  SELECT D.DEPT, E.ID
  3    FROM EMP E, DEPT D
  4   WHERE E.DEPT = D.DEPT
  5     AND D.DEPT = 10;
 
Explained

SQL> select * from table(dbms_xplan.display);
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1093152308
---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     2 |    78 |    31   (4)| 00:00:01 |
|*  1 |  HASH JOIN         |      |     2 |    78 |    31   (4)| 00:00:01 |
|*  2 |   TABLE ACCESS FULL| DEPT |     1 |    13 |    15   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| EMP  |     2 |    52 |    15   (0)| 00:00:01 |
---------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   1 - access("E"."DEPT"="D"."DEPT")
   2 - filter("D"."DEPT"=10)
   3 - filter("E"."DEPT"=10)
Note
-----
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
   - dynamic sampling used for this statement (level=2)
 
21 rows selected

SQL> EXPLAIN PLAN FOR
  2  SELECT D.DEPT, E.ID
  3    FROM EMP E, DEPT D
  4   WHERE 10 = ALL(D.DEPT, E.DEPT);
 
Explained

SQL> select * from table(dbms_xplan.display);
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3429684969
-----------------------------------------------------------------------------
| Id  | Operation            | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |      |     2 |    78 |    30   (0)| 00:00:01 |
|   1 |  MERGE JOIN CARTESIAN|      |     2 |    78 |    30   (0)| 00:00:01 |
|*  2 |   TABLE ACCESS FULL  | DEPT |     1 |    13 |    15   (0)| 00:00:01 |
|   3 |   BUFFER SORT        |      |     2 |    52 |    15   (0)| 00:00:01 |
|*  4 |    TABLE ACCESS FULL | EMP  |     2 |    52 |    15   (0)| 00:00:01 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("D"."DEPT"=10)
   4 - filter("E"."DEPT"=10)
Note
-----
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
   - dynamic sampling used for this statement (level=2)
 
21 rows selected


I would rather go with the first approach where Dept table would be hashed and I can see access("E"."DEPT"="D"."DEPT"). I would not like to have a MERGE JOIN CARTESIAN.
Re: Use of All with equal (=) [message #601008 is a reply to message #600927] Thu, 14 November 2013 08:45 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
As I said, you compare execution plan and choose one that fits most. But it wouldn't be wise not to use some feature at all if in some cases that feature results in sub-optimal plan.

SY.
Re: Use of All with equal (=) [message #601017 is a reply to message #601008] Thu, 14 November 2013 10:42 Go to previous message
manubatham20
Messages: 566
Registered: September 2010
Location: Seattle, WA, USA
Senior Member

Solomon Yakobson wrote on Thu, 14 November 2013 14:45
As I said, you compare execution plan and choose one that fits most. But it wouldn't be wise not to use some feature at all if in some cases that feature results in sub-optimal plan.


Agreed.
Previous Topic: Date comparison issue
Next Topic: IF is not working right please suggest
Goto Forum:
  


Current Time: Fri Apr 26 02:12:57 CDT 2024