It's All About ORACLE

Oracle - The number one Database Management System. Hope this Blog will teach a lot about oracle.

Find indexes for a table


It is not unusual to wonder what indexes a specific table might have, and what columns in the table are assigned to those indexes. The following query will provide this information to you. It draws on the information in the dba_ind_columns data dictionary view:


SQL> column table_owner format a15
SQL> column table_name format a20
SQL> column index_name format a20
SQL> column column_name format a20
 
SQL> Select owner, table_name, index_name, column_name
2  FROM dba_ind_columns
3  Order by owner, table_name, column_name
4  Where owner=’SCOTT’
5  AND table_name=’EMP’;
 
TABLE_OWNER     TABLE_NAME           INDEX_NAME           COLUMN_NAME
--------------- -------------------- -------------------- ----------
SCOTT           EMP                  PK_EMP               EMPNO
 
In this example we find that the EMP table in the SCOTT schema has one index called PK_EMP. This index is built on a single column, EMPNO.

grep command in Solaris


grep with ls command:
======================
  • Finding files whose last 4 digits are not integer:
bash-3.2# ls -ltr *[!0-9][!0-9][!0-9][!0-9]
-rw-r--r--   1 root     root     8101353 May 21  2012 apache-tomcat-7.0.27.zip
-rw-r--r--   1 root     root     1058049 Jun  5  2012 kanban.war
-r--------   1 root     root          16 May  6 15:01 autoreg_key
-rw-------   1 root     root          72 May  6 15:01 autoreg_config
-rw-r--r--   1 oracle   oinstall       0 May 11 13:25 DGStatusMonitor.lock
  • Finding files whose last 4 digits are integer:
bash-3.2# ls -ltr *[0-9][0-9][0-9][0-9]
-rw-rw----   1 oracle   oinstall  889038 May 10 08:31 net11956
-rw-rw----   1 oracle   oinstall   10417 May 10 08:31 US11956
-rw-rw----   1 oracle   oinstall   22605 May 10 08:31 TCP11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 ITCP11956
-rw-rw----   1 oracle   oinstall   15076 May 10 08:31 BEQ11956
-rw-rw----   1 oracle   oinstall   16566 May 10 08:31 TCPS11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 SPX11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 ISPX11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 DEC11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 DCE11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 LU6211956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 NMP11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 RAW11956
-rw-rw----   1 oracle   oinstall       0 May 10 08:31 VI11956
-rw-rw----   1 oracle   oinstall 1700310 May 10 08:31 net12166
-rw-rw----   1 oracle   oinstall   17738 May 10 08:31 US12166
  • Finding only directories:
bash-3.2# ls -ltr | grep ^d
drwxr-xr-x   2 oracle   oinstall     512 May 11 13:25 dataguard


  • Finding all files whose file name starts with first two digits capital alphabet, then 3rd, 4th digit are numeric.
bash-3.2# ls [A-Z][A-Z][0-9][0-9]*
LU6211956  LU6216111  LU623695   LU627535   US11956    US16111    US3695     US7535     VI11956    VI16111    VI3695     VI7535
LU6212166  LU6216485  LU623905   LU627909   US12166    US16485    US3905     US7909     VI12166    VI16485    VI3905     VI7909
LU6212519  LU6216695  LU624255   LU628119   US12519    US16695    US4255     US8119     VI12519    VI16695    VI4255     VI8119
LU6212729  LU6217925  LU624465   LU629331   US12729    US17925    US4465     US9331     VI12729    VI17925    VI4465     VI9331
LU6215893  LU6218135  LU627317   LU629541   US15893    US18135    US7317     US9541     VI15893    VI18135    VI7317     VI9541
bash-3.2# 


  • Checking for all directories:
bash-3.2# ls -l | grep ^d  
drwxrwxr-x   5 adm      adm          512 May  6 15:03 acct
drwxr-xr-x   4 root     root         512 May  9 22:36 ChargingSystem
drwxrwxrwt   2 root     root         512 May 11 22:41 ChargingSystemLogs
drwxr-xr-x   2 adm      adm          512 Jan 31 19:44 exacct
drwxr-xr-x   2 adm      adm          512 May  9 23:06 log
drwxr-xr-x   2 root     sys          512 Jan 31 19:49 pool
drwxrwxr-x   2 adm      sys          512 Jan 31 19:45 sa
drwxr-xr-x   2 root     sys          512 Jan 31 19:47 sm.bin
drwxr-xr-x   2 root     sys          512 Jan 31 19:44 streams
  • Checking for all files only:
