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 straightforward
Hemant 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....
Similar Messages
-
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 -
Analitical functions in recursive subquery factoring
Hi,
I have a problem with analitical functions in recursive subquery factoring.
My version:
>
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 64-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
>
When I run following query it runs fine:
with a (num, total) as
(select
1 num
,1 total
from
dual
union all
select
num + 1 num
,total + num + 1 total
from
a
where
num < 10
select
from
a
NUM TOTAL
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55
10 rows selected When I run the following query it generates an error:
with a (num, total) as
(select
1 num
,1 total
from
dual
union all
select
num + 1 num
,sum(num) over () total
from
a
where
num < 10
select
from
a
Error:
ORA-32486: unsupported operation in recursive branch of recursive WITH clause The manual states:
>
Oracle® Database
SQL Language Reference
11g Release 2 (11.2)
E17118-04
October 2010
The recursive member cannot contain any of the following elements:
- The DISTINCT keyword or a GROUP BY clause
- The model_clause
- An aggregate function. However, analytic functions are permitted in the select list.
- Subqueries that refer to query_name.
- Outer joins that refer to query_name as the right table.
>
According to this it should be posible to have an analitical function in here.
Also on this form I see samples of it used as such.
eg.:
Re: Seat Distribution-Can we do this in SQL?
Can anybody tell me if this is a problem with my version of oracle?
Or is there some other problem here?
Thanks,
PeterWorks ok in 11.2.0.3
SQL> select * from v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> with a (num, total) as
2 (select
3 1 num
4 ,1 total
5 from
6 dual
7 union all
8 select
9 num + 1 num
10 ,sum(num) over () total
11 from
12 a
13 where
14 num < 10
15 )
16 select
17 *
18 from
19 a
20 ;
NUM TOTAL
1 1
2 1
3 2
4 3
5 4
6 5
7 6
8 7
9 8
10 9
10 rows selected. -
I have a table with origin and destination ids.
There can be a dynamic number of connections ( not expecting more than 5) and the relation is always one to one: A -->B-->C-->D
A or B or C or D can only apear in a relation, This mean A-->C cannot happen because A e already connected to B.
I want to create a querie that receives the last destination values and return a column with the origin values that are related with that value:
for example:
if my parameter value is D, my result will be a column with three rows : A,B,C
if my parameter value is C, my result will be a column with two rows : A,B
example:
create table test_list as (
select 32000 origin, 68200 destination from dual
union all
select 60000 origin, 168200 destination from dual
union all
select 8200 origin, 36600 destination from dual
union all
select 36600 origin, 8400 destination from dual
union all
select 8400 origin, 61800 destination from dual
)I tried conect by prior and connect by root but could not achieve it!
found also some articles about "Recursive Subquery Factoring" but that got even worst because I could not get it to work.
My database is "Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production"
SELECT origin from test_list where
origin in (select * FROM (
SELECT CONNECT_BY_ROOT destination
FROM test_list
CONNECT BY PRIOR origin = destination and destination =68200 )
)So in my list:
32000 connects to 68200
60000 connects to 168200
8200 connects to 36600
36600 connects to 8400
8400 connects to 61800
My expected results are:
If parameter "VALUE_TO_SEARCH" = 68200 I expect only: 32000
If parameter "VALUE_TO_SEARCH" = 168200 I expect only: 60000
If parameter "VALUE_TO_SEARCH" = 61800 I expect : 8400,36600,8200
If parameter "VALUE_TO_SEARCH" = 32000 I expect no results.
What should be the best method to use, for best performance and in case is with the CONNECT_BY_ROOT and " CONNECT BY PRIOR" what am I doing wrong there?
Best regards,
Ricardo TomásUse START WITH clause:
SQL> select * from test_list;
ORIGIN DESTINATION
32000 68200
60000 168200
8200 36600
36600 8400
8400 61800
SQL> select origin
2 from test_list
3 start with destination = &destination
4 connect by destination = prior origin
5 /
Enter value for destination: 68200
old 3: start with destination = &destination
new 3: start with destination = 68200
ORIGIN
32000
SQL> /
Enter value for destination: 168200
old 3: start with destination = &destination
new 3: start with destination = 168200
ORIGIN
60000
SQL> /
Enter value for destination: 61800
old 3: start with destination = &destination
new 3: start with destination = 61800
ORIGIN
8400
36600
8200
SQL> /
Enter value for destination: 32000
old 3: start with destination = &destination
new 3: start with destination = 32000
no rows selected
SQL> SY. -
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. -
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. -
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. -
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 -
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. -
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. -
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 -
When does OBIEE forcibly use Subquery Factoring ?
In a 10.2 "datawarehouse" schema I see OBIEE queries of the form :
WITH SAWITH0 AS (select distinct T12345.COLUMN_1 as c1 from DIMENSION_TABLE T12345
where ( T12345.COLUMN_1 like 'HEMANT%')
) select distinct SAWITH0.c1 as c1 from SAWITH0I cannot understand why OBIEE wouldn't run a simpler
select distinct T12345.COLUMN_1 as c1 from DIMENSION_TABLE T12345
where ( T12345.COLUMN_1 like 'HEMANT%')What OBIEE setup would cause even simple queries to be generated as using Subquery Factoring ?
Hemant K Chitale
http://hemantoracledba.blogspot.comHi Hemant,
use of WITH can be disabled in the OBIEE RPD Connection pool. Its enabled by default to allow the performance benefit you would see when using aggregated result sets. I personally think the OBIEE server is simply not advanced enough to determine when it doesn't need to use it (ie when selecting distinct dimension member values in dashboard prompts).
Just my 2c. Hopefully some of the experianced users on here can expand a little more for you. Id be interested to know as well. -
OBIEE 11g "WITH SAWITH0 AS" subquery factoring clause in the generated sql
I've observed that the OBIEE 11g generates in the query log physical query using the WITH (sub-query factoring) clause to make the generated sql elegantly readable. This is great! Thanks for the developers. However I have some questions about this.
__Background__
Oracle Database' default behaviour is that if you have only one sub-query in the WITH section, it executes it as an in-line view and does not materialize it before the main sql is executed. If you have more than one, by default the database engine materializes all of them in the order of the definition. In some cases this can completely blow up the SGA and make the query never ending. To divert this behaviour you can apply two hints that work both in inline views and in sub-queries as well: /*+ MATERIALIZE */ and /*+ INLINE*/, however Analytics 11g does not seem to have hint capabilities at the logical table level, only at physical table level.
If we go with the current defaults, developers not aware of this feature can bump into serious performance issues for the sake of some syntax candy at the generated sql level, I'm afraid.
__Questions__
* Is it possible to turn the Analytics server not to use WITH but use inline views instead?
* Is there any way to sneak in some hints that would put the /*+ INLINE */ hint to the appropriate place in the generated sub-queries if needed
* Does the Oracle Database have any initialization parameter that can influence this sub-query factoring behavior and divert from the default?The WITH statement is not added to make the query more elegant, it's added for performance reasons. If your queries take long to run then you may have a design issue. In a typical DWH DB SGA needs to be seriously increased since the queries ran are much larger and complex than on an OLTP DB. In any case you can disable the WITH statement in the Admin Tool by double clicking on your database object on the physical layer and going to the Features tab. The feature is called WITH_CLAUSE_SUPPORTED.
-
Someone please help me understand why I get the results I do with this simple query:
WITH RUNDATES AS (
SELECT
RUN.RN_EXECUTION_DATE AS ACTUAL_DATE,
TC_PLAN_SCHEDULING_DATE AS PLANNED_DATE
FROM TESTCYCL
LEFT JOIN RUN ON RUN.RN_TESTCYCL_ID=TESTCYCL.TC_TESTCYCL_ID
SELECT MIN(ACTUAL_DATE) MINDT, MAX(PLANNED_DATE) MAXDT FROM RUNDATES
UNION ALL
SELECT MIN(ACTUAL_DATE) MINDT, MAX(PLANNED_DATE) MAXDT FROM RUNDATES
Result:
MINDT MAXDT
(Null) 2010-12-29 00:00:00
2009-01-26 00:00:00 2010-12-29 00:00:00I have reduced this query significantly for simlicity. In my real query, RUNDATES is referenced multiple times, but here I just reference it twice with a UNION.
As you can see, multiple references to it do not produce the same results.
If I remove the "LEFT" join and just perform a JOIN on the RUN table, it appears to work fine, but I need the left join so that all rows of the TESTCYCL table are included in the MAX(PLANNED_DATE) calculation.
ANY THOUGHTS?
I'm using 10g v10.2.0.3.0 64bit>
SoWright wrote:
Unfortunately, my access to the server is limited and I can only run SELECT queries
>
I was stuck in that situation for quite a while, so I "feel your pain" here. I have a few questions on this front...
1) Are you a local admin on your PC?
If yes, install Oracle XE 10g on your PC. This is a free download from Oracle, and allows you to have a database up to 4GB in size. It's perfect for a test environment, particularly when you normally only have SELECT permissions on databases you have access to. It's especially good if you can set up a database link between XE and the database(s) you normally query, so you can CREATE TABLEs in the XE database AS SELECT from the database(s) you normally query. This is what I use.
Link to XE 10g:
http://www.oracle.com/technetwork/database/express-edition/overview/index.html
If no, ask your IT department to install the above for you on your PC. Make sure to point out that it's free, but it comes straight from Oracle. See 'If yes'.
2) Can you run explain plan? In order to run explain plan you have to have permissions on plan_table. I'd guess you probably don't (I don't), so you may want to ask your IT department/DBA to run explain plan on your query for you and give you the results. Again, if you have XE, I use a little work-around that works at least some of the time.
I think the following might be for 8i-9i databases' plan_tables (which is what I have to use), but what I do is create a table in my XE database that mimics what the plan_table looks like:
CREATE TABLE MY_EXP_PLANS (
STATEMENT_ID VARCHAR2(30),
TIMESTAMP DATE,
REMARKS VARCHAR2(80),
OPERATION VARCHAR2(30),
OPTIONS VARCHAR2(30),
OBJECT_NODE VARCHAR2(128),
OBJECT_OWNER VARCHAR2(30),
OBJECT_NAME VARCHAR2(30),
OBJECT_INSTANCE NUMBER(38),
OBJECT_TYPE VARCHAR2(30),
OPTIMIZER VARCHAR2(255),
SEARCH_COLUMNS NUMBER,
ID NUMBER(38),
PARENT_ID NUMBER(38),
POSITION NUMBER(38),
COST NUMBER(38),
CARDINALITY NUMBER(38),
BYTES NUMBER(38),
OTHER_TAG VARCHAR2(255),
PARTITION_START VARCHAR2(255),
PARTITION_STOP VARCHAR2(255),
PARTITION_ID NUMBER(38),
OTHER LONG,
DISTRIBUTION VARCHAR2(30)
);If you go to http://www.adp-gmbh.ch/ora/exp_plan/plan_table.html
There's code for the 10g table on that page (just call it something else, like I did above).
And, as long as you have the actual database linked to your XE database, then you can run:
explain plan into my_exp_plans for
--whatever your query is goes here
SELECT my_col
FROM my_tab@mylinkedDB
etc.Here's a link to a page showing all the different database links you can create. I use a fixed user link...
http://psoug.org/reference/db_link.html
If you can get the explain plan, you should be able to get more help on your problem--particularly identifying the bug Justin Cave suggested may be to blame.
Just for the heck of it, you might check/post the results of DESC run and DESC testcycl, just to make sure that all the datatypes you think you know are correct. (Make sure dates are dates not timestamps or something, that sort of thing). I'm not sure it would affect anything in 10g, like I've had problems with in 8i, but it can't hurt to validate.
It is just really odd that you even get NULL as a result of a MIN(your_date) function. The only way you get NULL from MIN is if all values that MIN looks at are NULL, because normally, it ignores NULLS. So, if you have a column that has values 1, NULL, NULL, 2 then MIN(your_column) will return 1.
If you are unable to do any of the above, I'd stick with using the syntax that doesn't cause the bug to rear its ugly head. =) Though, if it were me, I'd still be worried it was going to mess up and I'd ask my IT department to get the patches or talk to Oracle support about it. -
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
Maybe you are looking for
-
Itunes says it's updating my ipod but it's not...
Hi, I'm running a Dell/Windows XP environment with an ipod mini (Version 1.4.1) and itunes (Version 6.0.4.2). My problem is that itunes says it's updating my ipod but it's not. When I copy songs across to my ipod, itunes provides the message "copying
-
Import/export data from ms sql 7 to xml file in a particular xpdl format
I'm trying to write java program to import/export data from ms sql 7 to xml file in a particular xpdl format. Can anyone suggest how to do it?
-
Sign in document forms??
Where are the sign in document forms?
-
[AS][CS3] Finding text with character style
Please help to tell me how to find text with a specific character style and then delete the text. I had no problem doing this with CS2 but its doing my head in now. Thanks, Andrew
-
Copy workflow from 5.0 to 6.0
Hi, My client is having two different systems ECC 5.0 and 6.0. Now workflow needs to be copied from 5.0 to 6.0. Please suggest what is the best way to do this task. I have tried with export and import to XML. It didn't work. I am missing many compone