In (select distinct .... ) Vs exists

Guys, which would be less costly and also quicker performance:
SELECT a.*
          FROM table1 a
WHERE a.linkid IN (
          SELECT DISTINCT b.id1
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING'
          UNION ALL
          SELECT DISTINCT b.id2
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING'
          UNION ALL
          SELECT DISTINCT b.id3
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING'
          UNION ALL
          SELECT DISTINCT b.id4
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING'
          UNION ALL
          SELECT DISTINCT b.id5
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING'
          UNION ALL
          SELECT DISTINCT b.id5
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING'
          UNION ALL
          SELECT DISTINCT b.id7
                     FROM table2 b
                    WHERE b.REF = 'SOMETHING')
SELECT a.*
  FROM table1 a
WHERE EXISTS (
          SELECT 1
            FROM table2 b
           WHERE b.REF = 'SOMETHING'
             AND (   a.linkid = b.id1
                  OR a.linkid = b.id2
                  OR a.linkid = b.id3
                  OR a.linkid = b.id4
                  OR a.linkid = b.id5
                  OR a.linkid = b.id6
                  OR a.linkid = b.id7)
                   )I have a feeling the optimiser may execute both with the same plan.
Thanks, FIXXXER
Edited by: fixxxer on 04-Nov-2011 05:54

Depends on a lot of things, one of them being your version.
It's possible Oracle would rewrite your query using a myriad of techniques, one of the newer ones being an OR EXPANSION
http://blogs.oracle.com/optimizer/entry/or_expansion_transformation
One thing to note is that Oracle knows not to add the DISTINCT's inside of the IN clause .... because when you say IN (some_set) that set is always DISTINCT'd for you.