bash-3.2# ls -ltr | grep ^-rw
-rw-rw-rw-   1 root     bin            0 Jun 28  2011 spellhist
-rw-------   1 uucp     bin            0 Jun 28  2011 aculog
-rw-r--r--   1 root     root           0 Jan 31 19:57 vold.log
-rw-r--r--   1 root     root      452949 May  7 02:01 messages.2
-rw-r--r--   1 root     root      329127 May 13 05:17 messages.1
-rw-r--r--   1 root     root        2116 May 20 05:17 messages.0
-rw-r--r--   1 root     root        6145 May 23 10:28 messages
-rw-r--r--   1 root     bin         5208 May 23 17:27 utmpx
-rw-r--r--   1 adm      adm       395064 May 23 18:00 wtmpx
-rw-------   1 root     root       20681 May 23 18:03 sulog
-rw-r--r--   1 root     root         105 May 23 18:48 emc_monitor.log

grep command:
==============
  • Finding all files in a directory having string "running":
bash-3.2# grep -l running *
dg_status_Sat_May_11_13:25:40_2013.log
bash-3.2# 
  • Finding lines in file dg_status_sasa.log where "running" string is present.
bash-3.2# grep running dg_status_sasa.log 
-- dgstatusmonitor running at Sun_May_12_01:29:47_2013 --
-- dgstatusmonitor running at Sun_May_12_13:29:55_2013 --
-- dgstatusmonitor running at Mon_May_20_01:35:45_2013 --
-- dgstatusmonitor running at Mon_May_20_13:35:53_2013 --
-- dgstatusmonitor running at Tue_May_21_01:36:01_2013 --
-- dgstatusmonitor running at Tue_May_21_13:36:09_2013 --
bash-3.2# 
  • Finding lines in file dg_status_sasa.log where "running" string is not present.
bash-3.2# grep -v running dg_status_sasa.log 


  • Check for case insensitive "running" sting also:

bash-3.2# grep -i running dg_status_sasa.log 


  • Checking how many times "running" occured:

bash-3.2# grep -c running saasaa.log 
6
  • Check for line number at which it occurs:
bash-3.2# grep -n running saasaa.log 
5:-- dgstatusmonitor running at Sun_May_12_01:29:47_2013 --
7:-- dgstatusmonitor running at Sun_May_12_13:29:55_2013 -- running
21:running
50:-- dgstatusmonitor running at Mon_May_20_01:35:45_2013 --
52:-- dgstatusmonitor running at Mon_May_20_13:35:53_2013 --
54:-- dgstatusmonitor running at Tue_May_21_01:36:01_2013 --
56:-- dgstatusmonitor running at Tue_May_21_13:36:09_2013 --
  • Make a search for two patterns using extended grep (egrep):
bash-3.2# egrep '(running|DGMGRL)' saasaa.log 
-- dgstatusmonitor running at Sun_May_12_01:29:47_2013 --
-- dgstatusmonitor running at Sun_May_12_13:29:55_2013 -- 
running
Additional information retrieved : DGMGRL for Solaris: Version 10.2.0.5.0 - 64bit Production
Welcome to DGMGRL, type "help" for information.
Additional information retrieved : DGMGRL for Solaris: Version 10.2.0.5.0 - 64bit Production
Additional information retrieved : DGMGRL for Solaris: Version 10.2.0.5.0 - 64bit Production
-- dgstatusmonitor running at Mon_May_20_01:35:45_2013 --
-- dgstatusmonitor running at Mon_May_20_13:35:53_2013 --
-- dgstatusmonitor running at Tue_May_21_01:36:01_2013 --
-- dgstatusmonitor running at Tue_May_21_13:36:09_2013 --

ORACLE - EXISTS, IN, JOIN operators

The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.

SQL IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);

The EXISTS Operator

The EXISTS checks the existence of a result of a subquery. The EXISTS subquery tests whether a subquery fetches at least one row. When no data is returned then this operator returns 'FALSE'. 
A valid EXISTS subquery must contain an outer reference and it must be a correlated subquery. 
The select list in the EXISTS subquery is not actually used in evaluating the EXISTS so it can contain any valid select list.


