UNION IN SQL
HOW CAN I UNION IN SQL MULTIPLE TIMES. I MAM TRYING TO USE UNION MULTIPLE TIMES SOME TIME IT WORKS BUT SO MANY OFTEN IT DOSE'NT AND ALSO SOME TIME IT GIVE'S THE WRONG RESULT
ANY REASON
OFTEN IT DOSE'NT AND ALSO SOME TIME IT GIVE'S THE WRONG RESULT May be because you are doing it the wrong way. If you are learning about union, it may confuse you initially, but try understanding the way union works and figure out where you are doing a mistake.
Similar Messages
-
Hi,
I am using Oracle JDeveloper 11 g (11.1.1.3.0) and am in the process of designing a Database. I was going to use Oracle Express which seems to indicate that it supports UNION in SQL. However, the JDeveloper tells me that while my SQL syntax is valid when I include 'union' statements, the use of 'union' is not supported. Can I ignore the JDeveloper warning or is there some other way of getting around this? Also, am I right in thinking that Oracle Express supports UNION?
Thanks for any help,
JimHi,
Oracle XE does support UNION. Not sure why JDeveloper gives you a warning and you don't provide steps to reproduce
Frank -
How to avoid unions in sql quries
Hi All,
I have an sql statement in which I am using unions to get the data from two tables with different conditions. how can avoid UNIONs in my query, which will increase performance. please suggest.
SQL Query is :
SELECT opt_url_path, locale
FROM urlregistry
WHERE master_assetid IN
(SELECT c.id FROM content_c c, content_cd cc
WHERE c.flextemplateid = cc.id AND cc.name = 'SoftwareDownload'
AND (c.PATH NOT LIKE '/auth%' AND c.PATH NOT LIKE '/gate%'))
UNION
SELECT opt_url_path, locale
FROM urlregistry
WHERE master_assetid IN
(SELECT c.id FROM content_c c, content_c_mungo cm
WHERE c.id = cm.cs_ownerid AND cm.cs_attrid =
(SELECT id FROM attribute WHERE name = 'Gated')
AND c.flextemplateid IN (SELECT id FROM content_cd WHERE name = 'Collateral')
AND cm.stringvalue = 'Yes'
AND (c.PATH NOT LIKE '/auth%'
AND c.PATH NOT LIKE '/gate%'))
UNION
SELECT PATH, locale
FROM bloburlregistry
WHERE master_assetid IN
(SELECT c.id FROM content_c c, content_c_mungo cm
WHERE c.id = cm.cs_ownerid AND cm.cs_attrid = (SELECT id FROM attribute WHERE name = 'Gated')
AND c.flextemplateid IN (SELECT id FROM content_cd WHERE name = 'Collateral')
AND cm.stringvalue = 'Yes'
AND (c.PATH NOT LIKE '/auth%'
AND c.PATH NOT LIKE '/gate%'))
Thank u.To increase performance you need to reduce the I/O. So when ever i tune a SQL the first thing i look for is that. If i have a table used multiple times i try to remove it and solve the required problem by just scanning the table once. But do to that we need to have good understanding of the data and there relation between tables. But we don't know anything about yours. Said that i came up with this.
select opt_url_path
, locale
from urlregistry
where master_assetid
IN
select c.id
from content_c c
left
join content_cd cc
on c.flextemplateid = cc.id
and (cc.name = 'SoftwareDownload' or cc.name = 'Collateral')
left
join content_c_mungo cm
on c.id = cm.cs_ownerid
and cm.cs_attrid =
SELECT id
FROM attribute
WHERE name = 'Gated'
and cm.stringvalue = 'Yes'
where c.PATH NOT LIKE '/auth%'
and c.PATH NOT LIKE '/gate%'
and (
cc.flextemplateid is not null or
cm.cs_owner_id is not null
union
select PATH
, locale
from bloburlregistry
where master_assetid IN
select c.id
from content_c c
, content_c_mungo cm
where c.id = cm.cs_ownerid
and cm.cs_attrid =
SELECT id
FROM attribute
WHERE name = 'Gated'
and c.flextemplateid IN
SELECT id
FROM content_cd
WHERE name = 'Collateral'
and cm.stringvalue = 'Yes'
and c.PATH NOT LIKE '/auth%'
and c.PATH NOT LIKE '/gate%'
This is a untested code. You can give this a try and see if it gives the expected output and also if the performance improves. -
Hi Guys,
Oracle Version: 10g.
I got a materialized having the following sql. Is there anyway, the SQL can be simplified by removing the unnecessary
aggregate function and group by clause. Any pointers will be highly appreciated. Thanks in advance!
SELECT P.PRD_ID,
D.MKT_ID,
D.FY_WK_END,
D.WK_TAG_CD,
SUM (D.P_QTY) ,
SUM (D.C_QTY) ,
SUM (D.SLS_AMT) ,
SUM (D.E_QTY) ,
SUM (D.CALC) ,
SUM (D.PKG_QTY * P.FCTR) ,
SUM (D.PKG_QTY * P.U_FCTR) ,
SUM(D.PNDS)
FROM ( SELECT P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD,
SUM (D.PKG) AS P_QTY,
SUM (D.BILL) AS C_QTY,
SUM (D.SLS_AMT) AS SLS_AMT,
SUM (D.E_QTY) AS E_QTY,
SUM (D.BILL * P.FCTR) AS CALC,
SUM (D.BILL * BWM.GROSS_WT) AS PNDS
FROM DEL_F D,
PRD_D P,
CUST_D T,
CALD C,
(SELECT MKT_ID,
MKT_CD,
SORG
FROM MKT_M
WHERE SORG = '803'
AND UPPER (GRP_TY) = 'CGS') M,
MV_MAT BWM
WHERE D.PRD_ID = P.PRD_ID
AND D.CUST_ID = T.CUST_ID
AND T.CUST_CD = M.MKT_CD
AND M.SELORG = D.SELORG
AND D.WK_ENDING_DT = C.CAL_DY_DT
AND P.MATL_CD = BWM.MAT
GROUP BY P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD) D,
MV_UP P
WHERE P.FIN_ID = D.PRD_ID
GROUP BY P.PRD_ID,
D.MKT_ID,
D.WK_ENDING_DT,
D.WK_TAG_CD
UNION ALL
SELECT P.PRD_ID,
D.MKT_ID,
D.FY_WK_END,
D.WK_TAG_CD,
SUM (D.PKG_QTY) ,
SUM (D.CASE_QTY) ,
SUM (D.SLS_AMT) ,
SUM (D.EQC_QTY) ,
SUM (D.CALC_QTY) ,
SUM (D.PKG_QTY * P.FCTR) ,
SUM (D.PKG_QTY * P.U_FCTR) ,
SUM(D.PNDS)
FROM ( SELECT P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD,
SUM (D.PKG) AS P_QTY,
SUM (D.BILL) AS C_QTY,
SUM (D.SLS_AMT) AS SLS_AMT,
SUM (D.E_QTY) AS E_QTY,
SUM (D.BILL * P.FCTR) AS CALC,
SUM (D.BILL * BWM.GROSS_WT) AS PNDS
FROM DEL_F D,
PRD_D P,
SELD S,
CALD C,
(SELECT MKT_ID,
MKT_CD,
SORG
FROM MKT_M
WHERE SORG = '803'
AND UPPER (GRP_TY) = 'SR') M,
MV_MAT BWM
WHERE D.PRD_ID = P.PRD_ID
AND D.SLL_ID = S.SLL_ID
AND S.RGN_CD = M.MKT_CD
AND M.SORG = D.SORG
AND D.WK_ENDING_DT = C.CAL_DT
AND P.MATL_CD = BWM.MATL
GROUP BY P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD) D,
MV_UP P
WHERE P.FIN_ID = D.PRD_ID
GROUP BY P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD;SELECT P.PRD_ID,
D.MKT_ID,
D.FY_WK_END,
D.WK_TAG_CD,
SUM (D.P_QTY) ,
SUM (D.C_QTY) ,
SUM (D.SLS_AMT) ,
SUM (D.E_QTY) ,
SUM (D.CALC) ,
SUM (D.PKG_QTY * P.FCTR) ,
SUM (D.PKG_QTY * P.U_FCTR) ,
SUM(D.PNDS)
FROM ( SELECT P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD,
SUM (D.PKG) AS P_QTY,
SUM (D.BILL) AS C_QTY,
SUM (D.SLS_AMT) AS SLS_AMT,
SUM (D.E_QTY) AS E_QTY,
SUM (D.BILL * P.FCTR) AS CALC,
SUM (D.BILL * BWM.GROSS_WT) AS PNDS
FROM DEL_F D,
PRD_D P,
CUST_D T,
CALD C,
(SELECT MKT_ID,
MKT_CD,
SORG
FROM MKT_M
WHERE SORG = '803'
AND UPPER (GRP_TY) = 'CGS') M,
MV_MAT BWM
WHERE D.PRD_ID = P.PRD_ID
AND D.CUST_ID = T.CUST_ID
AND T.CUST_CD = M.MKT_CD
AND M.SELORG = D.SELORG
AND D.WK_ENDING_DT = C.CAL_DY_DT
AND P.MATL_CD = BWM.MAT
GROUP BY P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD) D,
MV_UP P
WHERE P.FIN_ID = D.PRD_ID
GROUP BY P.PRD_ID,
D.MKT_ID,
D.WK_ENDING_DT,
D.WK_TAG_CD
UNION ALL
SELECT P.PRD_ID,
D.MKT_ID,
D.FY_WK_END,
D.WK_TAG_CD,
SUM (D.PKG_QTY) ,
SUM (D.CASE_QTY) ,
SUM (D.SLS_AMT) ,
SUM (D.EQC_QTY) ,
SUM (D.CALC) ,
SUM (D.PKG_QTY * P.FCTR) ,
SUM (D.PKG_QTY * P.U_FCTR) ,
SUM(D.PNDS)
FROM ( SELECT P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD,
SUM (D.PKG) AS P_QTY,
SUM (D.BILL) AS C_QTY,
SUM (D.SLS_AMT) AS SLS_AMT,
SUM (D.E_QTY) AS E_QTY,
SUM (D.BILL * P.FCTR) AS CALC,
SUM (D.BILL * BWM.GROSS_WT) AS PNDS
FROM DEL_F D,
PRD_D P,
SELD S,
CALD C,
(SELECT MKT_ID,
MKT_CD,
SORG
FROM MKT_M
WHERE SORG = '803'
AND UPPER (GRP_TY) = 'SR') M,
MV_MAT BWM
WHERE D.PRD_ID = P.PRD_ID
AND D.SLL_ID = S.SLL_ID
AND S.RGN_CD = M.MKT_CD
AND M.SORG = D.SORG
AND D.WK_ENDING_DT = C.CAL_DY_DT
AND P.MATL_CD = BWM.MAT
GROUP BY P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD) D,
MV_UP P
WHERE P.FIN_ID = D.PRD_ID
GROUP BY P.PRD_ID,
M.MKT_ID,
C.WK_ENDING_DT,
C.WK_TAG_CD;Hi Hoek, Etbin:
Thanks for the replies. As you can see above both the SQL, SQL1 UNION ALL SQL2 are almost alike except at a few places.
1. Tables: CUST_D and SELD
2. Filtering code for MKT_M
3. JOIN conditions of INLINE VIEWS of SQL1 and SQL2.
Problem 1: SQL1 and SQL2 has SUM function which aggregates the column that comes from the inline view when "WHERE P.FIN_ID = D.PRD_ID",
this join condition is satisfied. I am thinking since outer SQL are aggregating the same columns which are aggregated in the inline view for example -
SUM (D.CALC) <- This guy is in outer sql of both SQL1 and SQL2.
SUM (D.BILL * P.FCTR) AS CALC <- This guy is in inner sql of SQL1 and SQL2.
Does that mean, I can remove the SUM from outer sql and just write D.CALC?
Can I do the same things for other SUMs too?
OR
Can I remove the aggregation from inner sql i.e. inline view e.g. SUM (D.BILL * P.FCTR) AS CALC becomes just D.BILL * P.FCTR and then aggregate it in
outer sql?
Problem 2. If the above things can be done than instead of grouping bys twice in SQL1 and SQL2, we can group by only once. Will it give the same result as the original SQL?
Problem 3. Any other way of writing the sql in a simplified way? Can it be made simple?
Please advice.
Thanks in advance!
Edited by: abyss on Mar 12, 2011 9:41 AM
Edited by: abyss on Mar 12, 2011 9:43 AM
Edited by: abyss on Mar 12, 2011 9:43 AM
Edited by: abyss on Mar 12, 2011 9:44 AM -
Hi Gurus,
I have three tables. I want to join all tables using union in SQL statement. The query is returning all the records from both tables but i only require unique rows based on a specific column value. Here is my table structure -
TableA -
LIC_ID NUMBER(10) NOT NULL
LIC_NUMBER VARCHAR2(20)
COMMENCE_DATE DATE
EXPIRY_DATE DATE
TERM VARCHAR2(20)LIC_ID is the primary key in this table -
Sample data from TableA
LIC_ID LIC_NUMBER COMMENCE_DATE EXPIRY_DATE TERM
2 TR4323 12/04/2008 11/03/2010 2 Years
34 TR5432 23/07/2009 22/07/2010 1 Year
45 TR5321 24/06/2009 23/06/2010 1 Year
65 TR6666 23/07/2010 22/07/2011 1 Year
32 TR2423 30/05/2010 29/05/2011 1 YearTableB -
MAR_ID NUMBER(10) NOT NULL
LIC_ID NUMBER(10) NOT NULL
ZONE_NAME VARCHAR2(20)
DEPARTMENT VARCHAR2(20)
ACTIVITIES VARCHAR2(200)
COMMENTS VARCHAR2(200)MAR_ID is the primary key in this table and LIC_ID is the foreign key on TableA
Sample data from TableB -
MAR_ID LIC_ID ZONE_NAME DEPARTMENT ACTIVITIES COMMENTS
23 2 ZONE A IT NONE
43 34 ZONE B IT NONE
33 65 ZONE C ACCOUNT NONE
TableC
REC_ID NUMBER(10) NOT NULL
LIC_ID NUMBER(10) NOT NULL
DIST_NAME VARCHAR2(20)
REGION VARCHAR2(20)
ACTIVITIES VARCHAR2(200)
COMMENTS VARCHAR2(200)REC_ID is the primary key in this table and LIC_ID is the foreign key.
Sample data -
REC_ID LIC_ID DIST_NAME REGION ACTIVITIES COMMENTS
2 45 SA NORTH NONE
3 65 TA NORTH NONE
5 32 NT SOUTH NONEHere is my sql query -
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableB b
where a.lic_id=b.lic_id
union
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableC c
where a.lic_id=c.lic_idThe above query returns -
lic_id lic_number commence_date expiry_date
2 TR4323 12/04/2008 11/03/2010
34 TR5432 23/07/2009 22/07/2010
45 TR5321 24/06/2009 23/06/2010
65 TR6666 23/07/2010 22/07/2011
32 TR2423 30/05/2010 29/05/2011
65 TR6666 23/07/2010 22/07/2011 LIC_ID 65 exists in both table TableB and TableC hence it repeats in query but I want to display that only once. How can I do that? I want to return unique record on LIC_NUMBER.
Hope this make sence.
Many thanks,
TajuddinThanks for all your reply and suggestions. David altering session did not work.
Sven your idea helped me to figure it out what to do. I found a way around to fix it. Here is my current code -
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableB b
where a.lic_id=b.lic_id
union
select a.lic_id, a.lic_number, a.commence_date, a.expiry_date from
TableA a, TableC c
where a.lic_id=c.lic_id and c.lic_id not in ( select lic_id from TableB)This will exclude any LIC_ID exists in TableB.
Thanks again for your help guys.
Regards,
M Tajuddin
Web: http://tajuddin.whitepagesbd.com
Blog: http://aspblog.whitepagesbd.com -
Why field length is shorter than the actual one in a SQL query based grid?
Hi,
I have a grid based on a SQL query on a UDO table, but I found when I retrieve the data from a SQL query some column show only part of data. For example, in SQL server the query should return one column data like "ABCD", but in the grid it only shows "A" or "AB". I think the SQL query should be good because when copy the same query executing in SQL server side, it returns me all complete data. But when it is executed in add-on inside a grid, then some column only returns me partial data. In most case it only return the first one or two characters. I don't see any special in the query. Basically it is normal SELECT query, the only possible special is it is using "UNION".
The SQL query looks like below:
SELECT fieldA, fieldB FROM table1
UNION ALL
SELECT fieldA, fieldB FROM table2
When it is executed in SQL side, everything looks good, but when i run it in a grid in add-on then fieldB column only display partial data.
Any idea?
Thanks,
Lan
Edited by: ZHANGLAN on Oct 4, 2011 11:55 PMHi All,
Thanks for all your replies, I agree that the issue is caused by the UNION in SQL query. Because when i create a view in SQL based on that query and the grid is based on that SQL view then everything is fine now. I think Petr's solution should work in this case.
Thank you again!
Lan -
SQL question: group by horizontally
Hi,
I'm having trouble figuring out a way to display the result of a join in the following tabular format. Can someone please assist?
SQL> select * from tab1;
GNAME SNO
ABC 5
DEF 2
ABC 3
SQL> select * from tab2;
GNAME SNO
ABC 10
XYZ 5
DEF 15
My current output using a UNION ALL:
SQL> select gname, count(*) sno1 from tab1 group by gname
2 union all
3 select gname, count(*) sno2 from tab2 group by gname;
GNAME SNO1
ABC 2
DEF 1
ABC 1
DEF 1
XYZ 1
The output I'm expecting:
GNAME SNO1 SNO2
ABC 2 1
DEF 1 1
XYZ 0 1
Thanks in advance.szsl wrote:
My current output using a UNION ALL:
SQL> select gname, count(*) sno1 from tab1 group by gname
2 union all
3 select gname, count(*) sno2 from tab2 group by gname;you need this to identify all distinct gnames.
with tab1 as (
select 'ABC' as gname, 5 as sno from dual union all
select 'DEF' as gname, 2 as sno from dual union all
select 'ABC' as gname, 3 as sno from dual
), tab2 as (
select 'ABC' as gname, 10 as sno from dual union all
select 'XYZ' as gname, 5 as sno from dual union all
select 'DEF' as gname, 15 as sno from dual
select a.gname, sum( case when tab1.gname is not null then 1 else 0 end) as cnt_1,
sum( case when tab2.gname is not null then 1 else 0 end) as cnt_2
from (
select distinct gname from (select gname from tab1 union all select gname from tab2)
) a
left join tab1 on tab1.gname = a.gname
left join tab2 on tab2.gname = a.gname
group by a.gname
order by a.gname
;output:
GNAME CNT_1 CNT_2
ABC 2 2
DEF 1 1
XYZ 0 1 -
Does the union improves the performance?
Hi all,
I would like to know does the union in sql or pl/sql optimizes the performance of the query? here we are retreiveing the data from a remote databse to see about 3700 hundred records it took on oracle 9i 40 seconds to get, where as when we added the union its retriving the data in 2 seconds does the union has anything to do with backend in pl/sql to optimize this query.
CURSOR ecur IS
SELECT 0 id,' No PO' name,'' dir,'' SYMBOL,'' PHONE,'' EMAIL FROM dual
UNION
SELECT id,name,DIR,SYMBOL,PHONE,EMAIL
FROM [email protected]
WHERE dir IN ('A','C','W','V','S','SA','H')
AND name LIKE initcap(match)||'%'
ORDER BY name;
before added the union select statement it took 40 seconds after i added the union its taking 2 seconds.. so i want to know the difference between these two statements why so much difference in performance.
please do help me out...
Thanks a lot.....Do an explain plan of the two queries. Chances are the optimizer decided to use a different plan for the main query when you unioned it with dual.
-
.csv file should not get insert sql query with report
Hi Guys,
the below sqlplus code giving both sql query and records report in csv format file. I want only records csv file report.
SET LINESIZE 1000 TRIMSPOOL ON PAGESIZE 0 FEEDBACK OFF
SPOOL c:\oracle\extract\emp.csv
SELECT empno || ',' || ename || ',' || job || ',' || mgr || ',' || TO_CHAR(hiredate,'DD-MON-YYYY') AS hiredate || ',' || sal || ',' || comm || ',' || deptno
FROM emp ORDER BY ename;
SPOOL OFF
SET PAGESIZE 14
can any one suggest me on this.
Thanks in advance!
Regards,
KLRYou will have to use UNION ALL:
SQL> SET LINESIZE 1000 TRIMSPOOL ON PAGESIZE 0 FEEDBACK OFF
SQL> WITH t AS (
2 SELECT 'empno,ename,job,mgr,hiredate,sal,comm,deptno' str,
3 null ename
4 FROM dual
5 UNION ALL
6 SELECT empno || ',' || ename || ',' || job || ',' || mgr || ',' || TO_CHAR(hiredate,'DD-MON-YYYY') || ',' || sal || ',' || comm || ',' || deptno str,
7 ename
8 FROM emp
9 )
10 SELECT str
11 FROM t
12 ORDER BY ename NULLS FIRST;
empno,ename,job,mgr,hiredate,sal,comm,deptno
7876,ADAMS,CLERK,7788,23-MAY-1987,1100,,20
7499,ALLEN,SALESMAN,7698,20-FEB-1981,1600,300,30
7698,BLAKE,MANAGER,7839,01-MAY-1981,2850,,30
7782,CLARK,MANAGER,7839,09-JUN-1981,2450,,10
7902,FORD,ANALYST,7566,03-DEC-1981,3000,,20
7900,JAMES,CLERK,7698,03-DEC-1981,950,,30
7566,JONES,MANAGER,7839,02-APR-1981,2975,,20
7839,KING,PRESIDENT,,17-NOV-1981,5000,,10
7654,MARTIN,SALESMAN,7698,28-SEP-1981,1250,1400,30
7934,MILLER,CLERK,7782,23-JAN-1982,1300,,10
7788,SCOTT,ANALYST,7566,19-APR-1987,3000,,20
7369,SMITH,CLERK,7902,17-DEC-1980,800,,20
7844,TURNER,SALESMAN,7698,08-SEP-1981,1500,0,30
7521,WARD,SALESMAN,7698,22-FEB-1981,1250,500,30
SQL>
SY. -
Field with time in minutes and seconds
Dear community,
first of all, I am using Apex 3.1 on Oracle 10g, if this is necessary to know (yes I know, an old version, but given by the company I'm working for). After I've developed a few applications in Apex, thank to the forum and all the answers inside, I came to a problem that I could not solve:
I want to have a normal text field, where the user can enter a duration of an event (e.g. "3:45" or "2:30"). That duration will allways be in the format "MM:SS". No hours, no milliseconds... But how can I save the given values in the database, so in which format? A varchar would be a possibility, but I want to build a report later on, where there is a sum of the times. And how to get a sum of varchar?! But how to make it clear to Apex and Oracle DB that the users gives in a time and not a varchar? The only thing oracle offers is a date field, but I want to save a time and not a date!
Does anybody has a clue for me, cause it seems to me, that possibly I just don't know what I'm searching for, cause people must have had this problem before!?
THanks for your help!
Regards
hogeBut how to make it clear to Apex and Oracle DB that the users gives in a time and not a varchar? The only thing oracle offers is a date field, but I want to save a time and not a date!All APEX items are character strings, so implicit or explicit conversion is required when the values are stored in database columns. For the recommended <tt>INTERVAL DAY TO SECOND</tt> columns, use <tt>TO_DSINTERVAL</tt> to perform the conversion.
Use <tt>EXTRACT</tt> to get the minute and second values from the <tt>INTERVAL</tt> going in the other direction.
but I want to build a report later on, where there is a sum of the times.A user-defined aggregate function can be created to sum <tt>INTERVAL DAY TO SECOND</tt> values: here's an example.
APEX's lack of built-in support for summaries on <tt>INTERVAL</tt> types can be worked round by including the sum in the report query using a </tt>UNION</tt>:
SQL> with test_data as (
2 select
3 rownum n
4 , numtodsinterval(ceil(dbms_random.value(0, 3600)), 'SECOND') t
5 from
6 dual
7 connect
8 by rownum <= 10)
9 select
10 n
11 , t
12 , to_char(extract(minute from t))
13 || ':'
14 || to_char(extract(second from t), 'fm00') t1
15 from
16 test_data
17 union all
18 select
19 null
20 , sum_dsinterval(t)
21 , to_char(extract(hour from sum_dsinterval(t)))
22 || ':'
23 || to_char(extract(minute from sum_dsinterval(t)), 'fm00')
24 || ':'
25 || to_char(extract(second from sum_dsinterval(t)), 'fm00')
26 from
27 test_data
28 order by
29 1 nulls last
30 /
N T T1
1 +000000000 00:04:46.000000000 4:46
2 +000000000 00:07:05.000000000 7:05
3 +000000000 00:31:11.000000000 31:11
4 +000000000 00:53:16.000000000 53:16
5 +000000000 00:19:02.000000000 19:02
6 +000000000 00:57:12.000000000 57:12
7 +000000000 00:43:29.000000000 43:29
8 +000000000 00:03:11.000000000 3:11
9 +000000000 00:16:40.000000000 16:40
10 +000000000 00:20:49.000000000 20:49
+000000000 04:16:41.000000000 4:16:41A custom named column (row) report template with conditional templates can be used to layout and format such a report so that the summary row(s) are properly handled. -
Case INSENSITIVE Columns on Oracle
Hello Friends,
Good Monday for everyone....
I would like to ask you guys if there is a way to create a case INSENSITIVE Columns on Oracle. I used on Sqlserver before the COLLATE sintax, and I was able to make a columns (just that one) INSENSITIVE.
I'm using oracle 10gr2 on Windows plataform and herte is my nls_parameters. My ideia is to search on this column without the need of performing a function UPPER and LOWER and etc...
NLS_LANGUAGE BRAZILIAN PORTUGUESE
NLS_TERRITORY BRAZIL
NLS_CURRENCY Cr$
NLS_ISO_CURRENCY BRAZIL
NLS_NUMERIC_CHARACTERS ,.
NLS_CALENDAR GREGORIAN
NLS_DATE_FORMAT DD/MM/RR
NLS_DATE_LANGUAGE BRAZILIAN PORTUGUESE
NLS_CHARACTERSET WE8MSWIN1252
NLS_SORT WEST_EUROPEAN
NLS_TIME_FORMAT HH24:MI:SSXFF
NLS_TIMESTAMP_FORMAT DD/MM/RR HH24:MI:SSXFF
NLS_TIME_TZ_FORMAT HH24:MI:SSXFF TZR
NLS_TIMESTAMP_TZ_FORMAT DD/MM/RR HH24:MI:SSXFF TZR
NLS_DUAL_CURRENCY Cr$
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_COMP BINARY
NLS_LENGTH_SEMANTICS BYTE
NLS_NCHAR_CONV_EXCP FALSE
tks a lot
KeenAPC wrote:
No, they mean a setting which makes "APC" or "apc" match "Apc".
There is nothing to be done on 10g, other than building a function based index on the column in question, so that any UPPER() searches are optimized.
Well, as Kamran Agayev already noted CI is available in 10g too. It also worth mentioning FBI creates a hidden column. Also, your statement
In 11g we have the option to set the NLS_SORT parameter so that any searches are case-insensitive (or indeed accent insensitive). Find out more.
is incomplete. NLS_SORT affects nothing but sort:
SQL> connect scott
Enter password: *****
Connected.
SQL> select * from v$version
2 /
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for 32-bit Windows: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 order by name
12 /
NAM
Joe
Max
Sam
joe
max
sam
6 rows selected.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name = 'max'
12 /
NAM
max
SQL> alter session set nls_sort = binary_ci
2 /
Session altered.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 order by name
12 /
NAM
Joe
joe
max
Max
Sam
sam
6 rows selected.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name = 'max'
12 /
NAM
max
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name like 'm%'
12 /
NAM
max
SQL> select 'Max' name from dual union
2 select 'max' name from dual
3 /
NAM
Max
max
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select distinct name
10 from t
11 /
NAM
sam
Joe
joe
max
Sam
Max
6 rows selected.
SQL> As you can see, NLS_SORT alone works on sort but not on "searches". We also need to set NLS_COMP, which by default is BINARY. Prior to 10g R2 (I am not 100% sure, it could be prior 10g), the only NLS_COMP choice, besides BINARY, was ANSI. However, ANSI does not work with all comparison operators (e.g. does not work for LIKE, UNION, DISTINCT):
SQL> alter session set nls_sort = binary_ci
2 /
Session altered.
SQL> alter session set nls_comp=ansi
2 /
Session altered.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 order by name
12 /
NAM
Joe
joe
max
Max
Sam
sam
6 rows selected.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name = 'max'
12 /
NAM
Max
max
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name like 'm%'
12 /
NAM
max
SQL> select 'Max' name from dual union
2 select 'max' name from dual
3 /
NAM
Max
max
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select distinct name
10 from t
11 /
NAM
sam
Joe
joe
max
Sam
Max
6 rows selected.
SQL> Starting 10g R2 NLS_COMP can be set to LINGUISTIC, which will also work for LIKE and UNION but not for DISTINCT:
SQL> alter session set nls_sort = binary_ci
2 /
Session altered.
SQL> alter session set nls_comp=linguistic
2 /
Session altered.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 order by name
12 /
NAM
Joe
joe
max
Max
Sam
sam
6 rows selected.
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name = 'max'
12 /
NAM
Max
max
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select name
10 from t
11 where name like 'm%'
12 /
NAM
Max
max
SQL> select 'Max' name from dual union
2 select 'max' name from dual
3 /
NAM
Max
SQL> with t as (
2 select 'Max' name from dual union all
3 select 'sam' name from dual union all
4 select 'Joe' name from dual union all
5 select 'max' name from dual union all
6 select 'joe' name from dual union all
7 select 'Sam' name from dual
8 )
9 select distinct name
10 from t
11 /
NAM
sam
Joe
joe
max
Sam
Max
6 rows selected.
SQL> However even LINGUISTIC does not work with:
• CLOB or NCLOB data types
• Object data types
• Table partitions
• Index-organized tables
SY. -
Hi
I have a simple question about querying on Interfaces.
I have an interface called Entity, and 2 implementing classes called Service and ServiceProvider (mapped to tables Services (pk serviceId) and ServiceProviders (pk serviceProviderId)). serviceId and serviceProviderId have independent sequences. In Workbench, both classes are specified as implementing the Entity interface. The Entity interface currently specifies an accessor method getEntityId(), and entityId is a common query key for both classes.
I want to query against the Entity interface to obtain a single instance, by knowing the entityId and class name.
How can I formulate a readObject query so that Toplink generates the appropriate SQL:
select * from services where serviceId = ? or
select * from serviceProviders where serviceProviderId = ?
Sorry for the trivial nature of this question - I can't find any examples of querying against interface descriptors in a search of tutorials or the developers guide. The developers guide simply says "If there are multiple implementors of the interfaces, the query returns instances of all implementing classes." and this sounds like it would translate to a UNION ALL SQL query.
I also have in a separate class a variable one to one mapping where I can specify a class indicator (entityTypeId values). Somehow I feel that I need something like this for the Entity interface descriptor.
JamesFrankly, this just isn't something that is done that often. In the past decade I can only think of a couple of customers who've asked me about this. I always feel a bit uncomfortable with how querying through interfaces really tends to break polymorphism -- it just doesn't seem very natural from an OO perspective. Now, if instead of an interface you had used a common superclass, that makes more sense. I guess the fact that interfaces don't have state makes this whole area a bit of an OOAD research topic. :)
That being said, I don't expect TopLink would do an "or" query like you've shown, but would do a UNION.
My recommendation is to experiement with the queries, perhaps take a look at query-keys (they may be of some help here), and contact support with any feature suggestions you have in this area.
- Don -
How can this bad query be improved?
Db:11.2.0.3
We have a 3rd party app and the web app runs very slow. We want to make the 3rd party to fix the issue. for the
app login process, I did an AWR , found the problem query it runs 10 mins. Then I did the sqltrace
here is it:
select clndr_id , count(*)
from
task where (clndr_id = :"SYS_B_0") group by clndr_id union select clndr_id ,
count(*) from project where (clndr_id = :"SYS_B_1") group by clndr_id
call count cpu elapsed disk query current rows
Parse 1 0.01 0.00 0 0 0 0
Execute 1 0.01 0.00 0 0 0 0
Fetch 2 53.32 612.03 81650 58920 0 2
total 4 53.34 612.04 81650 58920 0 2
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 34 (PX)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
2 2 2 SORT UNIQUE (cr=58923 pr=81650 pw=22868 time=113329109 us cost=58277 size=24 card=2)
2 2 2 UNION-ALL (cr=58923 pr=81650 pw=22868 time=113329001 us)
1 1 1 SORT GROUP BY NOSORT (cr=58330 pr=81070 pw=22868 time=104312437 us cost=58128 size=7 card=1)
5589739 5589739 5589739 VIEW index$_join$_003 (cr=58330 pr=81070 pw=22868 time=619784236 us cost=57240 size=38875249 card=5553607)
5589739 5589739 5589739 HASH JOIN (cr=58330 pr=81070 pw=22868 time=617373467 us)
5590158 5590158 5590158 INDEX RANGE SCAN NDX_TASK_CALENDAR (cr=21676 pr=21676 pw=0 time=113637058 us cost=11057 size=38875249 card=5553607)(object id 24749)
6673774 6673774 6673774 INDEX FAST FULL SCAN NDX_TASK_PROJ_RSRC (cr=36651 pr=36526 pw=0 time=213370625 us cost=21921 size=38875249 card=5553607)(object id 217274)
1 1 1 SORT GROUP BY NOSORT (cr=593 pr=580 pw=0 time=9016527 us cost=149 size=17 card=1)
136390 136390 136390 INDEX FAST FULL SCAN NDX_PROJECT_CALENDAR (cr=593 pr=580 pw=0 time=165434 us cost=132 size=2315876 card=136228)(object id 154409)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
2 SORT (UNIQUE)
2 UNION-ALL
1 SORT (GROUP BY NOSORT)
5589739 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF 'TASK'
(TABLE)
5589739 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_TASK_CALENDAR' (INDEX)
5590158 SORT (GROUP BY NOSORT)
6673774 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_PROJECT_CALENDAR' (INDEX)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
Disk file operations I/O 2 0.00 0.00
db file sequential read 22235 1.61 138.66
direct path write 1620 3.25 177.42
db file scattered read 2313 1.89 238.98
direct path read 385 1.72 19.52
SQL*Net message from client 2 0.11 0.21Please make your comments.
Thanks in Advance.Salman Qureshi wrote:
Hi,
It looks to me that end result will give you distinct values because of distinct clndr_id. If my thinking is correct, can you use UNION ALL istead of UNION? This will reduce your query execution time by not spending time on removing duplication of results (UNION removes duplication).
Do you have fresh statistics on the tables/indexes involved in this query?
SalmanIt is a 3rd party app, not sure the query is exactly doing for, a good input though,
I have given a try as comparison.
Found
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
UNION
select clndr_id , count(*)
from
task where (clndr_id = :"SYS_B_0") group by clndr_id union select clndr_id ,
count(*) from project where (clndr_id = :"SYS_B_1") group by clndr_id
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 31.18 70.39 25288 58920 0 2
total 4 31.18 70.39 25288 58920 0 2
Misses in library cache during parse: 0
Optimizer mode: ALL_ROWS
Parsing user id: 34 (PX)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
2 2 2 SORT UNIQUE (cr=58923 pr=25288 pw=25284 time=70390927 us cost=58277 size=24 card=2)
2 2 2 UNION-ALL (cr=58923 pr=25288 pw=25284 time=70390652 us)
1 1 1 SORT GROUP BY NOSORT (cr=58330 pr=25288 pw=25284 time=70309151 us cost=58128 size=7 card=1)
5589739 5589739 5589739 VIEW index$_join$_003 (cr=58330 pr=25288 pw=25284 time=70027453 us cost=57240 size=38875249 card=5553607)
5589739 5589739 5589739 HASH JOIN (cr=58330 pr=25288 pw=25284 time=68083254 us)
5590158 5590158 5590158 INDEX RANGE SCAN NDX_TASK_CALENDAR (cr=21676 pr=0 pw=0 time=2449897 us cost=11057 size=38875249 card=5553607)(object id 24749)
6673774 6673774 6673774 INDEX FAST FULL SCAN NDX_TASK_PROJ_RSRC (cr=36651 pr=0 pw=0 time=3097204 us cost=21921 size=38875249 card=5553607)(object id 217274)
1 1 1 SORT GROUP BY NOSORT (cr=593 pr=0 pw=0 time=81462 us cost=149 size=17 card=1)
136390 136390 136390 INDEX FAST FULL SCAN NDX_PROJECT_CALENDAR (cr=593 pr=0 pw=0 time=68732 us cost=132 size=2315876 card=136228)(object id 154409)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
2 SORT (UNIQUE)
2 UNION-ALL
1 SORT (GROUP BY NOSORT)
5589739 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF 'TASK'
(TABLE)
5589739 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_TASK_CALENDAR' (INDEX)
5590158 SORT (GROUP BY NOSORT)
6673774 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_PROJECT_CALENDAR' (INDEX)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
direct path write 3347 1.59 43.26
direct path read 130 0.20 0.32
SQL*Net message from client 2 0.23 0.27
********************************************************************************$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
UNION ALL
SQL ID: d6z3ag876m67h Plan Hash: 4277397671
select clndr_id , count(*)
from
task where (clndr_id = :"SYS_B_0") group by clndr_id union all select
clndr_id , count(*) from project where (clndr_id = :"SYS_B_1") group by
clndr_id
call count cpu elapsed disk query current rows
Parse 1 0.00 0.00 0 0 0 0
Execute 1 0.01 0.00 0 0 0 0
Fetch 2 31.77 89.93 22886 58920 0 2
total 4 31.78 89.94 22886 58920 0 2
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 34 (PX)
Number of plan statistics captured: 1
Rows (1st) Rows (avg) Rows (max) Row Source Operation
2 2 2 UNION-ALL (cr=58932 pr=22887 pw=22868 time=89865448 us)
1 1 1 SORT GROUP BY NOSORT (cr=58339 pr=22887 pw=22868 time=89865428 us cost=57240 size=7 card=1)
5589739 5589739 5589739 VIEW index$_join$_003 (cr=58339 pr=22887 pw=22868 time=302390812 us cost=57240 size=38875249 card=5553607)
5589739 5589739 5589739 HASH JOIN (cr=58339 pr=22887 pw=22868 time=300505731 us)
5590158 5590158 5590158 INDEX RANGE SCAN NDX_TASK_CALENDAR (cr=21676 pr=0 pw=0 time=2275780 us cost=11057 size=38875249 card=5553607)(object id 24749)
6673774 6673774 6673774 INDEX FAST FULL SCAN NDX_TASK_PROJ_RSRC (cr=36651 pr=18 pw=0 time=3233656 us cost=21921 size=38875249 card=5553607)(object id 217274)
1 1 1 SORT GROUP BY NOSORT (cr=593 pr=0 pw=0 time=77989 us cost=132 size=17 card=1)
136390 136390 136390 INDEX FAST FULL SCAN NDX_PROJECT_CALENDAR (cr=593 pr=0 pw=0 time=55006 us cost=132 size=2315876 card=136228)(object id 154409)
Rows Execution Plan
0 SELECT STATEMENT MODE: ALL_ROWS
2 UNION-ALL
1 SORT (GROUP BY NOSORT)
5589739 TABLE ACCESS MODE: ANALYZED (BY INDEX ROWID) OF 'TASK'
(TABLE)
5589739 INDEX MODE: ANALYZED (RANGE SCAN) OF 'NDX_TASK_CALENDAR'
(INDEX)
5590158 SORT (GROUP BY NOSORT)
6673774 INDEX MODE: ANALYZED (RANGE SCAN) OF
'NDX_PROJECT_CALENDAR' (INDEX)
Elapsed times include waiting on following events:
Event waited on Times Max. Wait Total Waited
---------------------------------------- Waited ---------- ------------
SQL*Net message to client 2 0.00 0.00
Disk file operations I/O 2 0.00 0.00
direct path write 2069 3.12 58.90
db file sequential read 18 0.78 4.35
direct path read 22 0.12 0.15
SQL*Net message from client 2 0.46 0.71
********************************************************************************found union all used 90s vs union 70s, and the logic and the physical reads are about the same.
Notice that we now have it ran less that 2 mins for the query vs 10 mins before.
The before was at the peak hrs and now it is off the peak. the disk read from peak's 81650 reduced to 22886.
that was the cause of slow -- I/O contentions at peak hrs.
How can we improve the query performance by increasing disk throughput?
SQL> show parameter mem
NAME TYPE VALUE
hi_shared_memory_address integer 0
memory_max_target big integer 4000M
memory_target big integer 3600M
shared_memory_address integer 0
SQL> show parameter db_b
NAME TYPE VALUE
db_block_buffers integer 0
db_block_checking string FALSE
db_block_checksum string TYPICAL
db_block_size integer 8192
SQL> -
EXCEPT OR MINUS IN QUERY MANAGER
Hi,
it Can say me how it do in query manager an query with EXCEPT (MINUS)???
For example:
SELECT CardCode FROM OCRD
EXCEPT
SELECT CardCode FROM ORDR
Thanks...Hi
I am not sure wether there is any operator opposite to Union in sql server(i.e. intersects).
But if the number of fields that are to be compared are less then you can use the query as follows
SELECT CardCode,DocEntry FROM ORDR Where CardCode + cast(DocEntry as varchar) not in( SELECT CardCode+ cast(DocEntry as varchar) From OINV)
Hope this will help you
Regards
Vishnu -
How and Which Hint will be able to replicate the plan?
hi All, I am using 10.2.0.4.0.
I am having one query which is having different plans in different environment(databases) as below. I want to verify the performance of the query by forcing the optimizer to use plan as that of 'Environment -2' in 'Environment -1' as below. So how can i force the optimizer(using hint) to use the 'CONCATENATION' path rather than 'BITMAP CONVERSION TO ROWIDS' path of execution. (Note-Query in 'Environment -1' completes in ~3 minutes but in 'Environment -2' completes in ~1.5 minutes.)
Query:
select
FROM BASE XB,
MAP CC
WHERE (XB.BUYERPK = CC.PARENTPK
OR XB.BUYERPK = CC.CHILDPK)
and cc.CHILDPK= 3914297344;
Plan in Environment -1 :
Execution Plan
Plan hash value: 1223568078
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 14644 | 3846K| 2444 (7)| 00:00:13 |
| 1 | NESTED LOOPS | | 14644 | 3846K| 2444 (7)| 00:00:13 |
| 2 | TABLE ACCESS BY INDEX ROWID | MAP | 1 | 14 | 1 (0)| 00:00:01 |
|* 3 | INDEX UNIQUE SCAN | PARENT_CHILD_P1 | 1 | | 1 (0)| 00:00:01 |
| 4 | TABLE ACCESS BY INDEX ROWID | BASE | 14644 | 3646K| 2444 (7)| 00:00:13 |
| 5 | BITMAP CONVERSION TO ROWIDS | | | | | |
| 6 | BITMAP OR | | | | | |
| 7 | BITMAP CONVERSION FROM ROWIDS| | | | | |
| 8 | SORT ORDER BY | | | | | |
|* 9 | INDEX RANGE SCAN | IDX_byuer_supplyPK | | | 3 (34)| 00:00:01 |
| 10 | BITMAP CONVERSION FROM ROWIDS| | | | | |
| 11 | SORT ORDER BY | | | | | |
|* 12 | INDEX RANGE SCAN | IDX_byuer_supplyPK | | | 3 (34)| 00:00:01 |
Predicate Information (identified by operation id):
3 - access("CC"."CHILDPK"=3914297344)
9 - access("XB"."BUYERPK"="CC"."PARENTPK")
filter("XB"."BUYERPK"="CC"."PARENTPK")
12 - access("XB"."BUYERPK"="CC"."CHILDPK")
filter("XB"."BUYERPK"="CC"."CHILDPK")
Plan in Environment -2 :
Execution Plan
Plan hash value: 931058577
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 126K| 32M| 6173 (3)| 00:00:32 |
| 1 | CONCATENATION | | | | | |
| 2 | NESTED LOOPS | | 126K| 32M| 3086 (3)| 00:00:16 |
| 3 | TABLE ACCESS BY INDEX ROWID| MAP | 1 | 14 | 1 (0)| 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | PARENT_CHILD_P1 | 1 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS BY INDEX ROWID| BASE | 126K| 30M| 3085 (3)| 00:00:16 |
|* 6 | INDEX RANGE SCAN | IDX_byuer_supplyPK | 7261 | | 3 (34)| 00:00:01 |
| 7 | NESTED LOOPS | | 164 | 43952 | 3086 (3)| 00:00:16 |
| 8 | TABLE ACCESS BY INDEX ROWID| MAP | 1 | 14 | 1 (0)| 00:00:01 |
|* 9 | INDEX UNIQUE SCAN | PARENT_CHILD_P1 | 1 | | 1 (0)| 00:00:01 |
| 10 | TABLE ACCESS BY INDEX ROWID| BASE | 164 | 41656 | 3085 (3)| 00:00:16 |
|* 11 | INDEX RANGE SCAN | IDX_byuer_supplyPK | 7261 | | 3 (34)| 00:00:01 |
Predicate Information (identified by operation id):
4 - access("CC"."CHILDPK"=3914297344)
6 - access("XB"."BUYERPK"="CC"."CHILDPK")
9 - access("CC"."CHILDPK"=3914297344)
11 - access("XB"."BUYERPK"="CC"."PARENTPK")
filter(LNNVL("XB"."BUYERPK"="CC"."CHILDPK"))
Index 'IDX_byuer_supplyPK' is on (BUYERPK, supplypk);Since we don't know about the data structures involved or the possible volume of data, the better generic solution is to have a UNION ALL and add the predicate: LNNVL("XB"."BUYERPK"="CC"."CHILDPK") to the second query block. (In this case you query looks as if it would produce the right answer efficiently - but the correctness depends on the various PK columns actually being primary keys, and we shouldn't assum that).Hi Jonathan,
Thanks for pointing out this important oversight.
Just to clarify the difference with a noddy example...
If we do have duplicates:
SQL> create table t1
2 (par number
3 ,child number);
Table created.
SQL> create table t2
2 (col1 number
3 ,col2 varchar2(1));
Table created.
SQL> insert into t1 values (1,1);
1 row created.
SQL> insert into t1 values (1,1);
1 row created.
SQL> insert into t2 values (1,'A');
1 row created.
SQL> select * from t1;
PAR CHILD
1 1
1 1
SQL> select * from t2;
COL1 C
1 A
SQL> And this is the OR we want to rewrite:
SQL> select *
2 from t2, t1
3 where t2.col1 = par
4 or t2.col1 = child;
COL1 C PAR CHILD
1 A 1 1
1 A 1 1
2 rows selected.
SQL> Then the UNION is not equivalent because a UNION removes duplicates:
SQL> select *
2 from t2, t1
3 where t2.col1 = par
4 union
5 select *
6 from t2, t1
7 where t2.col1 = child;
COL1 C PAR CHILD
1 A 1 1
1 row selected.
SQL> And a UNION ALL:
SQL> select *
2 from t2, t1
3 where t2.col1 = par
4 union all
5 select *
6 from t2, t1
7 where t2.col1 = child;
COL1 C PAR CHILD
1 A 1 1
1 A 1 1
1 A 1 1
1 A 1 1
4 rows selected.
SQL> requires the LNVL to avoid the duplicates across the two branches:
SQL> select *
2 from t2, t1
3 where t2.col1 = par
4 union all
5 select *
6 from t2, t1
7 where t2.col1 = child
8 and lnnvl(t1.par=t1.child);
COL1 C PAR CHILD
1 A 1 1
1 A 1 1
2 rows selected.
SQL> Cheers,
Dominic
Maybe you are looking for
-
File window not updating modif. dates – and mysterious parallel universe discovered
The day started innocently enough when I tried to edit some <meta> content on a site I'm building and then saved my work. Little did I know... DW's "File" window refused to update the "Modified" column of the files I worked on. Open and closing the
-
Energy saver not coming on when lid closed
I'm not sure if this because I changed some setting or because I did the battery sw upgrade but now when IU close the lid on my MBR it does not sleep. The apple on the back of it stays lit, and it looks like the display never goes off. Is there a set
-
LMS 3.2 with RME 4.3.1 not Archiving configuration
HI , I am facing problem in Ciscoworks in Fetching config Archieve from Cisco ASA-5510 Adaptive Security Appliance . I am using LMS 3.2 and RME 4.3.1 SSH is enable in the device, and from LMS server I am able to connect through Putty. Also from Manag
-
Smartforms debugg in production server
hi all, I want to debugg smartforms in production server. How to debugg . Please suggest me. Regard jyoti prakash
-
Why do i have to answer security questions when i try to download
please help me on this subject