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 |
-------------------------------------------------------------------------------------------------------------
Similar Messages
-
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
ivwivw 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. -
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 selectedYou'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 -
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,
RahulHi 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. -
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.4Hi,
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 -
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. -
Credit sales order with credit check without top up limit
Hi Experts,
I have a silly question, just wanted to confirm is this possible in SAP system?
I have check thru seem no such function.
My user requested a requirement is credit sales order with credit limit check however without top up credit limit to specific customer. eventually allow this specific customer sales order, delivery order without blocking and created succesfully?
Thanks & Regards,
Yong Kok WahHi,
Use a seprate risk category for such customer. In OVA8 Setting for the given risk category set the Credit Limit Seasonal factor.
By means of above setting system will support the concept of TOP Up credit limit (for a given time period) for given customer against said credit limit.
Regards
Digambar Pandey -
Sales order with reference to sales order
Hi,
I need to create a sales order with reference another sales order using bapi CreateFromDat2. Please note, Sales Ord. Dist. channel, Div, sold-to to of the new order are different from original order. I gave reference order in field order_header_in-REFDOCTYPE . I am getting following error when I perform single test " FB Call: insufficient parameters. Can any of you tell me how to overcome this issue. And is it possible to create an order refering to another Saler order with different Sales Ord, Dist. channel, Division, sold-to ?
<b><REMOVED BY MODERATOR></b>
Cheers
Sudhaker
Message was edited by:
Alvaro Tejada GalindoThere is an OSS note <a href="https://service.sap.com/sap/support/notes/370988">Note 370988 - BAPIs in SD: Creation with reference</a>
<i><b>Symptom</b>
This note describes the problems of the 'Create with reference' function when using BAPIs to create sales documents.
If you are working online, you can use this function module to create a sales document with reference to an existing sales document or a billing document (for example, a debit or credit memo request) according to fixed rules. The rules for transferring information from the source document to the target document are defined in the copying control on header and item level. The creation with reference permits an item selection.
When you use BAPIs in SD, a function of this type is not fully available. A reference is only possible for a quantity contract, so that the target quantity of the contract can be reduced by the order quantity of the sales document. Furthermore, the document flow is also updated.
As an alternative to 'Create with reference' in Transaction VA01, two variants are provided for solving the 'Create with reference' problem in the BAPI.
1. Create a '1:1 ' - that is, a document copy of a sales document (customer quotation, order) with 'BAPI_SALESDOCUMENT_COPY'. At this point, you cannot create the 1:1 copy of either a billing document or a delivery.
2. Create a sales document that refers to an existing document with 'BAPISDORDER_GETDETAILEDLIST' and 'BAPI_SALESORDER_CREATEFROMDAT2'.
<b>Other terms</b>
BAPI_SALESDOCUMENT_COPY, BAPI_SALESORDER_CHANGE, BAPI_CUSTOMERINQUIRY_CHANGE, BAPI_CUSTOMERQUOTATION_CHANGE, BAPI_SALESORDER_CREATEFROMDAT1, BAPI_SALESORDER_CREATEFROMDAT2, BAPI_SALESORDER_CREATEFROMDATA, BAPISDORDER_GETDETAILEDLIST
<b>Reason and Prerequisites</b>
As of yet, no method exists for creating a sales document that refers to an existing document.
Solution
For solution No. 1
1. Create the documents using the BAPI_SALESDOCUMENT_COPY function module. However, this only permits a 1:1 copy without factoring out individual items.
2. After you have created the document copy, you can use a change BAPI to edit this copy.
For solution No. 2
1. Collect the data for the reference document (for example a customer quotation) with the 'BAPISDORDER_GETDETAILEDLIST' function module. In the example code, focus exclusively on the partners, items, schedule lines and conditions.
2. Prepare the Data. Execute the 'copying control' manually. This way, you decide which values are to be transferred.
3. Create the sales document using the 'BAPI_SALESORDER_CREATEFROMDAT2' function module by transferring the prepared data.
4. Finally, you must remove a COMMIT WORK so that the sales document can be written into the database.
An example of solution 2 is attached as a text file. You are allowed to select items and quantities for example, if you use input parameters. If there are no entries, then transfer 1:1 from the reference document. When you enter the 'condition type' parameter, you can determine whether conditions are copied or recalculated ('G' for transferring from the preceding document; 'B' or ' ' for re-determining, and 'C' for re-determining with transfer of the manual elements).</i>
Sample : <a href="https://websmp108.sap-ag.de/~sapidb/012003146900000486112004E/BAPI_COPY_WITH_REF.txt">ZZ_MS_BAPI_COPY</a>
Regards -
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 -
Visual Studio 2013 Community Azure Login Not Working with Two-factor Authentication
Has anybody had any problems logging in to Azure to publish when using Visual Studio 2013 Community and with two-factor authentication turned on?
I couldn't log on until I turned off two-factor authentication.
RegardsHello John,
Thanks for posting here!
You can try and set a credential helper like
git-credential-winstore in order to cache your credentials. See if that helps.
Couple of questions here:
1) Are you using a MSA account by any chance?
2) When you turn on two-factor authentication, do you get any error message?
3) Did you try with different browsers?
Looking forward to your response!
Regards,
Sadiqh -
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,
GokulAm 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 -
hello
Is it a rule that the subquery should always be followed with a select statement.
I am using it as follows
WITH subquery_this AS
SELECT col_name1, col_name2, col_name3, ROW_NUMBER() OVER ( PARTITION BY col_name3
ORDER BY col_name2 DESC )
AS S
FROM zs
WHERE col_name2 = something
Update sometable
SET col_name5 = (select col_name2 from subquery_this where sometable2.col_name1= subquery_this.col_name1)
My question is is it necessary that the "with" subquery should be followed by a select statement. As in my case it is followed with an update statement.
Also if I am using this in a procedure, where this should be placed.
-> In the BEGIN section or
-> After IS and before BEGIN i.e in DECLARE section of subprocedure.
Edited by: user6287828 on Mar 12, 2009 6:39 PMWITH is a feature only of the SELECT statement. But you can use SELECT in INSERT/UPDATE/DELETE statements.
Here's a trivial example:
SQL> update emp
2 set job = ( with subq as (select 'LAYABOUT' newjob from dual)
3 select newjob from subq)
4 where empno = 7934
5 ;
1 row updated.
SQL> select * from emp where empno = 7934;
EMPNO ENAME JOB MGR HIREDATE
7934 MILLER LAYABOUT 7782 23-JAN-1982 00:00:00 -
Subquery Factoring -Performance Issue
Hi experts,
Oracle 10G
select
a.colums,
b.columns
from
( select
Colums
from
F ,
(select
columns
from
bigtable
where
<condition with recdate and modified date with timezone convertions Given as in end of this qury)
) a,
( select
Colums
from
tables,
(select
columns
from
bigtable
where
<condition with recdate and modified date with timezone convertions Given as in end of this qury)
) b
(TO_DATE(TO_CHAR(FROM_TZ(CAST(CF.RECDT as timestamp),'GMT') at time zone 'America/Chicago','YYYY-MM-DD
HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')
BETWEEN TO_DATE('01-JAN-2009 00:00:00','DD-MON-YYYY HH24:MI:SS') AND TO_DATE('08-JAN-2009 00:00:00', 'DD-MON-YYYY
HH24:MI:SS')) OR
(TO_DATE(TO_CHAR(FROM_TZ(CAST(CF.MODIFID_DT as timestamp),'GMT') at time zone 'America/Chicago','YYYY-MM-DD
HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')
BETWEEN TO_DATE('01-JAN-2009 00:00:00','DD-MON-YYYY HH24:MI:SS') AND TO_DATE('08-JAN-2009 00:00:00', 'DD-MON-YYYY
HH24:MI:SS'))
Recdt - indexed
Modifieddt - not iddexed
bigtable used many times in select to avoid Subquery factoring used (with select) but it increased cost as per explain plan in sqldevleoper please suggest me to increase performance
Lots of thanks,
Kalinganow the above query use as
with subfact as
( select
select
columns
from
bigtable
where
<condition with recdate and modified date
select
a.colums,
b.columns
from
( select
Colums
from
F ,
subfact
where condition
) a,
( select
Colums
from
tables,
subfact
where condition
) b
it is cost increased in nested loop in explain plan why? is thre any other detail required. -
Recursive subquery factoring datatypes?
Hi all,
using 11.2.0.2.0
just mucking around with Recursive Subquery Factoring, trying to get my head around it.
I can do this fine:
SQL> with numlist (num) AS (SELECT 1 num
2 from dual
3 UNION ALL
4 SELECT numlist.num + 1
5 FROM numlist
6 where numlist.num < 10)
7 SELECT *
8 from numlist;
NUM
1
2
3
4
5
6
7
8
9
10
10 rows selected.but not with dates:
SQL> WITH datelist (dte) AS (SELECT to_date('01-01-2011','dd-mm-yyyy') Dte
2 FROM dual
3 UNION ALL
4 SELECT datelist.dte + 1
5 FROM datelist
6 WHERE datelist.dte < trunc(SYSDATE))
7 select *
8 from datelist;
SELECT datelist.dte + 1
ERROR at line 4:
ORA-01790: expression must have same datatype as corresponding expressionI'm not sure what I need to do.....
I'm sure it's a fairly straightforwardHemant K Chitale wrote:
I don't have an 11.2.0 environment to test this.
Try with a CAST at line 4 ? CAST X.DTE to a Date ?
Hemant K Chitaleahh, that's a little bit better.... it seems a little bit funky though:
15:38:58 SQL> WITH x (dte) AS (SELECT cast (to_date('01-01-2011','dd-mm-yyyy') as date) Dte
15:39:02 2 FROM dual
15:39:02 3 UNION ALL
15:39:02 4 SELECT cast(x.dte + 1 as date)
15:39:02 5 FROM x
15:39:02 6 WHERE x.dte < to_date('01-02-2011','dd-mm-yyyy'))
15:39:02 7 select *
15:39:02 8 from x;
DTE
01-JAN-11
31-DEC-10
30-DEC-10
29-DEC-10
28-DEC-10
27-DEC-10
26-DEC-10
25-DEC-10
24-DEC-10
23-DEC-10
22-DEC-10
21-DEC-10
20-DEC-10
19-DEC-10
18-DEC-10
17-DEC-10
16-DEC-10
15-DEC-10
14-DEC-10
13-DEC-10
12-DEC-10
11-DEC-10
10-DEC-10
09-DEC-10
08-DEC-10
07-DEC-10
06-DEC-10
05-DEC-10
04-DEC-10
03-DEC-10
02-DEC-10
01-DEC-10
30-NOV-10
29-NOV-10
28-NOV-10
27-NOV-10
26-NOV-10
25-NOV-10
24-NOV-10
23-NOV-10
22-NOV-10
21-NOV-10
20-NOV-10
...looks like it's going backwards.
if I cast it like the below, it only gives me one record...
15:39:03 SQL> WITH x (dte) AS (SELECT cast (to_date('01-01-2011','dd-mm-yyyy') as date) Dte
15:40:52 2 FROM dual
15:40:52 3 UNION ALL
15:40:52 4 SELECT cast(x.dte as date) + 1
15:40:52 5 FROM x
15:40:52 6 WHERE x.dte < to_date('01-02-2011','dd-mm-yyyy'))
15:40:52 7 select *
15:40:52 8 from x;
DTE
01-JAN-11
1 row selected.This is bizarre....
Maybe you are looking for
-
Rotate on iPod Touch 2G no longer working?
So I bought a new laptop and ipod touch 2g through the student discount program for the summer. I got the Touch last friday. Today, I was surfing the internet and Safari wouldn't rotate sideways. So I checked with my music, and yet again, it won't ro
-
ITunes can't sync TV shows...
I have a TV show which I'd like to transfer into my iPod (30gb video). The whole show is on MPEG-4 format which the iPod can read. I added all the show (about 76 episodes) and when I pressed "sync" only 4 of the episode has been transfered and I got
-
My 2 year old has deleted my 'apps store' icon and I cannot figure out how to get it back? Doesn't seem to be available in the on-line apps store, ironically enough
-
Ipod touch screen randomly quitting.
I just bought a new iPod Touch 5th Generation less than 2 weeks ago. About a week ago a problem arose. Now, sometimes the screen will randomly stop working and do things that I don't want it to do(e.g. open apps). It will also do things when I am not
-
Problems with the margin in then top of the page when using subtemplates
Hi, I have a main template (made by Word) where i have in the header section the following statement <?choose:?><?when:R_LANG='EN'?> <?call-template:otsikko_EN?> <?end when?><?otherwise:?> <?call-template:otsikko_FISE?> <?end otherwise?><?end choose?