SQL JOIN

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table.

Explaining IN, EXISTS and JOIN:

1. Getting details of all employee having salary more than avg salary of their department.

Wrong:
SELECT LAST_NAME, SALARY, DEPARTMENT_ID
FROM EMPLOYEES A
WHERE SALARY > ( SELECT AVG(SALARY)
FROM EMPLOYEES B
WHERE B.EMPLOYEE_ID = A.EMPLOYEE_ID
GROUP BY DEPARTMENT_ID);


Right:
SELECT LAST_NAME, SALARY, A.DEPARTMENT_ID, I.AVGSAL
FROM EMPLOYEES A, (SELECT AVG(SALARY) AVGSAL, DEPARTMENT_ID  
FROM EMPLOYEES GROUP BY DEPARTMENT_ID ) I
WHERE A.DEPARTMENT_ID = I.DEPARTMENT_ID
AND SALARY > ( SELECT AVG(SALARY)
FROM EMPLOYEES 
WHERE DEPARTMENT_ID = A.DEPARTMENT_ID);

2. Displaying records of employee who have switched job at least twice.

Wrong:
SELECT EMPLOYEE_ID, LAST_NAME, JOB_ID
FROM EMPLOYEES A
WHERE EXISTS ( SELECT 'X' FROM JOB_HISTORY
WHERE EMPLOYEE_ID = A.EMPLOYEE_ID
HAVING COUNT(*) > 2);

Right:
SELECT EMPLOYEE_ID, LAST_NAME, JOB_ID
FROM EMPLOYEES A
WHERE 2 <= ( SELECT count(*) FROM JOB_HISTORY
WHERE EMPLOYEE_ID = A.EMPLOYEE_ID);



3. EXISTS ensures search in the inner query does not continue when at least one match is found. EXISTS operator tests for existence of rows in the results set of the subquery.

SELECT ENAME, DEPTNO FROM EMP
WHERE EXISTS ( SELECT 'X' FROM DEPT 
WHERE DEPTNO = &DEPT_NUM);

4. Playing Employee Manager Self Join:

SQL> select * from emp;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

A). Want to find out manager of each employee

SELECT E.EMPNO, E.ENAME EMPLOYER, E.JOB EmpJob, E.MGR, M.ENAME Managers, M.JOB ManagerJob
FROM EMP E, EMP M
WHERE E.MGR = M.EMPNO
ORDER BY E.EMPNO;

     EMPNO EMPLOYER   EMPJOB           MGR MANAGERS   MANAGERJO
---------- ---------- --------- ---------- ---------- ---------
      7369 SMITH      CLERK           7902 FORD       ANALYST
      7499 ALLEN      SALESMAN        7698 BLAKE      MANAGER
      7521 WARD       SALESMAN        7698 BLAKE      MANAGER
      7566 JONES      MANAGER         7839 KING       PRESIDENT
      7654 MARTIN     SALESMAN        7698 BLAKE      MANAGER
      7698 BLAKE      MANAGER         7839 KING       PRESIDENT
      7782 CLARK      MANAGER         7839 KING       PRESIDENT
      7788 SCOTT      ANALYST         7566 JONES      MANAGER
      7844 TURNER     SALESMAN        7698 BLAKE      MANAGER
      7876 ADAMS      CLERK           7788 SCOTT      ANALYST
      7900 JAMES      CLERK           7698 BLAKE      MANAGER

     EMPNO EMPLOYER   EMPJOB           MGR MANAGERS   MANAGERJO
---------- ---------- --------- ---------- ---------- ---------
      7902 FORD       ANALYST         7566 JONES      MANAGER
      7934 MILLER     CLERK           7782 CLARK      MANAGER

13 rows selected.

B). Now we need to find the managers and employees who report to them. 

SELECT M.EMPNO, M.ENAME Manager, M.JOB ManagerJ,  E.EMPNO, E.ENAME Employes, E.JOB EmployerJ
FROM EMP E, EMP M
WHERE E.MGR = M.EMPNO;

     EMPNO MANAGER    MANAGERJ       EMPNO EMPLOYES   EMPLOYERJ
