WITH Subquery Factoring OR "Scalar SubqueriesRun Another Query per row

Hi Experts
Please suggest
which one query is better
Involved table p is of size 2GB and c1/c2 of size 800M.
And we have to run a report on p which will do FTS (no where clause on p)
So FTS on 2 GB table and then running Another Query per row .. so results job very slow
1)  Select p.id,
         (select sum(q1) from c1 where c1.id = p.id) c1_sum1,
         (select sum(q2) from c2 where c2.id = p.id) c2_sum2
   from p
2)
WITH Subquery Factoring
with c1_vw as
(select id, sum(q1) c1_sum1
    from c1
         group by id),
   c2_vw as
   (select id, sum(q2) c2_sum2
       from c2
      group by id),
   c1_c2 as
   (select c1.id, c1.c1_sum1, c2.c2_sum2
       from c1_vw c1, c2_vw c2
      where c1.id = c2.id )
   select p.id, c1_sum1, c2_sum2
      from p, c1_c2
     where p.id = c1_c2.id
   / 10.2..0.4
AIX 5.3
Thanks In Advance
ivw

ivw wrote:
which one query is betterThe better query IMHO is the one that returns the the correct results in the shortest amount of time using the least system resources (the last two items usually happen at the same time). Maintainability is an issue too. Which one do you like best?
Its hard to say which query will run best without running both, getting execution plans, and run-time statistics. At a pure guess I would think all things being equal they would have similar performance but do not really know.

