Home » SQL & PL/SQL » SQL & PL/SQL » IN (or) Equality Operator (SQL)
IN (or) Equality Operator [message #651566] Fri, 20 May 2016 02:31 Go to next message
kirankumarmutte
Messages: 4
Registered: May 2016
Junior Member
Hello Mates,

Which operator is the best one among the below operators in performance point of view?

IN (or) =

Thanks!!
Re: IN (or) Equality Operator [message #651568 is a reply to message #651566] Fri, 20 May 2016 02:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

None is best. They are same.

Re: IN (or) Equality Operator [message #651569 is a reply to message #651568] Fri, 20 May 2016 02:46 Go to previous messageGo to next message
kirankumarmutte
Messages: 4
Registered: May 2016
Junior Member
Hi Michel,

Thanks for the quick reply!!

I got this question from interviewer and I had answered nothing.

After that had a discussion with my friends, everyone has different view. Means some are saying IN is the best and some are saying = is the best.

Could not finalize the answer with proper solution.

Could you explain the process in reality?

Thanks in advance!!
Re: IN (or) Equality Operator [message #651570 is a reply to message #651569] Fri, 20 May 2016 02:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

IN is internally converted to OR (see the filter predicate):
SQL> explain plan for select * from dual where dummy in ('A','B','C');

Explained.

SQL> select * from table(dbms_xplan.display());
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------
Plan hash value: 272002086

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("DUMMY"='A' OR "DUMMY"='B' OR "DUMMY"='C')

I'd like to know the reasons of each of the different views.

[Updated on: Fri, 20 May 2016 02:52]

Report message to a moderator

Re: IN (or) Equality Operator [message #651571 is a reply to message #651570] Fri, 20 May 2016 02:59 Go to previous messageGo to next message
kirankumarmutte
Messages: 4
Registered: May 2016
Junior Member
Hi Michel,

IN is internally converted to OR (as per above explain plan).

In this case, = and OR both are same?

Please clarify.
Re: IN (or) Equality Operator [message #651572 is a reply to message #651571] Fri, 20 May 2016 03:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Clarify what?
I:
1/ said IN and OR= are same
2/ showed they are same

So what do you want I clarify?

But you, you did not answer to my question about what are the arguments of each point of view.

Re: IN (or) Equality Operator [message #651576 is a reply to message #651572] Fri, 20 May 2016 04:01 Go to previous messageGo to next message
kirankumarmutte
Messages: 4
Registered: May 2016
Junior Member
Hi Michel,

Got your explanation. But, I did not have the arguments as it was a interview question.

Hope you understand. Thanks!!

Re: IN (or) Equality Operator [message #651593 is a reply to message #651576] Fri, 20 May 2016 10:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK but you said: "After that had a discussion with my friends, everyone has different view. Means some are saying IN is the best and some are saying = is the best.".

Re: IN (or) Equality Operator [message #651616 is a reply to message #651593] Fri, 20 May 2016 15:46 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
Performance is the same.

However, there is a limit on how many values you can have in an "IN (...)" clause.

I am not aware of any limit on "OR = ..." conditions.

If you have a long list and they can be selected from somewhere, then ideally:

SELECT ...
FROM   ...
WHERE  ... IN
       (SELECT ...
        FROM   ...);

[Updated on: Fri, 20 May 2016 15:47]

Report message to a moderator

Re: IN (or) Equality Operator [message #651617 is a reply to message #651566] Fri, 20 May 2016 16:16 Go to previous message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
You need to be careful with NULLs with this sort of thing. For example, two queries that you might think would give the same result:
orclz>
orclz> update emp set ename=null where ename='MILLER';

1 row updated.

orclz> create index ename_i on emp (ename);

Index created.

orclz> set autot on exp
orclz> select * from emp where ename='KING' or ename is null;

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7839 KING       PRESIDENT            1981-11-17:00:00:00       5000                    10
      7934            CLERK           7782 1982-01-23:00:00:00       1300                    10


Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     2 |   174 |     3   (0)| 00:00:01 |
|*  1 |  TABLE ACCESS FULL| EMP  |     2 |   174 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

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

   1 - filter("ENAME" IS NULL OR "ENAME"='KING')

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)

orclz> select * from emp where ename in ('KING',null);

     EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
      7839 KING       PRESIDENT            1981-11-17:00:00:00       5000                    10


Execution Plan
----------------------------------------------------------
Plan hash value: 4162339657

-----------------------------------------------------------------------------------------------
| Id  | Operation                           | Name    | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                    |         |     1 |    87 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID BATCHED| EMP     |     1 |    87 |     2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN                  | ENAME_I |     1 |       |     1   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------

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

   2 - access("ENAME"='KING')

Note
-----
   - dynamic statistics used: dynamic sampling (level=2)

orclz>

Previous Topic: Display the columns of a table into rows delimited by comma
Next Topic: Moving data older than 2 years in all fact tables to respective archive tables & purging data
Goto Forum:
  


Current Time: Thu Apr 25 04:37:57 CDT 2024