---------- ---------- --------- ---------- ---------- ---------
      7566 JONES      MANAGER         7902 FORD       ANALYST
      7566 JONES      MANAGER         7788 SCOTT      ANALYST
      7698 BLAKE      MANAGER         7844 TURNER     SALESMAN
      7698 BLAKE      MANAGER         7499 ALLEN      SALESMAN
      7698 BLAKE      MANAGER         7521 WARD       SALESMAN
      7698 BLAKE      MANAGER         7900 JAMES      CLERK
      7698 BLAKE      MANAGER         7654 MARTIN     SALESMAN
      7782 CLARK      MANAGER         7934 MILLER     CLERK
      7788 SCOTT      ANALYST         7876 ADAMS      CLERK
      7839 KING       PRESIDENT       7698 BLAKE      MANAGER
      7839 KING       PRESIDENT       7566 JONES      MANAGER

     EMPNO MANAGER    MANAGERJ       EMPNO EMPLOYES   EMPLOYERJ
---------- ---------- --------- ---------- ---------- ---------
      7839 KING       PRESIDENT       7782 CLARK      MANAGER
      7902 FORD       ANALYST         7369 SMITH      CLERK

13 rows selected.

C). Find out employee who have at least one person reporting to them.

SQL> SELECT DISTINCT M.MGR, E.ENAME, E.JOB
  2  FROM EMP M, EMP E
  3  WHERE M.MGR = E.EMPNO
  4  AND M.MGR IS NOT NULL;

       MGR ENAME      JOB
---------- ---------- ---------
      7839 KING       PRESIDENT
      7782 CLARK      MANAGER
      7788 SCOTT      ANALYST
      7566 JONES      MANAGER
      7698 BLAKE      MANAGER
      7902 FORD       ANALYST

6 rows selected.

II. Perfoming point C using EXISTS clause
==========================================

SQL> SELECT EMPNO, ENAME, JOB
  2  FROM EMP E
  3  WHERE EXISTS ( SELECT 'X' FROM EMP WHERE MGR=E.EMPNO);

     EMPNO ENAME      JOB
---------- ---------- ---------
      7566 JONES      MANAGER
      7698 BLAKE      MANAGER
      7782 CLARK      MANAGER
      7788 SCOTT      ANALYST
      7839 KING       PRESIDENT
      7902 FORD       ANALYST

6 rows selected.

Perfoming this operation using IN:
----------------------------------

SELECT EMPNO, ENAME, JOB
FROM EMP 
WHERE EMPNO IN ( SELECT MGR FROM EMP WHERE MGR IS NOT NULL);



Checking Execution plan for the above three methods:
====================================================

1. Using Self Join:
===================

SQL>  SELECT DISTINCT M.MGR, E.ENAME, E.JOB
  2    FROM EMP M, EMP E
  3    WHERE M.MGR = E.EMPNO
  4    AND M.MGR IS NOT NULL;

       MGR ENAME      JOB
---------- ---------- ---------
      7839 KING       PRESIDENT
      7782 CLARK      MANAGER
      7788 SCOTT      ANALYST
      7566 JONES      MANAGER
      7698 BLAKE      MANAGER
      7902 FORD       ANALYST

6 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 1982370969

----------------------------------------------------------------------------------------
| Id  | Operation                     | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |        |    13 |   286 |     7  (29)| 00:00:01 |
|   1 |  HASH UNIQUE                  |        |    13 |   286 |     7  (29)| 00:00:01 |
|   2 |   MERGE JOIN                  |        |    13 |   286 |     6  (17)| 00:00:01 |
|   3 |    TABLE ACCESS BY INDEX ROWID| EMP    |    14 |   252 |     2   (0)| 00:00:01 |
|   4 |     INDEX FULL SCAN           | PK_EMP |    14 |       |     1   (0)| 00:00:01 |
|*  5 |    SORT JOIN                  |        |    13 |    52 |     4  (25)| 00:00:01 |
|*  6 |     TABLE ACCESS FULL         | EMP    |    13 |    52 |     3   (0)| 00:00:01 |
----------------------------------------------------------------------------------------

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

   5 - access("M"."MGR"="E"."EMPNO")
       filter("M"."MGR"="E"."EMPNO")
   6 - filter("M"."MGR" IS NOT NULL)


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
          9  consistent gets
          0  physical reads
          0  redo size
        685  bytes sent via SQL*Net to client
        415  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          6  rows processed


Using EXISTS
=============