Similar Messages

  • Ordering with Subquery Factoring

    Can I be assured that the results of this query will be ordered as defined by the 'order siblings by' clause?
    with hier as
       select ...
       from ...
       start with ...
       connect by ...
       order siblings by ...
    select ...
    from hier
    ;How about this one?
    with hier as
       select ...
       from ...
       start with ...
       connect by ...
       order siblings by ...
       subqry as
       select ...
       from ...
       where ...
    select ...
    from
       hier
       inner join subqry using ...
    ;I'm thinking I need something like this to ensure the proper order.
    with hier as
       select
          rownum rnum
       from ...
       start with ...
       connect by ...
       order siblings by ...
    select ...
    from hier
    order by rnum
    ;If not needed for the first query, is the above technique needed for the second?
    Thanks.

    It is true, the plan is changing but it is not really needed.
    Take a look at my queries and execution plans using the sample schema HR:
    with h as (
         select employee_id, first_name, last_name, department_id, manager_id
         from hr.employees
         start with manager_id is null
         connect by manager_id = prior employee_id
         order siblings by last_name
    select h.employee_id, h.first_name, h.last_name, h.department_id, h.manager_id, d.department_name
    from h join hr.departments d on d.department_id = h.department_id;
    | Id  | Operation                                  | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT                           |             |    20 |  1900 |     4  (25)| 00:00:01 |
    |   1 |  NESTED LOOPS                              |             |       |       |            |          |
    |   2 |   NESTED LOOPS                             |             |    20 |  1900 |     4  (25)| 00:00:01 |
    |   3 |    VIEW                                    |             |    20 |  1300 |     4  (25)| 00:00:01 |
    |*  4 |     CONNECT BY NO FILTERING WITH START-WITH|             |       |       |            |          |
    |   5 |      TABLE ACCESS FULL                     | EMPLOYEES   |    20 |  1300 |     3   (0)| 00:00:01 |
    |*  6 |    INDEX UNIQUE SCAN                       | DEPT_ID_PK  |     1 |       |     0   (0)| 00:00:01 |
    |   7 |   TABLE ACCESS BY INDEX ROWID              | DEPARTMENTS |     1 |    30 |     0   (0)| 00:00:01 |
    with h as (
         select employee_id, first_name, last_name, department_id, manager_id, rownum
         from hr.employees
         start with manager_id is null
         connect by manager_id = prior employee_id
         order siblings by last_name
    select h.employee_id, h.first_name, h.last_name, h.department_id, h.manager_id, d.department_name
    from h join hr.departments d on d.department_id = h.department_id
    order by rownum;
    | Id  | Operation                                     | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT                              |             |    20 |  1900 |     5  (40)| 00:00:01 |
    |   1 |  SORT ORDER BY                                |             |    20 |  1900 |     5  (40)| 00:00:01 |
    |   2 |   COUNT                                       |             |       |       |            |          |
    |   3 |    NESTED LOOPS                               |             |       |       |            |          |
    |   4 |     NESTED LOOPS                              |             |    20 |  1900 |     4  (25)| 00:00:01 |
    |   5 |      VIEW                                     |             |    20 |  1300 |     4  (25)| 00:00:01 |
    |   6 |       COUNT                                   |             |       |       |            |          |
    |*  7 |        CONNECT BY NO FILTERING WITH START-WITH|             |       |       |            |          |
    |   8 |         TABLE ACCESS FULL                     | EMPLOYEES   |    20 |  1300 |     3   (0)| 00:00:01 |
    |*  9 |      INDEX UNIQUE SCAN                        | DEPT_ID_PK  |     1 |       |     0   (0)| 00:00:01 |
    |  10 |     TABLE ACCESS BY INDEX ROWID               | DEPARTMENTS |     1 |    30 |     0   (0)| 00:00:01 |
    -------------------------------------------------------------------------------------------------------------

  • Query with subquery containing group clause doesn't return any rows - WHY ?

    Hi,
    My query doesn't return any values :
    select g1.NTRX from gtrx g1
    where exists
    (SELECT b.cfunctrx, b.cpro1trx, b.nmsgitrx, b.nmrc, b.ncrd, b.namtstrx,
    b.dltimtrx, b.nrtrftrx,count(*)
    FROM gtrxacq a, gtrx b
    WHERE a.ntrx = b.ntrx AND a.acq_bus_date = (SELECT curr_bus_date -1
    FROM gmbr
    WHERE nmbr = 0)
    and g1.NTRX=b.NTRX
    GROUP BY b.cfunctrx,
    b.cpro1trx,
    b.nmsgitrx,
    b.nmrc,
    b.ncrd,
    b.namtstrx,
    b.dltimtrx,
    b.nrtrftrx
    HAVING COUNT (*) > 1);
    but such query returns some number of rows :
    SELECT b.cfunctrx, b.cpro1trx, b.nmsgitrx, b.nmrc, b.ncrd, b.namtstrx,
    b.dltimtrx, b.nrtrftrx,count(*)
    FROM gtrxacq a, gtrx b
    WHERE a.ntrx = b.ntrx AND a.acq_bus_date = (SELECT curr_bus_date -1
    FROM gmbr
    WHERE nmbr = 0)
    /*and g1.NTRX=b.NTRX*/
    GROUP BY b.cfunctrx,
    b.cpro1trx,
    b.nmsgitrx,
    b.nmrc,
    b.ncrd,
    b.namtstrx,
    b.dltimtrx,
    b.nrtrftrx
    HAVING COUNT (*) > 1
    AND when i put results from query above into query :
    select g1.NTRX from gtrx g1
    where
    g1.CFUNCTRX= 200 and g1.CPRO1TRX= 000 and g1.NMSGITRX= 1240 and
    g1.NMRC= '000000000000675' and g1.NCRD= 405671**********
    and g1.NAMTSTRX=14.26 and g1.DLTIMTRX=to_date('07/08/2008 15:07:02','MM/DD/YYYY HH24:MI:SS')
    and g1.NRTRFTRX= '000414598393';
    it returns values.
    what is wrong ?
    Best Regards Arkadiusz Masny

    but such query returns some number of rows :
    /*and g1.NTRX=b.NTRX*/Add b.NTRX into group by and recheck.

  • How to use subquery factoring ("with" clause) in OWB?

    Hi,
    Is it possible to use subquery factoring (popularly known as "with" clause) in OWB 10gR2? I have a mapping with a splitter and union-all, which generates a query that has repeated scans of the same table. Subquery Factoring would be very useful here. I think this is a very common situation, so, I hope there's a way to achieve this. (If not in this version, this would be my wishlist item for the next version.)
    Appreciate your help.
    Regards,
    Rahul

    Hi Rahul,
    I'm afraid you have to put this on your wishlist. You may put the query with the "with"-clause into a view (or table function if you need parameters) if performance is too bad. But that is somehow against the idea (and benefits) of owb.
    Another possibility is to use a temporary table and spilt the mappings into two parts: first fill the temp table, then the target table.
    Regards,
    Carsten.

  • Bug in WITH clause (subquery factoring clause) in Oracle 11?

    I'm using WITH to perform a set comparison in order to qualify a given query as correct or incorrect regarding an existing solution. However, the query does not give the expected result - an empty set - when comparing the solution to itself in Oracle 11 whereas it does in Oracle 10. A minimal example os posted below as script. There are also some observations about changes to the tables or the query that make Oracle 11 returning correct results but in my opinion these changes must not change the semantics of the queries.
    Is this a bug or am I getting something wrong? The Oracle versions are mentioned in the script.
    -- Bug in WITH clause (subquery factoring clause)
    -- in Oracle Database 11g Enterprise Edition 11.2.0.1.0?
    DROP TABLE B PURGE;
    DROP TABLE K PURGE;
    DROP TABLE S PURGE;
    CREATE TABLE S (
         m     number NOT NULL,
         x     varchar2(30) NOT NULL
    CREATE TABLE K (
         k char(2) NOT NULL,
         x varchar2(50) NOT NULL
    CREATE TABLE B (
         m     number NOT NULL ,
         k char(2) NOT NULL ,
         n     number
    INSERT INTO S VALUES(1, 'h');
    INSERT INTO S VALUES(2, 'l');
    INSERT INTO S VALUES(3, 'm');
    INSERT INTO K VALUES('k1', 'd');
    INSERT INTO K VALUES('k2', 'i');
    INSERT INTO K VALUES('k3', 'm');
    INSERT INTO K VALUES('k4', 't');
    INSERT INTO K VALUES('k5', 't');
    INSERT INTO K VALUES('k6', 's');
    INSERT INTO B VALUES(1, 'k1', 40);
    INSERT INTO B VALUES(1, 'k2', 30);
    INSERT INTO B VALUES(1, 'k4', 50);
    INSERT INTO B VALUES(3, 'k1', 10);
    INSERT INTO B VALUES(3, 'k2', 20);
    INSERT INTO B VALUES(3, 'k1', 30);
    INSERT INTO B VALUES(3, 'k6', 90);
    COMMIT;
    ALTER TABLE S ADD CONSTRAINT S_pk PRIMARY KEY (m);
    ALTER TABLE K ADD CONSTRAINT K_pk PRIMARY KEY (k);
    ALTER TABLE B ADD CONSTRAINT B_S_fk
    FOREIGN KEY (m) REFERENCES S(m) ON DELETE CASCADE;
    CREATE OR REPLACE VIEW v AS
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC;
    -- Query 1: Result should be 0
    WITH q AS
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC
    SELECT COUNT(*)
    FROM
    SELECT * FROM q
    MINUS
    SELECT * FROM v
    UNION ALL
    SELECT * FROM v
    MINUS
    SELECT * FROM q
    -- COUNT(*)
    -- 6
    -- 1 rows selected
    -- Query 2: Result set should be empty (Query 1 without counting)
    WITH q AS
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC
    SELECT *
    FROM
    SELECT * FROM q
    MINUS
    SELECT * FROM v
    UNION ALL
    SELECT * FROM v
    MINUS
    SELECT * FROM q
    -- M N
    -- null 10
    -- null 30
    -- null 40
    -- 1 40
    -- 3 10
    -- 3 30
    -- 6 rows selected
    -- Observations:
    -- Incorrect results in Oracle Database 11g Enterprise Edition 11.2.0.1.0:
    -- Query 1 returns 6, Query 2 returns six rows.
    -- Correct in Oracle Database 10g Enterprise Edition 10.2.0.1.0.
    -- Correct without the foreign key.
    -- Correct if attribute x is renamed in S or K.
    -- Correct if attribute x is left out in S.
    -- Correct without the ORDER BY clause in the definition of q.
    -- Only two results if the primary key on K is left out.
    -- Correct without any change if not using WITH but subqueries (see below).
    -- Fixed queries
    -- Query 1b: Result should be 0
    SELECT COUNT(*)
    FROM
    SELECT * FROM
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC
    MINUS
    SELECT * FROM v
    UNION ALL
    SELECT * FROM v
    MINUS
    SELECT * FROM
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC
    -- COUNT(*)
    -- 0
    -- 1 rows selected
    -- Query 2b: Result set shoud be empty (Query 1b without counting)
    SELECT *
    FROM
    SELECT * FROM
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC
    MINUS
    SELECT * FROM v
    UNION ALL
    SELECT * FROM v
    MINUS
    SELECT * FROM
    SELECT S.m, B.n
    FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
    WHERE K.x='d'
    ORDER BY B.n DESC
    -- M N
    -- 0 rows selected

    You're all gonna love this one.....
    The WITH clause works. But not easily.
    Go ahead, build the query, (as noted in a recent thread, I, too, always use views), set the grants and make sure DISCOVERER and EULOWNER have SELECT privs.
    1. Log into Disco Admin as EULOWNER. Trust me.
    2. Add the view as a folder to the business area.
    3. Log into Disco Desktop as EULOWNER. Don't laugh. It gets better.
    4. Build the workbook and the worksheet (or just the worksheet if apropos)
    5. Set the appropriate "sharing" roles and such
    6. Save the workbook to the database.
    7. Save the workbook to your computer.
    8. Log out of Desktop.
    9. Log back into Desktop as whatever, whoever you usually are to work.
    10. elect "open existing workbook"
    11. Select icon for "open from my computer". See? I told you it would get better!
    12. Open the save .dis file from your computer.
    13. Save it to the database.
    14. Open a web browser and from there, you're on your own.
    Fortran in VMS. Much easier and faster. I'm convinced the proliferation of the web is a detriment to the world at large...On the other hand, I'm also waiting for the Dodgers to return to Brooklyn.

  • Recursive WITH (Recursive Subquery Factoring) Never Returns

    11.2.0.2 database on Windows, SQL Developer Version 3.2.20.09, build MAIN-09.87 (Database and SQL Developer are on the same machine. I have also tried connecting to a Linux 11.2 database and have the same results.)
    I've been doing some simple testing with recursive WITH (Recursive Subquery Factoring) and when I run this following statement in SQL*Plus it returns instantly. However when running in SQL Developer it never returns, I've let it run for quite a long time (172 seconds) and gotten nothing, I finally kill the statement. Once I ran it and even killing the job didn't come back. I can get an explain plan but if I try to run it, run as script or autotrace it never returns. I have only one plan in the plan_table for this test, and it's only 4 lines long. No errors, no messages.
    WITH get_plan (query_plan, id, planlevel) as
    select ' '||operation||' '||options||' '||object_name query_plan, id, 1 planlevel
    from plan_table
    where id = 0
    union all
    select lpad(' ',2*planlevel)||p.operation||' '||p.options||' '||p.object_name query_plan, p.id, planlevel+1
    from get_plan g, plan_table p
    where g.id = p.parent_id
    SELECT QUERY_PLAN FROM GET_PLAN ORDER BY PLANLEVEL;

    Hi Jeff, using either give the same results. The query is "running", as is the little graphic with the bouncing gray bar is moving back and forth saying either "Query Results" or "Scriptrunner Task" as appropriate.
    OK this is odd. I run a count(*) on plan_table in SQL*Plus and get 4, in SQL Developer I get 487. Hun? That makes no sense I'm connect as the same user in each. Where are all these other entries coming from and why can't I see them in SQL Plus? Does SQL Developer have it's own PLAN_TABLE?
    **EDIT --- Yes that seems to be the case. The PLAN_ID I see in SQL Plus doesn't even exist in the SQL Deveropler version of the table. OK that's good to know. I assume the plan_table for SQL Developer is local to it somehow? It's not in the database as best I can see.
    Edited by: Ric Van Dyke on Feb 7, 2013 5:19 PM

  • With clause of select (subquery factoring)

    I've got a script that looks like this:
    with X as (select * from atable)
    select x1.*, x2.*
    from X x1,
    X x2
    This works perfectly well in SQL*Plus, in PL/SQL, etc. Yet when I try to add it as "New Folder from Database" to an existing Business area in Discoverer, I get the infamous "ORA-03001 Unimplemented Feature".
    What am I doing wrong?
    Oracle 9i, Disco 9.0.4

    Hi,
    You will have to define a database view containing your query and load the view into your database. I don't think you can use subquery factoring in a custom folder.
    Rod West

  • Query with subquery should return value but doesn't

    When I run this SQL, it returns no value:
    SELECT vfn.cat
    FROM vps_fishery_ner vfn, valid_fishery vf
    WHERE vfn.plan = vf.plan
    AND vfn.cat = vf.cat
    AND vf.permit_year = 2010
    AND vf.moratorium_fishery = 'T'
    AND vfn.vp_num = 211652
    AND vfn.ap_year = 2010
    AND vfn.plan = 'MUL'
    AND vfn.date_issued = (SELECT MAX(date_issued)
    FROM vps_fishery_ner
    WHERE vp_num = 211652
    AND ap_year = 2010);
    In order to test, I take out the subquery and run it separately:
    SELECT MAX(date_issued)
    FROM vps_fishery_ner
    WHERE vp_num = 211652
    AND ap_year = 2010;
    Returns 02-APR-10
    Then I paste this date into the original query (using the TRUNC function, of course, since I hardcode only the DDMMYY part of the date):
    SELECT vfn.cat
    FROM vps_fishery_ner vfn, valid_fishery vf
    WHERE vfn.plan = vf.plan
    AND vfn.cat = vf.cat
    AND vf.permit_year = 2010
    AND vf.moratorium_fishery = 'T'
    AND vfn.vp_num = 211652
    AND vfn.ap_year = 2010
    AND vfn.plan = 'MUL'
    AND TRUNC(date_issued) = TO_DATE('02-APR-10');
    And this returns the required value, 'A'.
    So why doesn't the full query with subquery work, if the value that is returned by the subquery is valid and works when you just paste it in?
    Thanks.

    Hi,
    Not sure about your question.
    But you say when you uss 01-apr-10 you get the expected results.
    So why dont you try using trunc on botht sides
    SELECT vfn.cat
      FROM vps_fishery_ner vfn, valid_fishery vf
    WHERE vfn.plan = vf.plan
       AND vfn.cat = vf.cat
       AND vf.permit_year = 2010
       AND vf.moratorium_fishery = 'T'
       AND vfn.vp_num = 211652
       AND vfn.ap_year = 2010
       AND vfn.plan = 'MUL'
       AND trunc(vfn.date_issued) = (SELECT MAX(trunc(date_issued))
                                FROM vps_fishery_ner
                               WHERE vp_num = 211652
                                 AND ap_year = 2010);Rememeber if you are couting on some Index to be used you might want to recheck.
    IN answer to your question why it does not return with subquery included, because the TIME PART is not the same.
    You yourself proved it by using the supplying only the date part.
    Regards,
    Bhushan

  • Is it possible to update a query with another query?

    I'm trying to update a query with another query (see attached
    code). Here's my setup: I've got a table in an Access database in
    which I enter a string into a form and update. This string
    corresponds to a single record in another table of the same
    datasource. The first table has only one record to provide the
    second, which has many and will have more. Basically what I'm
    wondering is: Is this a valid thing to do in coldfusion? If not
    please help with an alterate method. I'm still a novice at
    coldfusion.
    The overall effect I'm going for is to display the one record
    as a featured truck profile on the web site:
    www.truckerstoystore.net.
    I currently get an error when I try to display the page with the
    current query setup.
    Check this page to see the error:
    www.truckerstoystore.net/currentTOW2.cfm
    Help on this issue is very much appreciated.
    ------------------------------------------------------------------------------------------ -----------------------------------------------------------------------

    I think this is what you are after
    <!--- this query will get all the records from the DB
    --->
    <cfquery name="cTOW" datasource="tow">
    SELECT *
    FROM currentTOW
    <!--- Do you need to find a particular record in the
    database --->
    <!--- If so, then you need a 'where' clause in here
    --->
    </cfquery>
    <!-- Loop the cTOW query for each record returned -->
    <cfloop query="cTOW">
    <!--- For the record returned from the cTOW query you now
    need to update the table --->
    <!-- Update the table -->
    <cfquery name="currentTOW" datasource="tow">
    UPDATE Your tblName
    SET
    Dataname = cTOW.DataValue
    </cfquery>
    </cfloop>
    thats it
    PS: I think your original query needs modifying. To return
    the exact records that you want to update from the original table.
    ie: Primary and foreign key relationship

  • Subquery factoring with "with" - slow

    I have a query of the form
    with t1 as ( select-query-with-grouping )
    , t2 as (other-query-with-grouping )
    , t3 as (another-query-with-grouping )
    select * from t1
    fulll outer join t2 on t1.KeyColumn = t2.KeyColumn
    full outer join t3 on t1.KeyColumn = t3.KeyColumn or t2.KeyColumn = t3.KeyColumn
    Each of the 3 grouped select queries runs very quickly (under a second) and returns about 800 rows.
    But in combination, the query takes about a minute to return, also about 800 rows.
    If I select the data into temp tables and then join against that, it is very fast. About a second.
    Question is - shouldn't i be seeing about the same performance using "with ..." as with temp tables?
    I tried applying the "materialize" hint to the inner queries, but it had no effect.
    Any suggestions about how i can make "With" behave like using temp tables?
    Thanks in advance
    Edited by: 854248 on Apr 22, 2011 7:11 PM

    Thanks for your reply.
    The inner queries do use inner joins to an other table common table. There is nothing that can be moved outside though.
    I do see that if I omit either of the pieces of the OR in the second outer join - that is, just join one or the other of t1 or t2, then the query is quite fast (and returns just a few more rows). But, the point of this query is to get all the rows from N tables that share a common key column. And I think that means that as each tX is joined, it has to be t1.key = tX.key or t2.key = tX.key or t3.key=tX.key ... That's how we write the query in SqlServer 2k5/8 (also using the "with" syntax) where it is quite fast with this same data set.
    From my reading about the "with" feature, the oracle optimizer should "materialize" the tN pieces of the query using some sort of in memory temp table like approach. It appears that it is either not doing that, or whatever it is doing is performing very badly. As i say, if I create tN global temporary tables from these pieces and then do the full outer join, it is very fast. I wonder if that is a 10g/11g feature, or a limitation of my 10g express edition or something. That said, I see the same trend in a different data set on an 11g standard edition system that we use in QA. I have tried adding the /*+ materialize */ hint to both systems, to no avail.

  • DB polling : query with subquery

    Hi,
    We are upgrading the 10g code to 11g, where one process has the DB polling with custom SQQL. This SQL has a subquery like,
    Select Name, ID, CID from tabl1 where lastdate + (select num_value from table2 where column2 = "xxx") <= sysdate
    We want the similar functionality in 11g. The upgraded code generated few artifiacts like toplink. We have bulilt that and deployed to server. But it is not polling the data eventhough the matching records are present in the tables.
    Please let us know how to fix this issue.
    Thanks,
    Vinoth

    Hi,
    Not sure about your question.
    But you say when you uss 01-apr-10 you get the expected results.
    So why dont you try using trunc on botht sides
    SELECT vfn.cat
      FROM vps_fishery_ner vfn, valid_fishery vf
    WHERE vfn.plan = vf.plan
       AND vfn.cat = vf.cat
       AND vf.permit_year = 2010
       AND vf.moratorium_fishery = 'T'
       AND vfn.vp_num = 211652
       AND vfn.ap_year = 2010
       AND vfn.plan = 'MUL'
       AND trunc(vfn.date_issued) = (SELECT MAX(trunc(date_issued))
                                FROM vps_fishery_ner
                               WHERE vp_num = 211652
                                 AND ap_year = 2010);Rememeber if you are couting on some Index to be used you might want to recheck.
    IN answer to your question why it does not return with subquery included, because the TIME PART is not the same.
    You yourself proved it by using the supplying only the date part.
    Regards,
    Bhushan

  • Issue with Results from Another Query (Error on Null value)

    Hi All,
    We have a WebI report using "Result from Another Query" option of BO XI R3.1. The report was running fine till recently the dimension object using result from another query had a null value. Report suddenly throwed error as the query filters are invalid.
    Is there a way to make this filter optional if no data/null value is there ? Because we need those null values in report as well.
    Thank you for your time.
    Thanks & Regards
    LN

    Hi Vivek,
    It was not directly solved but I applied alternate logic to over come the issue.
    Here's what I did to overcome:
    I used a sub query in place of the whole result from another query.
    For Ex:
    Dim1 inlist result from another query1
    I made it as
    Dim1 inlist (Dim0)
    where Conditions.
    Here Dim0 is the object which we use for Result from another query and Conditions will be the necessary filter conditions to arrive proper Dim0.  Make sure proper context is formed for the sub query.
    Even though it resolved my problem, It introduces an new issue. It causes increase in query run time when huge set of data is returned from sub query.
    Please let me know if i haven't explained clearly.
    Hi Aris_BO,
    Sorry for not responding earlier.  The logic would probably make more queries null & not null. Thats why I was not advised to use it.
    Thanks
    LN

  • Subquery Factoring and Materialized Hint

    WITH t AS
            (SELECT MAX (lDATE) tidate
               FROM rate_Master
              WHERE     Code = 'G'
                    AND orno > 0
                    AND TYPE = 'L'
                    AND lDATE <= ':entereddate')
    SELECT DECODE (:p1,  'B', RateB,  'S', RateS,  Rate)
      FROM rate_Master, t
    WHERE     Code = 'G'
           AND orno > 0
           AND TYPE = 'L'
           AND NVL (lDATE, SYSDATE) = tidate;In the given example the sub query returns just one row because of the aggregate function max. Making this in to a With clause will be of any benefit ? Also i presume/understand that the subquery factoring would be really useful only when we try to make a sub query which returns more rows in a with clause. Is my intrepration right?
    Secondly adding the /*+ Materialize */ hint to a With query is mandatory or the optimizer by itself will do it and make a temp table transformation. In my example i am forced to give the hint in the query. Please discuss and help
    Thanks in advance.

    ramarun wrote:
    WITH t AS
    (SELECT MAX (lDATE) tidate
    FROM rate_Master
    WHERE     Code = 'G'
    AND orno > 0
    AND TYPE = 'L'
    AND lDATE <= ':entereddate')
    SELECT DECODE (:p1,  'B', RateB,  'S', RateS,  Rate)
    FROM rate_Master, t
    WHERE     Code = 'G'
    AND orno > 0
    AND TYPE = 'L'
    AND NVL (lDATE, SYSDATE) = tidate;In the given example the sub query returns just one row because of the aggregate function max. Making this in to a With clause will be of any benefit ? Also i presume/understand that the subquery factoring would be really useful only when we try to make a sub query which returns more rows in a with clause. Is my intrepration right?I am not aware of any performance Benefit due to use of With clause. IMO, It eases the job to write a Subquery multiple times in a query.
    The solution you adopted has to hit the cache twice and hence do not look very performant. I will advise you to opt for Analytic functions (like the suggestion I provided in another thread). If the solution does not yeild correct results, then provide with a Script that we can replicate (Create table, Sample Insert statement and the expected output).
    select decode(:p1, 'B', RateB, 'S', RateS, Rate)
       from (
                select RateB, RateS, Rate, NVL(ldate, sysdate) ldate, dense_rank() over (order by case when NVL(lDATE, SYSDATE) <= ':entereddate' then NVL(lDATE, SYSDATE) else to_date('01/01/1970', 'DD/MM/YYYY' end DESC) rn
                  from rate_Master
               where Code = 'G'
                   and orno > 0
                   and type = 'L'
             ) a
      where a.rn = 1;>
    Secondly adding the /*+ Materialize */ hint to a With query is mandatory or the optimizer by itself will do it and make a temp table transformation. In my example i am forced to give the hint in the query. Please discuss and help
    Usage of Hints is only for Debugging purposes and is not meant to be used in production code. It is when you have to ascertain the reason for CBO choosing a plan that you do not expect it to take, you use hints to force your plan and find the cost and analyze it. Hence, I do not support the idea of Hints for production code.

  • Subquery factoring and DB Link

    Hello,
    2 node RAC
    Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
    PL/SQL Release 10.2.0.5.0 - Production
    CORE    10.2.0.5.0      Production
    TNS for Linux: Version 10.2.0.5.0 - Production
    NLSRTL Version 10.2.0.5.0 - ProductionI have a query of the form..
    WITH temp as
                 (select col1, col2, col3 from LOCAL_TABLE)
               SELECT /*+ driving_site (remote1) */
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote2.col1,
                 remote2.col1,
                 remote2.col1,
                 remote2.col1,
                 remote2.col1,
                 null,
                 null
              from remote_view1@dblink remote1, -- Remote View over 2 dbs
                     remote_view2@dblink remote2  -- Remote View over 2 dbs
               where remote1.col1 in (select col1 from temp);This query used to work, but recently there has been a change in remote_view1 after which the query takes a long time to execute.
    The good performing query takes 2 secs, while the bad performing one takes about 8 minutes.
    However, if I remove the subquery factoring and include the query as a sub-query it works well.
               SELECT /*+ driving_site (remote1) */
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote1.col1,
                 remote2.col1,
                 remote2.col1,
                 remote2.col1,
                 remote2.col1,
                 remote2.col1,
                 null,
                 null
              from remote_view1@dblink remote1, -- Remote View over 2 dbs
                     remote_view2@dblink remote2  -- Remote View over 2 dbs
               where remote1.col1 in  (select col1, col2, col3 from LOCAL_TABLE);I tried to check the data from v$sql. I observed that;
    v$sql.remote = 'Y' - good performance
    v$sql.remote = 'N' - bad performance
    Has anyone faced this situation ?
    Rgds,
    Gokul

    Am I right in assuming that queries executed entirely on remote db don't have plans stored in v$sql_planRight.
    SQL> select /*+ domtest */ 1 from dual@domtest;
             1
             1
    SQL> select * from table(dbms_xplan.display_cursor);
    PLAN_TABLE_OUTPUT
    SQL_ID  9d51rf84zvt0s, child number 0
    select /*+ domtest */ 1 from dual@domtest
    NOTE: cannot fetch plan for SQL_ID: 9d51rf84zvt0s, CHILD_NUMBER: 0
          Please verify value of SQL_ID and CHILD_NUMBER;
          It could also be that the plan is no longer in cursor cache (check v$sql_plan)
    8 rows selected.
    SQL> select sql_id, remote, sql_text from v$sql where sql_id = '9d51rf84zvt0s';
    SQL_ID        R SQL_TEXT
    9d51rf84zvt0s Y select /*+ domtest */ 1 from dual@domtest
    SQL> select * from v$sql_plan where sql_id = '9d51rf84zvt0s';
    no rows selected
    SQL> select /*+ domtest */ 1 from dual@domtest t, dual;
             1
             1
    Elapsed: 00:00:00.00
    SQL> select * from table(dbms_xplan.display_cursor);
    PLAN_TABLE_OUTPUT
    SQL_ID  d0m08znks4yak, child number 0
    select /*+ domtest */ 1 from dual@domtest t, dual
    Plan hash value: 3754369022
    | Id  | Operation            | Name | Rows  | Cost (%CPU)| Time     | Inst   |IN-OUT|
    |   0 | SELECT STATEMENT     |      |       |     4 (100)|          |        |      |
    |   1 |  MERGE JOIN CARTESIAN|      |     1 |     4   (0)| 00:00:01 |        |      |
    |   2 |   REMOTE             | DUAL |     1 |     2   (0)| 00:00:01 | DOMTE~ | R->S |
    |   3 |   BUFFER SORT        |      |     1 |     2   (0)| 00:00:01 |        |      |
    |   4 |    FAST DUAL         |      |     1 |     2   (0)| 00:00:01 |        |      |
    Remote SQL Information (identified by operation id):
       2 - SELECT 0 FROM "DUAL" "T" (accessing 'DOMTEST' )
    22 rows selected.
    Elapsed: 00:00:00.04
    SQL> select sql_id, remote, sql_text from v$sql where sql_id = 'd0m08znks4yak';
    SQL_ID        R SQL_TEXT
    d0m08znks4yak N select /*+ domtest */ 1 from dual@domtest t, dua
    SQL> select /*+ driving_site(t) */ 1 from dual@domtest t, dual;
             1
             1
    Elapsed: 00:00:00.00
    SQL> select * from table(dbms_xplan.display_cursor);
    PLAN_TABLE_OUTPUT
    SQL_ID  741u736nrk6dx, child number 0
    select /*+ driving_site(t) */ 1 from dual@domtest t, dual
    NOTE: cannot fetch plan for SQL_ID: 741u736nrk6dx, CHILD_NUMBER: 0
          Please verify value of SQL_ID and CHILD_NUMBER;
          It could also be that the plan is no longer in cursor cache (check v$sql_plan)
    8 rows selected.
    Elapsed: 00:00:00.01
    SQL> select sql_id, remote, sql_text from v$sql where sql_id = '741u736nrk6dx';
    SQL_ID        R SQL_TEXT
    741u736nrk6dx Y select /*+ driving_site(t) */ 1 from dual@domtest
                    t, dual
    Elapsed: 00:00:00.00
    SQL>
    SQL> select * from v$sql_plan where sql_id = '741u736nrk6dx';
    no rows selected
    Elapsed: 00:00:00.01
    SQL> Edited by: Dom Brooks on Aug 21, 2012 1:05 PM

  • Slow connect by prior ... start with subquery in 9i

    Has anyone come across a performance problem (compared to 8i) when using hierarchical queries where the START WITH list is generated by a subquery? The culprit seems to be an extra visit to the subquery block as part of the CONNECT BY WITH FILTERING operation.
    For example, take a simple tree structure:
    CREATE TABLE tree
    id NUMBER,
    parentid NUMBER
    CONSTRAINT tree_pk PRIMARY KEY (id)
    ...and a subquery - here just a table called sample with a subset of the ids from the tree table:
    CREATE TABLE sample
    id NUMBER,
    CONSTRAINT sample_pk PRIMARY KEY (id)
    ...with which to drive the start points of the treewalk:
    SELECT parentid, id, label
    FROM tree
    CONNECT BY PRIOR parentid = id
    START WITH id IN
    SELECT id FROM SAMPLE
    With the tables populated and analyzed, I get this from 8i:
    Execution Plan
    .0......SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=19)
    .1....0...CONNECT BY
    .2....1.....NESTED LOOPS (Cost=1 Card=1280 Bytes=10240)
    .3....2.......INDEX (FAST FULL SCAN) OF 'ID_PK' (UNIQUE) (Cost=1 Card=1280 Bytes=5120)
    .4....2.......INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE)
    .5....1.....TABLE ACCESS (BY USER ROWID) OF 'TREE'
    .6....1.....TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (Cost=2 Card=1 Bytes=19)
    .7....6.......INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE) (Cost=1 Card=1)
    Statistics
    .....0..recursive calls
    .....4..db block gets
    .15687..consistent gets
    ....59..physical reads
    .....0..redo size
    223313..bytes sent via SQL*Net to client
    .38276..bytes received via SQL*Net from client
    ...343..SQL*Net roundtrips to/from client
    .....3..sorts (memory)
    .....0..sorts (disk)
    ..5120..rows processed
    and this is 9i:
    Execution Plan
    .0......SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1 Bytes=19)
    .1....0...CONNECT BY (WITH FILTERING)
    .2....1.....NESTED LOOPS
    .3....2.......NESTED LOOPS (Cost=2 Card=1280 Bytes=10240)
    .4....3.........INDEX (FAST FULL SCAN) OF 'ID_PK' (UNIQUE) (Cost=2 Card=1280 Bytes=5120)
    .5....3.........INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE)
    .6....2.......TABLE ACCESS (BY USER ROWID) OF 'TREE'
    .7....1.....NESTED LOOPS
    .8....7.......BUFFER (SORT)
    .9....8.........CONNECT BY PUMP
    10....7.......TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (Cost=2 Card=1 Bytes=19)
    11...10.........INDEX (UNIQUE SCAN) OF 'TREE_PK' (UNIQUE) (Cost=1 Card=20480)
    12....1.....INDEX (UNIQUE SCAN) OF 'SAMPLE_PK' (UNIQUE) (Cost=1 Card=1 Bytes=4)
    Statistics
    .....1..recursive calls
    .....1..db block gets
    .20525..consistent gets
    ....72..physical reads
    ...120..redo size
    224681..bytes sent via SQL*Net to client
    .38281..bytes received via SQL*Net from client
    ...343..SQL*Net roundtrips to/from client
    .....9..sorts (memory)
    .....0..sorts (disk)
    ..5120..rows processed
    ..so, about another 5000 logical reads, corresponding to the extra access of the sample table at the bottom of the query plan. So instead of just visiting the START WITH subquery once, to kick off the treewalk, I seem to be revisiting it for every row returned. Not too bad if that happens to be a unique index scan as here but that's not always the case.
    I know I've got new options for re-writing this as a join under 9i, I'm just curious about those extra lookups and why they're necessary.
    Cheers - Andrew

    There is undocumented parameter in Oracle 9i "_old_connect_by_enabled"
    which controls the behavoiur of hierarchy queries in 9i and above:
    You can try to return to 8i behaviour using it:
    SQL> SELECT parentid, id
      2  FROM tree
      3  CONNECT BY PRIOR parentid = id
      4  START WITH id IN
      5  (
      6  SELECT id FROM SAMPLE
      7  )
      8  /
    Execution Plan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=26)
       1    0   CONNECT BY (WITH FILTERING)
       2    1     TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE)
       3    2       NESTED LOOPS (Cost=2 Card=1 Bytes=26)
       4    3         TABLE ACCESS (FULL) OF 'SAMPLE' (TABLE) (Cost=2 Card
              =1 Bytes=13)
       5    3         INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (C
              ost=0 Card=1 Bytes=13)
       6    1     NESTED LOOPS
       7    6       BUFFER (SORT)
       8    7         CONNECT BY PUMP
       9    6       TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE) (Cost=
              1 Card=1 Bytes=26)
      10    9         INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (C
              ost=1 Card=1)
      11    1     TABLE ACCESS (FULL) OF 'TREE' (TABLE) (Cost=1 Card=1 Byt
              es=26)
      12    1     INDEX (UNIQUE SCAN) OF 'SAMPLE_PK' (INDEX (UNIQUE)) (Cos
              t=1 Card=1 Bytes=13)
    SQL> alter session set "_old_connect_by_enabled" = TRUE;
    Session altered.
    SQL> SELECT parentid, id
      2  FROM tree
      3  CONNECT BY PRIOR parentid = id
      4  START WITH id IN
      5  (
      6  SELECT id FROM SAMPLE
      7  )
      8  /
    Execution Plan
       0      SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=26)
       1    0   CONNECT BY
       2    1     NESTED LOOPS (Cost=2 Card=1 Bytes=26)
       3    2       TABLE ACCESS (FULL) OF 'SAMPLE' (TABLE) (Cost=2 Card=1
               Bytes=13)
       4    2       INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (Cos
              t=0 Card=1 Bytes=13)
       5    1     TABLE ACCESS (BY USER ROWID) OF 'TREE' (TABLE)
       6    1     TABLE ACCESS (BY INDEX ROWID) OF 'TREE' (TABLE) (Cost=1
              Card=1 Bytes=26)
       7    6       INDEX (UNIQUE SCAN) OF 'TREE_PK' (INDEX (UNIQUE)) (Cos
              t=1 Card=1)
    Rgds.