Similar Messages

  • Select distinct problem with muliple join tables, help needed

    Hi,
    I have two main tables. Each has its of sub joined tables.
    guest_id_for_reservation connects two major tables. This has
    to be that way
    because my guest may change the room status from single to
    double (and the
    similar exceptional requests).
    guests reservation
    guest_id_for_reservation
    countrytable hoteltable
    delegationtable roomtype
    I form a query. I want to select distinct those results. But
    it does not
    work.
    If I do not include any table related to reservation table
    and its sub
    joined tables (disregarding guest_id_for_reservation), it
    works.
    Is there a specific syntax for select distinct of this type
    or any
    workaround.?
    Thank you
    Hakan

    Hi I'm still battling with this - have connected the AX to my Imac via ethernet and it shows up fine in Airport Utility. Status light is green and it says its set up to connect to my existing wireless network using wireless connection. Security in Network Preferences is the same for both: WPA2 Personal.
    So I don't think there's a problem with the AX, and my current wireless network (BT Home Hub) is working fine.
    And when I restore factory settings Airport Utility can see the AX before updating settings so the wireless side of AX must work too.
    I'm figuring it must be something about the settings that mean AU can't see it anymore. But I can't work out what, since security is the same.
    Any ideas would be great!

  • Query help in select Distinct on one column.

    CREATE GLOBAL TEMPORARY TABLE Table1 (
    ID1 varchar2(100) ,
    Name1 varchar2(100),
    Name11 varchar2(100)
    insert into Table1 values ('a','n1','h3');
    insert into Table1 values('b','n2','h2');
    insert into Table1 values('a','n3','h1');
    insert into Table1 values('c','n4','h5');
    insert into Table1 values ('c','n5','h4');
    insert into Table1 values('d','n6','h6');
    select * from Table1;
    ID1,NAME1,     NAME11
    a,     n1,     h3
    b,     n2,     h2
    a,     n3,     h1
    c,     n4,     h5
    c,     n5,     h4
    d,     n6,     h6
    I am trying to select distinct ID1 and all values associated with it which is max row.I want to result as -
    ID1,NAME1,     NAME11
    a,     n3,     h1
    b,     n2,     h2
    c,     n5,     h4
    d,     n6,     h6
    Can you please help me to write simple query to get above result.
    Edited by: 871447 on Jul 25, 2011 9:42 AM
    Edited by: 871447 on Jul 25, 2011 9:45 AM

    Hi,
    Do a self-join, to combine the two rows for each value of id1 onto one output row.
    Make it an outer join, in case there is only one row with a vlaue for id1.
    SELECT  l.id1
    ,     l.name1
    ,     NVL ( r.name11
             , l.name11
             )          AS name11
    FROM              table1     l
    LEFT OUTER JOIN     table1     r  ON  l.id1     = r.id1
                              AND l.name1     < r.name1
    ;Edited by: Frank Kulash on Jul 25, 2011 12:57 PM
    Sorry, I mis-read the problem.
    Lee's solution, above, assumes that name1 is unique, as it is in your sample data.
    What output would you want if that's not the case?
    If name1 is not unique, but the combination of (id1, name1) is unique, then you can modify Lee's solution like this:
    SELECT  *
    FROM    table1
    WHERE   (id1, name1) IN (
                        SELECT    id1
                        ,       MAX (name1)
                               FROM          table1
                        GROUP BY  id1
    ;Or, if you can't make any assumptions about uniqueness, you might need something like this:
    WITH     got_r_num     AS
         SELECT  id1, name1, name11
         ,     ROW_NUMBER () OVER ( PARTITION BY  id1
                                   ORDER BY          name1     DESC
                             ,                name11     DESC
                           )      AS r_num
         FROM     table1
    SELECT  id1, name1, name11
    FROM     got_r_num
    WHERE     r_num     = 1
    ;

  • How to select distinct values from a table when it has composite primary ke

    Hi
    I have the requirement like , I need to select distinct one column values from the table which has composite primary key. How to acheive this functioinality using view object.
    Eg : Table 1 has col1 and col2, col3
    col1 col2 col3
    1 A NA
    1 B NA
    2 A NA
    3 C NA
    2 D NA
    primary key (col1,col2)
    I have to select distinct col1.
    Thanks

    Hi
    I got the solution for above. By Creating the read only view object we can acheive this.
    thanks

  • Need of SQL query in selecting distinct values from two tables

    hi,
    I need a query for selecting distinct values from two tables with one condition.
    for eg:
    there are two tables a & b.
    in table a there are values like age,sex,name,empno and in table b valuses are such as age,salary,DOJ,empno.
    here what i need is with the help of empno as unique field,i need to select distinct values from two tables (ie) except age.
    can anybody please help me.
    Thanks in advance,
    Ratheesh

    Not sure what you mean either, but perhaps this will start a dialog:
    SELECT DISTINCT a.empno,
                    a.name,
                    a.sex,
                    b.salary,
                    b.doj
    FROM    a,
            b
    WHERE   a.empno = b.empno;Greg

  • Serious performance problem - SELECT DISTINCT x.JDOCLASSX FROM x

    I am noticing a huge performance problem when trying to access a member that
    is lazily loaded:
    MonitorStatus previousStatus = m.getStatus();
    This causes the following query to be executed:
    SELECT DISTINCT MONITORSTATUSX.JDOCLASSX FROM MONITORSTATUSX
    This table has 3 million records and this SQL statement takes 3 minutes to
    execute! Even worse, my app heavily uses threads, so this statement is
    executed in each of the 32 threads. As a result the application stops.
    Is there any way that I can optimize this? And more importantly, can Kodo
    handle a multithreaded app like this with a huge database? I've been having
    a lot of performance problems since I've started doing stress & load
    testing, and I'm thinking Kodo isn't ready for this type of application.
    Thanks,
    Michael

    You can prevent this from happening by explicitly enumerating the valid
    persistent types in a property. See
    http://docs.solarmetric.com/manual.html#com.solarmetric.kodo.PersistentTypes
    for details.
    >
    Inconveniently, this nugget of performance info is not listed in the
    optimization guide. I'll add in an entry for it.This setting did in fact prevent the query from running which fixed the
    problem. It definitely belongs in the optimization guide.
    And more importantly, can Kodo
    handle a multithreaded app like this with a huge database? I've beenhaving
    a lot of performance problems since I've started doing stress & load
    testing, and I'm thinking Kodo isn't ready for this type of application.I'd like to find out more information about details about your issues. We
    do a decent amount of stress / load testing internally, but there are
    always use cases that we don't test. Please send me an email (I'm assuming
    that [email protected] is not really your address) and let's
    figure out some way to do an analysis of what you're seeing.This email is just for posting to usenet, to avoid spam. I'm now running my
    app through stress/load testing so I hope to discover any remaining issues
    before going into production. As of this morning the system seems to be
    performing quite well. Now the biggest performance problem for me is the
    lack of what I think is called "outer join". I know you'll have this in 3.0
    but I'm suprised you don't have this already because not having it really
    affects performance. I already had to code one query by hand with JDBC due
    to this. It was taking 15+ minutes with Kodo and with my JDBC version it
    only takes a few seconds. There are lots of anti-JDO people and performance
    issues like this really give them ammunition. Overall I just have the
    impression that Kodo hasn't been used on many really large scale projects
    with databases that have millions of records.
    Thanks for configuration fix,
    Michael

  • Issue with "Select Distinct" query in Oracle 10g against Oracle 9i

    Hi,
    I would appreciate if some one help me here because it is really urgent.
    We are upgrading our database from 9i to 10g.
    There are the "Select distinct" queries in the code which populated the grid on the applications screens. We found a difference in 9i and 10g the way the result is populated for these queries. If "Select Distinct" query wihtout a order by clause is executed in 9i then the result is automatically sorted. But Oracle 10g does not do this.
    We can change the queries adding order by clause but we are almost at the end of the testing and want to know if there is any way that we can do this from database settings. Would there be any impact of these settings change on overall operation of Oracle 10g?
    I would appreciate if some one can help me here.
    Thanks,
    Dinesh

    then the result is automatically sorted.No. Oracle may have done a sort operation to perform the distinct, but it still did not guarantee the order of your results.
    In 10g and in 9i, if you want your results in a certain order you must use order by.

  • Select Distinct and join in ODI

    Hi,
    I have following task to perform: I am loading metadata into Planning dimension from Oracle database. I have two tables
    1. "Sales"
    Columns: Name, Number, Value
    Sample Data:
    Product 1, 10, 200
    Product 2, 30, 100,
    Product 1, 15, 500
    2. P&R
    Columns:
    Name, Alias
    Product 1, SampleSoda1
    Product 2, SampleSoda2,
    Resource 1, CanForSoda,
    Resource 2, CO2
    What I need to do is: I have to select name and alias from second table of all products that were sold.
    So I need to select distinct Name from Table 1, naxt join it with Table 2 (so I have Name and Alias) and load it to planning.
    I am a little confused how to do it.
    Any help would be great!
    Best regards,
    Greg

    Hi Greg,
    What you can do is either :
    - Create a yellow interface with your table Sales as source. Map the name column directly in the target. In the flow tab, click on your target and select "Distinct rows".
    - Create a second interface, with your first interface as source. Select the "Use Temporary Interface as Derived Table (Sub-Select)" checkbox.
    - Add your second datastore and join it. Or you can use a lookup table.
    OR
    - Create an interface with Sales and P&R as source (or set P&R as a lookup table).
    - Go on the flow tab and select "Distinct rows".
    If you've a lot a data in the first table, I would go for the first solution.
    Hope it helps.
    Regards,
    JeromeFr
    Edited by: JeromeFr on Feb 14, 2013 9:52 AM
    To be more clear in the first step of solution 1

  • Select distinct for a date column in present. service when creating prompt

    hi all
    I am trying to create some new prompts in my answers and I have the following problem. When I try to create a new prompt on a date column ( for example Modification Date), when I try to run this report on this prompt it is displaying the same date a lot of times ( for example 2/4/11 it is being displayed 4 times).
    Is there any way i can change this, so to display distinct dates?
    Thank you and best regards

    Hi Deva
    I am trying to use this, but in the system are being saved the date + hour.
    When i try select distinct modification date from My_view, i still have the same dates because they have different hours.
    When i try select disctinct to_date(modification_date)..... I am having an error in BI.
    Please help me to solve this issue.
    Regards

  • Select Distinct (More than one column)

    Hi All,
    I have the following SQL statement written in MySQL that returns only one row per MODCODE with the associated DEPTCODE of AE:
    SELECT DISTINCT
    EL_MODULE.DEPTCODE, EL_MODULE.MODCODE, EL_MODULE.MODNAME, EL_MODULE.MODLEVEL, EL_DEPTLEVEL.DEPTLEVELHEADER
    FROM EL_DEPTLEVEL, EL_MODULE
    WHERE EL_MODULE.MODLEVEL = EL_DEPTLEVEL.LEVELCODE AND EL_MODULE.DEPTCODE='AE' ORDER BY EL_MODULE.MODLEVEL
    However when I attempt to use this in oracle it returns three rows, could somebody point out what it is I need to change to get this working correctly in oracle.
    Hope someone can help.
    Jon

    Duplicates? No way! This query returns unique comibination of
    EL_MODULE.DEPTCODE,
    EL_MODULE.MODCODE,
    EL_MODULE.MODNAME,
    EL_MODULE.MODLEVEL and
    EL_DEPTLEVEL.DEPTLEVELHEADER
    Cheers
    Sarma.

  • Select distinct bug ?

    When using ‘insert into table2 select distinct field from table1′ and table2 contains a field with a default value sys_guid(), the distinct operator does not seem to work ! This was tested on Oracle 10.2.0.4 on 64 bit linux. See the following SQL code to prove it :
    create table table1 (field1 varchar2(100));
    insert into table1 (field1) values (‘value1′);
    insert into table1 (field1) values (‘value1′);
    insert into table1 (field1) values (‘value2′);
    – distinct and to_char(sysdate) function gives the correct 2 rows (‘value1′ and ‘value2′)
    create table table2 (field1 varchar2(100), field2 raw(16) default to_char(sysdate, ‘hh’));
    insert into table2(field1) select distinct field1 from table1;
    select * from table2;
    rollback;
    drop table table2 purge;
    – distinct and sys_guid function gives 3 rows (twice ‘value1′ !!) instead of 2
    create table table2 (field1 varchar2(100), field2 raw(16) default sys_guid());
    insert into table2(field1) select distinct field1 from table1;
    select * from table2;
    rollback;
    drop table table2 purge;
    – with group by : no problem
    create table table2 (field1 varchar2(100), field2 raw(16) default sys_guid());
    insert into table2(field1) select field1 from table1 group by field1;
    select * from table2;
    rollback;
    drop table table2 purge;
    drop table table1 purge;

    It looks like I can reproduce (on XE 10.2.0.1), with or without using a default for sys_guid:
    SQL> select banner from v$version where rownum=1;
    BANNER
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
    1 row selected.
    SQL> create table table1 (field1 varchar2(100));
    Table created.
    SQL> insert into table1 (field1) values ('value1');
    1 row created.
    SQL> insert into table1 (field1) values ('value1');
    1 row created.
    SQL> insert into table1 (field1) values ('value2');
    1 row created.
    SQL> -- distinct and to_char(sysdate) function gives the correct 2 rows (‘value1' and ‘value2')
    SQL> create table table2 (field1 varchar2(100), field2 raw(16) default to_char(sysdate, 'hh'));
    Table created.
    SQL> insert into table2(field1) select distinct field1 from table1;
    2 rows created.
    SQL> select * from table2;
    FIELD1     FIELD2
    value1     11
    value2     11
    2 rows selected.
    SQL> drop table table2 purge;
    Table dropped.
    SQL> -- distinct and sys_guid function gives 3 rows (twice ‘value1' !!) instead of 2
    SQL> create table table2 (field1 varchar2(100), field2 raw(16) default sys_guid());
    Table created.
    SQL> insert into table2(field1) select distinct field1 from table1;
    3 rows created.
    SQL> select * from table2;
    FIELD1     FIELD2
    value1     5AF743C0FD484D578E84276C875C5BC1
    value1     725ABE6066BF42E2A78406BD549D9E85
    value2     CC43E3C688AF428B885A0F4EA24482CB
    3 rows selected.
    SQL> drop table table2 purge;
    Table dropped.
    SQL> -- without using DEFAULT, here Max is right (see posts below)
    SQL> create table table2 (field1 varchar2(100), field2 raw(16));
    Table created.
    SQL> insert into table2(field1, field2) select distinct field1, sys_guid() from table1;
    3 rows created.
    SQL> select * from table2;
    FIELD1     FIELD2
    value1     7B277A2068AF468EBC06E33F6700B9E3
    value1     9644C1C8A909489F98B51531BDD282D9
    value2     8707FD2E6AB94EA7810D8D4AB19AB091
    3 rows selected.
    SQL> drop table table2 purge;
    Table dropped.
    SQL> -- with group by : no problem
    SQL> create table table2 (field1 varchar2(100), field2 raw(16) default sys_guid());
    Table created.
    SQL> insert into table2(field1) select field1 from table1 group by field1;
    2 rows created.
    SQL> select * from table2;
    FIELD1     FIELD2
    value2     202A87C86EF446D8915B3C976385D68F
    value1     484625A675524FA09CC393A78411B38A
    2 rows selected.
    SQL> drop table table2 purge;
    Table dropped.
    SQL> drop table table1 purge;
    Table dropped.Edited by: hoek on Feb 18, 2010 11:29 AM

  • Ora-00600 error on select distinct when using cursors

    Hi, we are using oracle9iFS version 9.0.1.1.0. When I execute the following query in sqlPlus an ora-00600 error occurs.
    Here is the query:
    SELECT     DISTINCT ODMV_FOLDER.NAME,
         CURSOR(SELECT ODMV_DOCUMENT.NAME
    FROM     ODMV_DOCUMENT
    WHERE     ODMV_DOCUMENT.ID = ODMV_FOLDERRELATIONSHIP.RIGHTOBJECT)as Document
    FROM     ODMV_FOLDER,
              ODMV_FOLDERRELATIONSHIP
    WHERE     ODMV_FOLDER.CREATOR = 96 /*this is the user 'system'*/
    AND (ODMV_FOLDER.ID = ODMV_FOLDERRELATIONSHIP.LEFTOBJECT)
    I have no clue why is this happening. I might be doing something wrong since a similar query with a nested cursor works with the scott/tiger schema. Your help is greatly appreciated. Thank you in advance.
    Alfonso.

    I was able to reproduce this error. This looks like a bug in the RDBMS; I would suggest posting on their newsgroup or working with support to handle this problem.

  • Select distinct is using the wrong index

    Hello,
    I have an indexes question?
    I have a table with 1M rows, the table structure is as follows:
    create table X(aTime DATE , a1 NUMBER(28,0), a2 NUMBER(28,0), a3 NUMBER(28,0), .... , a20 NUMBER(28,0) );
    I have also 2 indexes:
    - IDX1 is defined on (aTime, a1, a2, a3).
    - IDX2 is defined on (a1, a2, a3).
    I run the following query: "SELECT DISTINCT A1, A2, A3 from X;" and see that the optimizer chooses to do a FTS (full table scan) though I was expecting him to use IDX2 because it looks like a "covering" indexes giving all the answers to the query without fetching data rows.
    When I try to force him to use indexes, using the following hint "/*+INDEX(X IDX2)*/" the optimizer chooses IDX1.
    When I use same hint after I dropped IDX1 the optimizer still prefers to make FTS .
    Some general info:
    - a1, a2 and a3 have about 500 distinct values each).
    - db version 8.1.7.4.
    - all tables and indexes are analyzed.
    I have two questions:
    ===============
    1.
    Why does the optimizer prefers IDX1 (aTime, a1, a2, a3) over IDX2 (a1, a2, a3) when the query specifically requests distinct values of (aTime, a1, a2, a3), isn't there a shortcut the optimizer should use here?
    2.
    When I request the optimizer to use specific index (using hints), shouldn't it use the index I request regardless of his calculations? If not, is there any way to change this behavior?
    Tal Olier
    [email protected]

    You said you analyzed the tables and indexes. Did you also analyze the indexed columns?
    A full table scan may be a better execution plan than using an index, especially if you are selecting the full table.
    Remember that a hint is jut that, a hint. It suggests to the optimizer to do something, but the optimizer may still choose a different plan.

  • Something´s going wrong with select distinct (Oracle 10g)

    Our database is oracle 10g release 2 and the query statement is:
    select distinct last_name
    from students;
    and the query returns all of the last_names without an specific order..
    If I execute the same query in oracle 9i the query returns all of the last_name in alphabetic order.
    Why is it different on Oracle 10g?

    See also this blog entry from Mr. Kyte.
    C.

  • Help! Howto use the join function in a query with select distinct ?

    Hi!
    I have 2 tables. I want to select only 1 painting of each artists.
    select distinct idartist
    from tbl_artworks
    where blah blah blah
    order by rand()
    how does the "join" function work for add: name, lastname, title, image and much more... i try... but i fail...
    tbl_artists
    idartist
    name
    lastname
    1
    Paul
    Gaugain
    2
    Vincent
    Van Gogh
    3
    Pablo
    Picasso
    tbl_artworks
    idartwork
    idartist
    title
    image
    1
    1
    days of gods
    image1.jpg
    2
    2
    sunflower
    image2.jpg
    3
    3
    Dora maar au chat
    image3.jpg
    4
    2
    Sky
    image4.jpg
    5
    3
    La vie
    image5.jpg

    Getting a single random image for each probably requires a combination of sql and cf.  It would take someone smarter than me to do it with sql alone.  I would probably try something like this:
    1.  Run a database query that gets all the images from all the artists.
    2.  Run a Q of Q that gets a distinct list of artist ids.
    3.  Loop through that list and run a Q of Q to get all the images for that artist.
    4.  Still in that loop, use randrange (1 to the recordcount) to select a random record from your Q of Q

Maybe you are looking for

  • BT infinity what a laugh after 15 months still get...

    I am a very unhappy with this and very stressed, Let me explain from the start I decided in may 2012 to try BT infinity, as was with sky and contract had ended. so BT where very helpful to start. everything was ordered, on the day engineers came to i

  • Connect a UIView with a UIViewController

    How can I connect a UIView subclass with a UIViewController subclass with code? I know how associate a nib file with a UIViewController, but it would be nice not having to create nib files for all my view controller classes, and just create everythin

  • Regarding end user permission

    Hi Gurus, I have three iviews (v1,v2,v3)assigned to a role(RoleAll) which will be assigned to user. The requirement is: certain user can only see certain iviews. my notion is: another three roles(role1, role2, role3) created, and set the iviews' end

  • Missed place text on upload to iWeb

    I'm using a LOT of my upload allotment trying to get around what I consider a "bugs" in iWeb. Text I insert into iWeb looks fine on the iWeb page but when uploaded, it grows dramatically in size, distorts, moves itself around the page, makes other te

  • Where is Shuffle feature in Itunes 11 or how do I downgrade back to Itunes 10?

    Upgraded to Itunes 11 and now the SHUFFLE feature won't work with my playlists.  How to I get it to work or how do I downgrade to earlier version of Itunes.  Not happy with Apple right now...  Any help is much appreciated!