Query with many sub-selects on the same table
Hi,
Please consider the example below (Oracle 11g):
create TABLE test1 (order_id number, CODE VARCHAR2(10), VALUE NUMBER);
insert into test1 VALUES (1, 'A', 100);
insert into test1 VALUES (1, 'B', 200);
insert into test1 VALUES (1, 'C', 300);
insert into test1 VALUES (1, 'D', 400);
insert into test1 VALUES (2, 'A', 10);
insert into test1 VALUES (2, 'B', 20);
SELECT order_id,
CODE,
VALUE,
(SELECT VALUE FROM test1 t1
WHERE t1.order_id = t.order_id
AND t1.code = 'B') b_Value,
(SELECT SUM(VALUE)/COUNT(*) FROM test1 t1
WHERE t1.order_id = t.order_id
AND t1.code IN ('C', 'D')) cd_Value,
(SELECT COUNT(*) FROM test1 t1
WHERE t1.order_id = t.order_id
AND t1.code IN ('C', 'D')) cd_qty
FROM TEST1 t
WHERE CODE = 'A';In my real-life case, I don't have 3 but tens of sub-query columns like those. The performance is OK but I was wondering whether there were a better way in terms of performance and maintainability to write this query.
I thought of the WITH clause but couldn't see how it could help much.
Please note that some sub-queries can be more complex than just looking up a column, like in column cd_value/cd_qty.
Any suggestions?
Thanks
Luis
Simpler, but will go for two table scans. You will have to use outer joins according to your requirement.
select t1.order_id,
t1.CODE,
t1.VALUE,
max(decode(t2.code,'B',t2.value)) b_value,
sum(decode(t2.code,'C',t2.value,'D',t2.value,0))/nullif(sum(decode(t2.code,'C',1,'D',1,0)),0) cd_value,
sum(decode(t2.code,'C',1,'D',1,0)) cd_qty
from TEST1 t1,test1 t2
where t1.CODE = 'A'
and t1.ORDER_ID = t2.order_id
group by t1.order_id,
t1.CODE,
t1.VALUEEdited by: jeneesh on Feb 7, 2012 11:54 AM
Similar Messages
-
URGENT!Problem running a Query with a Subquery that includes the same table
Hello all,
Currently we are working over Oracle Database 10g Release 2 (10.2.0.3) Patch Set1.
We have registered a schema called ICRI and we have created two VIEW over this schema to work in some occasions.
CREATE OR REPLACE VIEW "ICRI_RELACIONAL_VIEW"
(IDICRI, NOMBRERECURSO, VERSIONRECURSO) AS
SELECT extractValue(value(i), '/ICRI/ID/text()'),
extractValue(value(i), '/ICRI/NombreRecurso/text()'),
extractValue(value(i), '/ICRI/VersionRecurso/text()') || '.' || extractValue(value(m), '/Modificacion/Secuencia/text()'),
FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Modificaciones/Modificacion'))) m
WHERE extractValue(value(m), '/Modificacion/Secuencia/text()') =
(SELECT max(extractValue(value(s), '/Secuencia/text()'))
FROM table(xmlsequence(extract(value(i),'/ICRI/Modificaciones/Modificacion/Secuencia'))) s)
WITH READ ONLY;
CREATE OR REPLACE VIEW "ICRI_DOMINIOS_VIEW"
(ID, DOMINIO) AS
SELECT extractValue(value(i), '/ICRI/ID/text()'),
extractValue(value(a), '/Dominio/text()', 'xmlns="http://www.orswegimoarmada.es/ORSWE/ICRI"')
FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Dominios/Dominio'))) a
WITH READ ONLY;We have created 5000 XML documents based in this schema and stored in the database.
Now we have executed different querys to obtain certain data.
* QUERY 1
SELECT COUNT(*) FROM ICRI_DOMINIOS_VIEW V1, ICRI_DOMINIOS_VIEW V2
WHERE V1.ID = V2.ID AND V1.DOMINIO = 'Mar' AND V2.DOMINIO = 'Tierra'Time: 38sg. 1 row, Value: 1097.
* QUERY 2
SELECT COUNT(*) FROM ICRI_DOMINIOS_VIEW V1
WHERE V1.DOMINIO = 'Mar'
AND
V1.ID IN (SELECT V2.ID FROM ICRI_DOMINIOS_VIEW V2
WHERE V2.DOMINIO = 'Tierra')Time: 34sg. 1 row, Value: 1097.
* QUERY 3 (XPath Version)
SELECT COUNT(*)
FROM ICRI
WHERE existsNode(object_value, '/ICRI/Dominios[Dominio="Mar" and Dominio="Tierra"]')=1
Time: 32msg. 1 row, Value: 1097.
* QUERY 4 (Version XPath)
SELECT extractValue(object_value, '/ICRI/ID/text()')
FROM ICRI
WHERE existsNode(object_value, '/ICRI/Dominios[Dominio="Mar" and Dominio="Tierra"]')=1
Time: 63mseg. 1097 rows.
* QUERY 5
SELECT V1.ID FROM ICRI_DOMINIOS_VIEW V1, ICRI_DOMINIOS_VIEW V2
WHERE V1.ID = V2.ID AND V1.DOMINIO = 'Mar' AND V2.DOMINIO = 'Tierra'
Time: 15sg. 1097 rows.
* QUERY 6
SELECT V1.ID FROM ICRI_DOMINIOS_VIEW V1
WHERE V1.DOMINIO = 'Mar'
AND
V1.ID IN (SELECT V2.ID FROM ICRI_DOMINIOS_VIEW V2
WHERE V2.DOMINIO = 'Tierra')
Time: 26sg. 1097 rows.
Now, with the next query, we have found an important issue in Oracle, because this query doesn't return any row.
* QUERY 7
SELECT extractValue(value(i), '/ICRI/ID/text()') ID,
extractValue(value(i), '/ICRI/NombreRecurso/text()') NOMBRE,
extractValue(value(i), '/ICRI/VersionRecurso/text()') || '.' || extractValue(value(m), '/Modificacion/Secuencia/text()') VERSION
FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Modificaciones/Modificacion'))) m
WHERE
(extractValue(value(m), '/Modificacion/Secuencia/text()') =
(SELECT max(extractValue(value(s), '/Secuencia/text()'))
FROM table(xmlsequence(extract(value(i),'/ICRI/Modificaciones/Modificacion/Secuencia'))) s)
AND
(extractValue(value(i), '/ICRI/ID/text()') IN
(select extractValue(object_value, '/ICRI/ID/text()') ID
FROM ICRI
WHERE (existsNode(object_value, '/ICRI/Dominios[Dominio="Mar" and Dominio="Tierra"]')=1)
)Time 607mseg. 0 rows.
* QUERY 8
SELECT VI.IDICRI, VI.NOMBRERECURSO, VI.VERSIONRECURSO
FROM ICRI_RELACIONAL_VIEW VI, (SELECT V1.ID FROM ICRI_DOMINIOS_VIEW V1, ICRI_DOMINIOS_VIEW V2
WHERE V1.ID = V2.ID AND V1.DOMINIO = 'Mar' AND V2.DOMINIO = 'Tierra') V3
WHERE VI.IDICRI = V3.ID Time: 16sg. 1097 rows.
* QUERY 9
SELECT VI.IDICRI, VI.NOMBRERECURSO, VI.VERSIONRECURSO
FROM ICRI_RELACIONAL_VIEW VI
WHERE VI.IDICRI IN
(SELECT V1.ID FROM ICRI_DOMINIOS_VIEW V1
WHERE V1.DOMINIO = 'Mar'
AND
V1.ID IN (SELECT V2.ID FROM ICRI_DOMINIOS_VIEW V2
WHERE V2.DOMINIO = 'Tierra')Time: 34 sg. 1097 rows.
* QUERY 10
SELECT extractValue(value(i), '/ICRI/ID/text()') ID,
extractValue(value(i), '/ICRI/NombreRecurso/text()') NOMBRE,
extractValue(value(i), '/ICRI/VersionRecurso/text()') || '.' || extractValue(value(m), '/Modificacion/Secuencia/text()') VERSION
FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Modificaciones/Modificacion'))) m,
(SELECT V1.ID FROM ICRI_DOMINIOS_VIEW V1, ICRI_DOMINIOS_VIEW V2
WHERE V1.ID = V2.ID AND V1.DOMINIO = 'Mar' AND V2.DOMINIO = 'Tierra') V3
WHERE
(extractValue(value(m), '/Modificacion/Secuencia/text()') =
(SELECT max(extractValue(value(s), '/Secuencia/text()'))
FROM table(xmlsequence(extract(value(i),'/ICRI/Modificaciones/Modificacion/Secuencia'))) s)
AND
extractValue(value(i), '/ICRI/ID/text()') = V3.ID
Time: 15sg. 1097 rows.
* QUERY 11
SELECT extractValue(value(i), '/ICRI/ID/text()') ID,
extractValue(value(i), '/ICRI/NombreRecurso/text()') NOMBRE,
extractValue(value(i), '/ICRI/VersionRecurso/text()') || '.' || extractValue(value(m), '/Modificacion/Secuencia/text()') VERSION
FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Modificaciones/Modificacion'))) m
WHERE
(extractValue(value(m), '/Modificacion/Secuencia/text()') =
(SELECT max(extractValue(value(s), '/Secuencia/text()'))
FROM table(xmlsequence(extract(value(i),'/ICRI/Modificaciones/Modificacion/Secuencia'))) s)
AND
(extractValue(value(i), '/ICRI/ID/text()') IN (SELECT V1.ID FROM ICRI_DOMINIOS_VIEW V1
WHERE V1.DOMINIO = 'Mar'
AND
V1.ID IN (SELECT V2.ID FROM ICRI_DOMINIOS_VIEW V2
WHERE V2.DOMINIO = 'Tierra'))
)Time: 30sg. 1097 rows.
* QUERY 12
SELECT extractValue(value(i), '/ICRI/ID/text()') ID,
extractValue(value(i), '/ICRI/NombreRecurso/text()') NOMBRE,
extractValue(value(i), '/ICRI/VersionRecurso/text()') || '.' || extractValue(value(m), '/Modificacion/Secuencia/text()') VERSION
FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Modificaciones/Modificacion'))) m
WHERE
(extractValue(value(m), '/Modificacion/Secuencia/text()') =
(SELECT max(extractValue(value(s), '/Secuencia/text()'))
FROM table(xmlsequence(extract(value(i),'/ICRI/Modificaciones/Modificacion/Secuencia'))) s)
)Time: 187msg. 5000 rows.
Well, if we execute the query based in a relational view all work fine but the performance of the query is hugely decreased. If we try to execute the query based in the XPath values, this options must be the correct, the query doesn't return any result.
Any idea to solve this problem? For us it is very important to find a solution as soon as possible, because our development time is finishing.
Our clients have installed Oracle Client 10.2.0.1 & ODAC 10.2.0.20.
Thanks in advance for your help,
David.SQL> alter session set optimizer_features_enable='10.1.0';
Session altered.
SQL> SELECT count(*)
2 FROM ICRI i, table(xmlsequence(extract(value(i), '/ICRI/Modificaciones/Mo
dificacion'))) m
3 WHERE
4 extractValue(value(i), '/ICRI/ID/text()') IN
5 (select extractValue(object_value, '/ICRI/ID/text()') ID
6 FROM ICRI
7 WHERE (existsNode(object_value,
8 '/ICRI/Dominios[Dominio="Mar" and Dominio="Tierra"]')=1))
9 /
COUNT(*)
5
Test this with a few of your queries and see if the results are expected.
if so I am thinking it is close to bug 5585187
QUERY NOT RETURNING PROPER RESULTS WITH INLINE VIEWS
Fixed in 11.
I am going to see if I can get an env to see if your TC works with this fix before I confirm it 100 percent.
Also note this was done with a very scaled down version of your testcase. Using only one XML doc
regards
Coby
Message was edited by: Coby
coadams -
Interactive report with detail sub-report on the same page
Hi all,
I'm fairly new to APEX, sorry if my question is too obvious. I'm using Apex 4.0.2 on top of Oracle 11.2.
I have an interactive report listing the content of a table. Since there are many columns in that table I would the report to show only a few identifying columns and the complete record currently selected being displayed using another report in a separate region on the same page.
The "Link Column" on the interactive report does almost that except that it jumps on another page; I would like to simply have a sub-report updated with the selected data instead of a new page displayed. Does anyone know how to do that?
Thanks for your help,
ChrisChris,
this sounds like what I normally do in my referential data.
I use the wizard to create "form with report"
I make sure that both regions (the form and the report) will be set on the same page number
then I check/change the order of the regions (report first, form second)
then I set the form to display in column 2
regards,
Richard
P.S. the report only shows enough columns to recognize the records. the form shows all records -
Query with ROWNUM - does it scan the entire table? or returns once the limit is reached?
Suppose I have a table with 50k records.
I am running a query like the one below
select *
from sometable
where ROWNUM < 10 AND AnotherCondition='y'
In the above query, I can think of two possible ways to come up with the answer
1. Scan and apply filter condition on ALL the rows, and return the first 10
2. Do the things mentioned in option (1) but stop scanning once the record limit of 10 is reached. Don't scan the rest since it is not required.
I would like to know which of the following approaches does Oracle follow? Or, does it follow any other strategy that is not mentioned above?
Has anyone done any tests to find out the answer for the above question? If so, could you please share the findings?
Thanks in advance.
Regards,
Vivek GanesanThanks! But where would I run the sql statement. When anyone launches the application it creates the database files in their user directory. How would I connect to the database after that to execute the statement?
I see the create table statements in the files I have pulled into NetBeans in both the source folder and the distribution folder. Could I add the statement there before the table is created in the jar file in the distribution folder and then re-compile it for distribution? OR would I need to add it to the file in source directory and recompile those to create a new distribution?
Thanks! -
Optimization of an update that uses multiple selects on the same table
Oracle version : 10g
Hello,
I have created an UPDATE statement which uses two selects on a single table to do an update according to date criteria.
Due to the fact that the update has different criteria for updating according to whether the date falls within one of two ranges
(future or past) two selects were used together with a union between them to unite the results.
The end result is of course two full table scans and a poorly performing execution.
The goal is to update a field XDEL (type number) based on a date column value (column XDATE) where the dates of interest
fall both in the past (less than sysdate minus 45 days) and in the future (greater than sysdate +90 days). For records with XDATE
values within these ranges, I wish to set field XDEL=1
For records in the future (>=sysdate+90):
for a unique combination of XNAME+XCODE, where XDATE is within the date range for update, set XDEL=1
but for a non-unique combination of XNAME+XCODE+XDATE, update XDEL=1 only for the 'oldest' future record, ie update XDEL for MIN(XDATE) only
For records in the future (< sysdate+45):
for a unique combination of XNAME+XCODE, where XDATE is within the date range for update, set XDEL=1
for a non-unique combination of XNAME+XCODE, update XDEL=1 only for the 'newest' past record, ie update XDEL for MAX(XDATE) only
The combination of XNAME+XCODE XDATE is unique. As an example of the 'in the past' scenario:
XNAME | XCODE | XDATE
================
1. AAAA ~ 002 ~ 01/01/2006
2. AAAA ~ 002 ~ 02/01/2006
3. AAAA ~ 002 ~ 03/01/2006
4. XXXX ~ 123 ~ 02/01/2006
Here, we would update XDEL in record 3. as it corresponds to MAX(XDATE) for the GROUP AAA002 (and we would also update record 4. as a unique record that meets the criteria)
As an example of the 'in the future' scenario:
XNAME | XCODE | XDATE
================
1. HHHH ~ 002 ~ 01/01/2011
2. HHHH ~ 002 ~ 02/01/2011
3. HHHH ~ 002 ~ 03/01/2011
4. XXXX ~ 123 ~ 02/01/2011
Here, we would update XDEL in record 1. as it corresponds to MIN(XDATE) for the GROUP HHHH002 (and of course 4. as a unique record that meets the criteria)
Here is a query that works, but is slow:
UPDATE TAB1 SET XDEL=1 WHERE (XNAME,XCODE,XDATE) IN ((SELECT XNAME,XCODE,max(XDATE) FROM TAB1 WHERE XDATE < sysdate-45 GROUP BY XNAME,XCODE) UNION (SELECT XNAME,XCODE,min(XDATE) from TAB1 WHERE XDATE >= sysdate+90 GROUP BY XNAME,XADDR));
XDATE is a DATE type
XCODE and XADDR are VARCHAR2
XDEL is number type
Any ideas would be greatly appreciated.Here is a suggestion. The code is not tested.
update tab1 t1
set xdel = 1
where exists (select null
from (
select xname,
xcode,
max(case when xdate < sysdate-45 then xdate else null end) xdate1,
min(case when xdate >= sysdate+90 then xdate else null end) xdate2
from tab1
group by xname, xcode
) t
where t1.xname = t.xname
and t1.xcode = t.xcode
and (t1.xdate = t.xdate1 or t1.xdate = t.xdate2)) -
Update and Select from the same table
Hello,
i have this select - i tested it and its working
[code]
select
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTBT
else DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT
end
as KTBT_ISO,
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTZN
else DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN
end
as KTZN_ISO,
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTAB
else DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB
end as KTAB_ISO,
DTF_REEW_201301.brnrn,
DTF_REEW_201301.ktat
from
reewcore.CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
reewdq.DTF_REEW_201301 DTF_REEW_201301
where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1)
[/code]
With the result from KTBT_ISO, KTZN_ISO and KTAB_ISO i want to update the table "reewdq.DTF_REEW_201301" - and this table is in the select-statement!
I believe, i tried every update-statement - but no update is working.
I need something like this:
[code]
update reewdq.DTF_REEW_201301 t1 set t1.KTBT_ISO=
select
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTBT
else DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT
end
as KTBT_ISO
from
reewcore.CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
reewdq.DTF_REEW_201301 DTF_REEW_201301
where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1)
t1.KTZN_ISO=
select
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTZN
else DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN
end
as KTZN_ISO
from
reewcore.CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
reewdq.DTF_REEW_201301 DTF_REEW_201301
where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1)
t1.KTAB_ISO=
select
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTAB
else DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB
end as KTAB_ISO
from
reewcore.CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
reewdq.DTF_REEW_201301 DTF_REEW_201301
where
(DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT)
and CTF_REEW_KUM_201301.KTMO = (DTF_REEW_201301.KTMO - 1)
[/code]
But this code isn´t working. Has someone an idea please? Can someone please help me.
Best regards
HeidiUse MERGE:
merge
into reewdq.DTF_REEW_201301 t1
using (
select case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTBT
else DTF_REEW_201301.KTBT - CTF_REEW_KUM_201301.KTBT
end as KTBT_ISO,
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTZN
else DTF_REEW_201301.KTZN - CTF_REEW_KUM_201301.KTZN
end as KTZN_ISO,
case
when DTF_REEW_201301.KTMO =1 then DTF_REEW_201301.KTAB
else DTF_REEW_201301.KTAB - CTF_REEW_KUM_201301.KTAB
end as KTAB_ISO,
reewdq.ROWID as rid
from reewcore.CTF_REEW_KUM_201301 CTF_REEW_KUM_201301,
reewdq.DTF_REEW_201301 DTF_REEW_201301
where DTF_REEW_201301.BRNRN = CTF_REEW_KUM_201301.BRNRN
and DTF_REEW_201301.KTAT = CTF_REEW_KUM_201301.KTAT
and CTF_REEW_KUM_201301.KTMO = DTF_REEW_201301.KTMO - 1
) t2
on (
t1.rowid = t2.rid
when mathed
then
update
set t1.KTBT_ISO = t2.KTBT_ISO,
t1.KTZN_ISO = t2.KTZN_ISO,
t1.KTAB_ISO = t2.KTAB_ISO
SY. -
Hi, excuse me for silly question, but I can't get on with subject.
Please, see my @Entity:
@Entity
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="DEPARTMENT_SEQ")
@SequenceGenerator(name="DEPARTMENT_SEQ", sequenceName="DEPARTMENT_SEQ", allocationSize=1)
@Column(name="ID")
private Long id;
@Column(name="PARENT_ID")
private Long parentId;
@Column(name="LABEL")
private String label;
Each department can have one parent (except root department). Each department can have 0-* children (sub-departments).
Is it possible to realize tree structure using JPA 1.x?This example seems to demonstrate what you want:
http://www.java2s.com/Code/Java/JPA/Relationsononeentity.htm -
UPDATE involving the same table in sub query
DB version: 11.2
We have a table called SHP_GC_TRACK which has around 8 million records with partitions . In the below UPDATE, it is updating a column based on the SELECT on the same table in a subquery.
UPDATE shp_gc_track a
SET f_tran_proc = 'Y'
WHERE last_update_date <
(SELECT MAX (last_update_date)
FROM shp_gc_track b
WHERE a.shp_trx_rowid = b.shp_trx_rowid
AND a.c_shp_inst = b.c_shp_inst
AND a.f_tran_proc = b.f_tran_proc
AND b.f_ltr_received = 'D'
AND f_rec_code IN ('G', 'W')
AND b.f_rec_status = 'B'
AND b.c_shp_inst = :b1
AND a.c_shp_inst = :b1
AND a.f_ltr_received = 'D' -----------------> part of composite index
AND a.f_tran_proc = 'N' -----------------> part of composite index
AND a.f_rec_code IN ('G', 'W') --------------> part of composite index
AND a.f_rec_status = 'B'; -----------------> part of composite index This UPDATE takes a long time to execute and sometime get hung.
We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status, f_tran_proc . Explain plan shows this composite index is being used.
Is there anyway to rewrite this query or any other suggestions ?Steve_74 wrote:
DB version: 11.2
We have a table called SHP_GC_TRACK which has around 8 million records with partitions . In the below UPDATE, it is updating a column based on the SELECT on the same table in a subquery.
UPDATE shp_gc_track a
SET f_tran_proc = 'Y'
WHERE last_update_date <
(SELECT MAX (last_update_date)
FROM shp_gc_track b
WHERE a.shp_trx_rowid = b.shp_trx_rowid
AND a.c_shp_inst = b.c_shp_inst
AND a.f_tran_proc = b.f_tran_proc
AND b.f_ltr_received = 'D'
AND f_rec_code IN ('G', 'W')
AND b.f_rec_status = 'B'
AND b.c_shp_inst = :b1
AND a.c_shp_inst = :b1
AND a.f_ltr_received = 'D' -----------------> part of composite index
AND a.f_tran_proc = 'N' -----------------> part of composite index
AND a.f_rec_code IN ('G', 'W') --------------> part of composite index
AND a.f_rec_status = 'B'; -----------------> part of composite index This UPDATE takes a long time to execute and sometime get hung.
We have a composite index on four columns f_ltr_received, f_rec_code, f_rec_status, f_tran_proc . Explain plan shows this composite index is being used.
Is there anyway to rewrite this query or any other suggestions ?Tuning updates with subqueries can be hard :(. Sadly my suggestions below are of the try-it-and-see-what-happens variety - nothing certain
First, check the index. Is it bitmap or b-tree? If b-tree see if the most restrictive columns are listed first - this can help with b-tree index efficiency. Also if b-tree a composite bitmap for columns with lots of repeating values instead might help
Its a correlated subquery so you can't just run the subquery first putting the result into a scalar varaiable and using the variable in the SQL instead. You can try putting the results of the subuqery w/join keys in a GTT first using the GTT in the SQL to see if I/O is reduced overall during both operations.
Do you have the licence for the parallel query option? Using parallel DML (this must be turned on manually) might help. Check the docs for the ALTER SESSION command to do this. Also, the PARALLEL_INDEX() hint might help
Post the execution plan of the SQL -
I was re-naming a folder with many sub folders attached, suddenly, Thunderbird did not respond and the folder I was re-naming disappeared along with all the sub folders attached to it. How can I get them back? I am not a computer savvy person. Any help would be greatly appreciated.
Thank you.
[email protected]Turned out that my anti-virus was doing a scheduled scan and had found a virus in an email attachment in my deleted items folder, thus nothing being able to do during the scan.
-
Many of the times my Iphone 3G 16GB shows "No Service" in the specific network area, but if the same sim card is used with other mobile handset in the same network area its shows full network, Is this a Iphone 3G Handset problem or is it a problem with network service provider for iphone in india with Airtel.
Try to reset Network setting thru (Setting/General/Reset/Reset Network Setting - after clicking on it the phone will ask to reboot)
According to my R&D I have experienced that one must reset the network each time when we r at home or in office or other places, the phone after resetting the network setting acquires the area specific network setting n works well, try it & if any other solution do let me known
Calling up customer care did not help me, they do keep us in a loop with the same old answeres that “Our technical team is working on it & WILL GET BACK TO u” leaving us with no solution at the end
Try what I suggest think to will help u -
Updating values with in the same table for other records matching conditions
Hi Experts,
Sorry for not providing the table structure(this is simple structure)
I have a requirement where i need to update the columns of a table based on values of the same table with some empid and date match. If the date and empid match then i have take these values from other record and update the one which is not having office details. I need the update query
Before update my table values are as below
Sort_num Emp_id office start_date
1 101 AUS 01/01/2013
2 101 01/01/2013
3 101 15/01/2013
4 103 USA 05/01/2013
5 103 01/01/2013
6 103 05/01/2013
7 104 FRA 10/01/2013
8 104 10/01/2013
9 104 01/01/2013
After update my table should be like below
Sort_num Emp_id office start_date
1 101 AUS 01/01/2013
2 101 AUS 01/01/2013
3 101 15/01/2013
4 103 USA 05/01/2013
5 103 01/01/2013
6 103 USA 05/01/2013
7 104 FRA 10/01/2013
8 104 FRA 10/01/2013
9 104 01/01/2013
Thanks in advanceI do not have time to create the table with data but basically you should be able to code the following
update table a
set office = ( select office from table b where b.emp_id = a.emp_id
and b.start_date = a.start_date
and b.office is not null
where exists ( [same query as in set] )
and a.office is null
I believe that will do the trick.
HTH -- Mark D Powell -- -
LookUp to the same table with multiple conditions
Hi,
I nead to do a lookup to the same table in the flow but with diffrent quieres, each query contains it's own 'where'.
Can I do it somehow in one look up or do I have to use a few ?
select a from table where a=1
select b from table where c=3
ThanksHi,
Using multiple lookups will be a cleaner approach. If you are using multiple lookups on the same table consider using Cache transform. Refer the below link for details on Cache transform
Lookup and Cache Transforms in SQL Server Integration Services
Alternatively if you want to go ahead with single look up , you may have to modify the SQL statement in the Lookup accordingly to return the proper value. In you case it may be
select a,b from table where a=1 or c=3
Note : Consider the above as a pseudo code. This needs to be tested and applied based on your requirement.
Best Regards Sorna -
How to write sql query with many parameter in ireport
hai,
i'm a new user in ireport.how to write sql query with many parameters in ireport's report query?i already know to create a parameter like(select * from payment where entity=$P{entity}.
but i don't know to create query if more than 1 parameter.i also have parameter such as
$P{entity},$P{id},$P{ic}.please help me for this.
thanksYou are in the wrong place. The ireport support forum may be found here
http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&func=showcat&catid=9 -
Hierarchical query with many-to-many relationship
I have read with interest the creative solutions to complex hierarchical queries posted previously; they have been instructive but have not quite addressed this scenario.
We have a hierarchy table H, with columns for ID, name, parentID, and other attributes.
Within this table are a number of independent hierarchies, each existing for a different purpose.
We have a master list of hierarchies in table T which describes the purpose of each hierarchy, provides some default attributes which the nodes can inherit, and stores a unique id for each hierarchy and a pointer to the root node of the corresponding hierarchy in table H.
We have a master list of items M, with identically named columns to those in H, along with many other attributes.
The members of table M ALL belong to EACH of the Hierarchies. So we have a link table I to define the intersection of H and M.
So the leaf nodes of H are really containers for the list of elements from M which may be attached to them.
The universe of M is very volatile, with new members being added, old ones deleted, and existing ones being reclassified frequently from node to node within each hierarchy. Since the hierarchies have to be built to handle every possible scenario, so that the members of M can always find a suitable node to reside in, quite often, in fact more often than not, the majority of leaf nodes for each hierarchy are empty at any given moment.
Therefore, although we always know the root sector of a given hierarchy and can traverse downwards from there, if we worked our way up from the intersection table, we could eliminate up to 70% of the nodes of any given hierarchy from further consideration, as they don't need to be (in fact, must not be) included in reports.
As implied by the above, rows in M are structurally similar (in terms of columns, but not in any real world sense) and are a superset of rows in H. But combining them into the one table doesn't seem to help the reporting process due to the many-to-many relationship which prevents the ID/parentID relationship from being carried through to this level.
There are a number of other considerations of which the most pertinent is that the people using this database generally have an interest in only a subset of the master list of items in M. This relationship is also dynamic but important enough and rigid enough that another link table P exists to combine the Users in table U with the subset of M in which they are interested. (The users are also grouped into hierarchies of a totally different nature, but this aspect is secondary for now.)
The reporting is reasonably straightforward for any single combination of User and Hierarchy; they want to see all the items they are interested in, listed in hierarchical sequence, totalled on change of level with the individual items M listed beneath the nodes of H. This is unfortunately required in real time, so retrieval performance is paramount.
Some statistics might help to determine the optimum approach:
The largest hierarchy has 10,000 nodes. The smallest about 100.
The largest would have 70% or more of its nodes unused at any point in time, and even the smallest would have 25% unused.
The hierarchies tend to be broad rather than deep, the maximum number of levels being about 5; but the larger ones should be twice as deep as this if performance was not compromised.
There are dozens of hierarchies, but it may be possible to sharply reduce this number by exploiting the Order Siblings By clause.
The number of rows in M varies between 500,000 and 50,000; depending upon how long historical data is retained on-line (and performance permitting, it would be retained indefinitely).
The number of users varies between 1000 and 2000 but the range of M in which they are interested varies greatly; from as few as 100 to as many as 10,000+. So it is almost always worth beginning by eliminating the items in which they are not interested, implying once again that the hierarchy should be traversed upwards rather than down from the root.
The current system is very old and survives by a tactic of building what are essentially materialised views of the database structure for each user overnight using, ahem, non-relational technology. This is inefficient and not easily scaled (but it works) and hence this redevelopment project needs to (a) work, and (b) work better and faster.
I am happy to provide some DDL scripts if that helps explain the problem better than this narrative.
I can't help feeling that the solution lies in somehow extending the hierarchical query past the many-to-many link table so that the Master list can be merged directly into the hierarchy such that the M items become the leaf nodes rather than the design outlined above - but I don't know how to do that. But I am sure everyone reading this does! :)
All advice appreciated. Database version is not an issue; we are currently using version 10XE for experimentation, but production usage could be on 11 if that contains helpful features.
Thank you
CSHi,
ChrisS. wrote:
I am happy to provide some DDL scripts if that helps explain the problem better than this narrative.Yes, please do.
The problem seems interesting, I'm sure many people here (including myself) are willing to help you in this matter.
So yes, post DDL for the tables, as well as INSERTs to populate them with representative data. Please also include the output you require along with detailed explanations about the logic to get it.
Don't forget to put lines of code between {code} tags in order to preserve formatting and readability, like this :
SELECT sysdate FROM dual;Thanks. -
CS3 Camera Raw Too Many Files Open at the Same time alert!!!
Please help me. I keep getting this error message in Camera Raw that says there are too many files open at the same time - but I only have 100 open:( Please help - I was getting this error with CS2 and thought upgrading to CS3 would fix it and it didn't!!!
> "10 or 100 - you can quickly go through a stack of images in ACR and make any desired changes you want. Whether making the same or similar adjustment to similar files, or making radically different adjustments to different images as appropriate".
I've done this with far more than 100! I think my maximum is 425 raw files, invoking ACR from Bridge without Photoshop even loaded, and it worked well. (I've also done 115 JPEGs in order to crop them under extreme time constraints).
It can be very slick. For example, if I use a ColorChecker a number of times in a shoot, it is easy to select just the set (perhaps 100 or so) that a particular ColorChecker shot applies to and set the WB for all of them.
Furthermore, in case people don't know, you can set ratings on raw images while many of them are open in ACR. (Just click under the thumbnail). It isn't as powerful as Lightroom, but it is not to be dismissed.
I suspect that it is possible to apply sensor-dust-healing to lots of images in the same way, and certainly it is easy to apply presets based on various selections.
Perhaps with AMG (Adobe Media Gallery) it will be sensible to use the above capability to process 100s of raw files, then create a set of web pages for the best of them, in not much more time than it would have taken in Lightroom. I judge that Lightroom is the "proper" tool for the job (perhaps after 1.1!), but Bridge+ACR can go a long way.
Maybe you are looking for
-
How do i create a subset of data in numbers 3.5
Hi I use Numbers 3.5.2 on my MacBook Pro. I am familiar with Excel but finding it difficult to transfer my Pivot Table knowledge into Numbers. I just about understand how SUMIF works now but have a further question related to summarising data from on
-
How do you create a popup in sharepoint within a content editor?
I need to create a popup in sharepoint 2007 by clicking on a link within a content editor. thanks
-
Need Help in JNDI Datasource using tomcat and spring
Hi, I am trying to connect my local database using JNDI datasource in tomcat and spring I have done the below configuration in Tomcat 7 server: In tomcat server.xml (Path: E:\apache-tomcat-7.0.35\conf) I added the below configuration <Resource name="
-
BAPI for XD02 (Customer Update)
Hi Everyone, I am unable to find any BAPI which updates selective fields from transaction XD02 in background. Does any one have any info on the same. Thanks, Altaf Shaikh.
-
Multiple simultaneous jsx call in Photoshop
Greetings, We are trying to develop a small online Imaging service (like scene 7) using Photoshop CS4. We have some jsx files, some PSD files and the backend is PHP. The php executes photoshop.exe from command-line (i.e. runs photoshop as network ser