Maybe you are looking for

  • ABAP Development standards for HR- ABAP

    Hi ALL Does any body has any document for Programming standards in HR ABAP programming. Thanking in advance for you support. cheers AJ

  • HT5242 Anyone know what the "extended period of time" is?

    Anyone know what the "extended period of time" is when the Java update decides for us when it should turn off automatic applet running?  Looking forward to the hack which allows us to fix that and make the decision ourselves.

  • Resolving connection lost....

    Hi, Many times when I'm moving data from one DB to another, my connection is being lost and the procedure results in "running" status for longer time. Is there any piece of code which frequently check the connection (like the one we have in Toad (Tes

  • Problem in function module 'CSAP_MAT_BOM_CREATE'

    Hi I am using CSAP_MAT_BOM_CREATE to create BOM i passing the following values CALL FUNCTION 'CSAP_MAT_BOM_CREATE' EXPORTING material = lv_matnr plant = lv_werks bom_usage = lv_stlan i_stko = wa_stko TABLES t_stpo = it_stpo EXCEPTIONS error = 1 OTHER

  • Help with Printing a Photobook

    Hello, I just purchased a new Mac and Photoshop Elements 6.0 about 2 months ago. I created a 48 page photobook as a christmas gift (ordered 3, in fact), and was very surprised that the only way I can print is through Kodak easyshare. I'm not happy wi