SELECT, can you help me explain this simple result please

Hi, I have semething that I can't explain with my knowledge of the subsoil of sql:
T1 (number cu_id):
1
2
3
4

T2 (number cu_id):
3
4
5
6
-------------------------------------------------------
Select T1.cu_id from T1, T2 where T1.cu_id! = T2.cu_id;

which produces 14 lines, looking at the result I guess what is happening on such a cross join, can you help me understand how sql in doing so:
1
1
1
1
2
2
2
2
3
3
3
4
4
4
-----------------------------------------
While select T1.cu_id from T1, T2 where T1.cu_id = T2.cu_id; -* (with the equals sign) produces 2 rows, which is good for me
3
4



TX
T

Published by: trento on 26 May 2011 20:53

Published by: trento on May 26, 2011 20:54

Because it takes each T1 line and compares against T2 lines - essentially a loop in all lines.

SQL> explain plan for  select t1.col_1 from t1, t2 where t1.col_1 != t2.col_1;

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1967407726

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |    12 |   312 |     6   (0)| 00:00:01 |
|   1 |  NESTED LOOPS      |      |    12 |   312 |     6   (0)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| T1   |     4 |    52 |     2   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| T2   |     3 |    39 |     1   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   3 - filter("T1"."COL_1"<>"T2"."COL_1")

Note
-----
   - dynamic sampling used for this statement

19 rows selected.

SQL>

What you should do is NOT IN operation:

SQL> explain plan for  select t1.col_1 from t1 where col_1 not in (select col_1 from t2);

Explained.

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 895956251

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |    13 |     4   (0)| 00:00:01 |
|*  1 |  FILTER            |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL| T1   |     4 |    52 |     2   (0)| 00:00:01 |
|*  3 |   TABLE ACCESS FULL| T2   |     4 |    52 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

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

   1 - filter( NOT EXISTS (SELECT /*+ */ 0 FROM "T2" "T2" WHERE
              LNNVL("COL_1"<>:B1)))
   3 - filter(LNNVL("COL_1"<>:B1))

Note
-----
   - dynamic sampling used for this statement

21 rows selected.

SQL>  select t1.col_1 from t1 where col_1 not in (select col_1 from t2);

     COL_1
----------
         1
         2

SQL>

Hemant K Collette

Tags: Database

Similar Questions

Maybe you are looking for

  • IPhone cannot be restored (IOS 10 update)

    I downloaded the update of 10 new IOS on my Iphone 6s. Now, there is an Itunes logo on my screen. I have already connected my phone with Itunes but I can't restore... Help, please

  • You can import Contacts from Gmail in Firefox OS?

    It seems that you can only import Contacts from a SIM or Facebook card. I guess that means that you can't import or synchronize the Contacts of Gmail. Is this correct?

  • I can't get homeshare bind iPhotos

    I've updated my mac and iPhotos is different than it used to be when I got to my apple tv.  For some reason, I can't identify a photo library even if I 26 000 digital photos available on my mac.  More precisely: My Mac of 2009 vintage and does not al

  • Yupp tv missing app after the recent update

    We have subscription Yupp tv on our Sony Bravia KDL46-EX620. This app has been missing since 11/13, and we are not able to access the programs. This happened after a recent refresh of internet content. We contacted Yupp tv several times and told us t

  • How can I write in a table cell (row, column appear) in a databae?

    How can I write in a table cell (row, column appear) in a database using LabVIEW Database Toolkit? I use Ms Access. Suppose I have three columns in a table, I write 1 row of the 1st column, then 1st rank of the 3rd column. The problem I have is after