SQL>   SELECT EMPNO, ENAME, JOB
  2    FROM EMP E
  3    WHERE EXISTS ( SELECT 'X' FROM EMP WHERE MGR=E.EMPNO);

     EMPNO ENAME      JOB
---------- ---------- ---------
      7566 JONES      MANAGER
      7698 BLAKE      MANAGER
      7782 CLARK      MANAGER
      7788 SCOTT      ANALYST
      7839 KING       PRESIDENT
      7902 FORD       ANALYST

6 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 71815250

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     6 |   132 |     6  (17)| 00:00:01 |
|   1 |  MERGE JOIN SEMI             |        |     6 |   132 |     6  (17)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP    |    14 |   252 |     2   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN           | PK_EMP |    14 |       |     1   (0)| 00:00:01 |
|*  4 |   SORT UNIQUE                |        |    13 |    52 |     4  (25)| 00:00:01 |
|*  5 |    TABLE ACCESS FULL         | EMP    |    13 |    52 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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

   4 - access("MGR"="E"."EMPNO")
       filter("MGR"="E"."EMPNO")
   5 - filter("MGR" IS NOT NULL)


Statistics
----------------------------------------------------------
          1  recursive calls
          0  db block gets
         11  consistent gets
          0  physical reads
          0  redo size
        687  bytes sent via SQL*Net to client
        415  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          6  rows processed

Using IN
=========

SQL> SELECT EMPNO, ENAME, JOB
  2  FROM EMP
  3  WHERE EMPNO IN ( SELECT MGR FROM EMP WHERE MGR IS NOT NULL);

     EMPNO ENAME      JOB
---------- ---------- ---------
      7566 JONES      MANAGER
      7698 BLAKE      MANAGER
      7782 CLARK      MANAGER
      7788 SCOTT      ANALYST
      7839 KING       PRESIDENT
      7902 FORD       ANALYST

6 rows selected.


Execution Plan
----------------------------------------------------------
Plan hash value: 71815250

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     6 |   132 |     6  (17)| 00:00:01 |
|   1 |  MERGE JOIN SEMI             |        |     6 |   132 |     6  (17)| 00:00:01 |
|   2 |   TABLE ACCESS BY INDEX ROWID| EMP    |    14 |   252 |     2   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN           | PK_EMP |    14 |       |     1   (0)| 00:00:01 |
|*  4 |   SORT UNIQUE                |        |    13 |    52 |     4  (25)| 00:00:01 |
|*  5 |    TABLE ACCESS FULL         | EMP    |    13 |    52 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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

   4 - access("EMPNO"="MGR")
       filter("EMPNO"="MGR")
   5 - filter("MGR" IS NOT NULL)


Statistics
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         11  consistent gets
          0  physical reads
          0  redo size
        687  bytes sent via SQL*Net to client
        415  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          6  rows processed
 
Joining two tables using EXISTS and Joins
===========================================

SELECT EMPNO, ENAME, DEPTNO
FROM EMP E
WHERE EXISTS ( SELECT 'X'
 FROM DEPT 
 WHERE DEPTNO = E.DEPTNO);
 
SELECT EMPNO, ENAME, E.DEPTNO
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO;
 
     EMPNO ENAME          DEPTNO
---------- ---------- ----------
      7369 SMITH              20
      7499 ALLEN              30
      7521 WARD               30
      7566 JONES              20
      7654 MARTIN             30
      7698 BLAKE              30
      7782 CLARK              10
      7788 SCOTT              20
      7839 KING               10
      7844 TURNER             30
      7876 ADAMS              20

     EMPNO ENAME          DEPTNO
---------- ---------- ----------
      7900 JAMES              30
      7902 FORD               20
      7934 MILLER             10

14 rows selected.


When to use EXISTS and When to use IN:


Lets say the result of the subquery
    ( select y from T2 )

is "huge" and takes a long time.  But the table T1 is relatively small and executing ( select null from t2 where y = x.x ) is very very fast (nice index on t2(y)).  Then the exists will be faster as the time to full scan T1 and do the index probe into T2 could be less then the time to simply full scan T2 to build the subquery we need to distinct on.

Lets say the result of the subquery is small -- then IN is typicaly more appropriate.

If both the subquery and the outer table are huge -- either might work as well as the other -- depends on the indexes and other factors. 


You Might Also Like

Related Posts with Thumbnails

Pages