Home » SQL & PL/SQL » SQL & PL/SQL » + 0 in the where clause (PL/SQL Release 12.1.0.2.0 - Production)
+ 0 in the where clause [message #645653] Thu, 10 December 2015 08:23 Go to next message
msinha8
Messages: 43
Registered: March 2014
Location: Hyderbad
Member
Hi All,

Want to understand the significance of + 0 in the where clause

for say :-

select t1.c1,
       t1.c2,
       t2.c1,
       t2.c2
from table1 t1,
     table2 t2
where t1.c1= t2.c1 + 0;


Regards
Re: + 0 in the where clause [message #645655 is a reply to message #645653] Thu, 10 December 2015 08:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
ask the person who put it there.
perhaps he was paid based upon the number of characters he typed.
Re: + 0 in the where clause [message #645656 is a reply to message #645655] Thu, 10 December 2015 08:41 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
As it is now, the +0 makes no sense.

Two possibilities I can think of why it is there, depending on what data type t2.c1 is.

1) If t2.c1 is a character type, then it forces an implicit conversion of t2.c1 to number. In a way that is sometimes used in Perl.
So it could be the thing was written by a Perl hacker that doesn't really know about SQL and functions like to_number or cast.

2) The other possibility, if t2.c1 is for example numeric or a date, could be that there used to be something other than 0 sometime in the past, and the join was something like "get everything from yesterday an join it to everything from today" or some such, which then changed, and instead of completely deleting the +whaterver the person decided to change it to +0 as to leave a hint to where to adjust that "join offset".





Re: + 0 in the where clause [message #645658 is a reply to message #645656] Thu, 10 December 2015 10:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

I bet the guy read a 20+ years old book and want to prevent from using an index on t2.c1. It was the way we did it with RBO.

(Another possibility is a cheap way to convert a timestamp to a date.)

Re: + 0 in the where clause [message #645659 is a reply to message #645656] Thu, 10 December 2015 10:11 Go to previous messageGo to next message
msinha8
Messages: 43
Registered: March 2014
Location: Hyderbad
Member
Thanks all!!

So, By above discussion, can I make this point that "+0" is irrelevant to use?
Re: + 0 in the where clause [message #645660 is a reply to message #645659] Thu, 10 December 2015 10:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Ir depends.
DESC TABLE1
DESC TABLE2

[Updated on: Thu, 10 December 2015 10:30]

Report message to a moderator

Re: + 0 in the where clause [message #645671 is a reply to message #645660] Thu, 10 December 2015 12:48 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Adding the +0 would be the same as using col1||null for a varchar2 field. If forces the optimizer to not use a specific index on that column (unless you have a function index) and was heavily used in the Rule Based Optimizer. As long as your statistics are up to date there is no need to do that under the Cost Based Optimizer used in modern versions of Oracle. Also you could use an optimizer hint to force it to use a different index. Both methods were used.

[Updated on: Thu, 10 December 2015 12:49]

Report message to a moderator

Re: + 0 in the where clause [message #645685 is a reply to message #645653] Fri, 11 December 2015 00:28 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
While it would work for numeric type, however the performance might degrade.

For example, comparing the explain plans:

SQL> SET autot ON EXPLAIN
SQL> SELECT A.empno,
  2    b.deptno
  3  FROM scott.emp A,
  4    scott.dept b
  5  WHERE A.deptno = b.deptno +0;

     EMPNO     DEPTNO
---------- ----------
      7369         20
      7499         30
      7521         30
      7566         20
      7654         30
      7698         30
      7782         10
      7788         20
      7839         10
      7844         30
      7876         20
      7900         30
      7902         20
      7934         10

14 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 71037407

-----------------------------------------------------------------------------
| Id  | Operation          | Name    | Rows  | Bytes | Cost (%CPU)| Time
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |         |    14 |   140 |     4   (0)| 00:00:01
|*  1 |  HASH JOIN         |         |    14 |   140 |     4   (0)| 00:00:01
|   2 |   INDEX FULL SCAN  | PK_DEPT |     4 |    12 |     1   (0)| 00:00:01
|   3 |   TABLE ACCESS FULL| EMP     |    14 |    98 |     3   (0)| 00:00:01
-----------------------------------------------------------------------------

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

   1 - access("A"."DEPTNO"="B"."DEPTNO"+0)

SQL> SELECT A.empno,
  2    b.deptno
  3  FROM scott.emp A,
  4    scott.dept b
  5  WHERE A.deptno = b.deptno;

     EMPNO     DEPTNO
---------- ----------
      7369         20
      7499         30
      7521         30
      7566         20
      7654         30
      7698         30
      7782         10
      7788         20
      7839         10
      7844         30
      7876         20
      7900         30
      7902         20
      7934         10

14 rows selected.


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

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

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

   1 - filter("A"."DEPTNO" IS NOT NULL)


In the former case, it is forcing the optimizer to do an additional index full scan which you can see as access("A"."DEPTNO"="B"."DEPTNO"+0). It is an overhead, while all we want is to get all the rows therefore one FTS on EMP is enough as we have a foreign key constraint defined.
Re: + 0 in the where clause [message #645686 is a reply to message #645685] Fri, 11 December 2015 01:25 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Your conclusion is wrong. It is highly dependent on the version, tables and data.
One example does not prove a rule.

Quote:
While it would work for numeric type


And other data types.

[Updated on: Fri, 11 December 2015 01:30]

Report message to a moderator

Previous Topic: Join on TABLE OF NUMBER passed as a parameter to a function
Next Topic: How to Resolve error: ORA-00932
Goto Forum:
  


Current Time: Tue Jun 30 19:25:01 CDT 2026