473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to change this query, so that it will not use the correlation?

I have the following query, which is takling couple of minutes to
return 500+ rows!

I have all the proper indexes and statistics is upto date.

I correlation after the OR clause is the culprit. I have tried a few
ways to get the sum and then join with the table pr. But am not
getting the exact result.

Can someone show me the way to write this query without using the
correlation? My brain is blanked out!
select recordno, (select sum(amount)
from pmt
where cnyno = 1234 and
pmtdt <= sysdate and
state = 'X' and
(pmt.key = pr.recordno
or
pmt.key in
(select recordno from pr cr
where cr.cnyno = pr.cnyno and
cr.parentrec = pr.recordno
)
)
) tot
from pr
where pr.cnyno = 1234 and
pr.created <= sysdate

thanks!
Jul 19 '05 #1
6 2751
Questions:
1- Version?
2- Execution plan?
3- What do you mean by "correlation"?

Daniel
I have the following query, which is takling couple of minutes to
return 500+ rows!

I have all the proper indexes and statistics is upto date.

I correlation after the OR clause is the culprit. I have tried a few
ways to get the sum and then join with the table pr. But am not
getting the exact result.

Can someone show me the way to write this query without using the
correlation? My brain is blanked out!
select recordno, (select sum(amount)
from pmt
where cnyno = 1234 and
pmtdt <= sysdate and
state = 'X' and
(pmt.key = pr.recordno
or
pmt.key in
(select recordno from pr cr
where cr.cnyno = pr.cnyno and
cr.parentrec = pr.recordno
)
)
) tot
from pr
where pr.cnyno = 1234 and
pr.created <= sysdate

thanks!

Jul 19 '05 #2
To answer your question:

1- Version? 9.2.0.3
2- Execution plan? I will have it attached later. At this time
I am looking for how to modify "OR" part of the query without
changing the semantics.
3- What do you mean by "correlation"?
The table in subquery referring back to the
table outside the subquery.
thanks,

da*************@hotmail.com (Daniel Roy) wrote in message news:<37************************@posting.google.co m>...
Questions:
1- Version?
2- Execution plan?
3- What do you mean by "correlation"?

Daniel
I have the following query, which is takling couple of minutes to
return 500+ rows!

I have all the proper indexes and statistics is upto date.

I correlation after the OR clause is the culprit. I have tried a few
ways to get the sum and then join with the table pr. But am not
getting the exact result.

Can someone show me the way to write this query without using the
correlation? My brain is blanked out!
select recordno, (select sum(amount)
from pmt
where cnyno = 1234 and
pmtdt <= sysdate and
state = 'X' and
(pmt.key = pr.recordno
or
pmt.key in
(select recordno from pr cr
where cr.cnyno = pr.cnyno and
cr.parentrec = pr.recordno
)
)
) tot
from pr
where pr.cnyno = 1234 and
pr.created <= sysdate

thanks!

Jul 19 '05 #3
Try replacing the "in" by an "exists", as it can perform much better,
especially if the list of possible values is large. I can't think of
any way to remove the "or", but I've never seen an "or" slow down a
select statement considerably. Note also that if the query returns 500
rows, it doesn't mean necessarily that it should run fast. What
determines the speed is the number of rows it has to go through, and
not the number of rows returned. It would be much easier for us to
help if we had the execution plan. Also make sure that your stats are
up-to-date.

Daniel
Jul 19 '05 #4
Daniel!

Thanks for the response.

I know the number of rows returned is irrelevent. I shouldn't have
have that information there.

EXISTS, wouldn't work either in this case. I believe, I somehow have
to get rid of the correlation, to have the best response time. [All
the stats and indexes are in place]. The table MRECORD has many rows
(a few million) and when the "WHERE" condition (line# 30 to 32) is
applied, it gets only a few hundred rows.

Anyway, I have the query attached with explain plan.

1 select record#,entity, (select nvl(sum(amount),0)
2 from entryitems
3 where comp# = 1234 and
4 paymentdate <= sysdate and
5 state = 'X' and
6 (entryitems.payKey = mr.record#
7 or
8 entryitems.payKey in
9 (select record# from mrecord
childrec
10 where childrec.comp# = 1234
and
11 childrec.parentent =
mr.record#
12 )
13 )
14 ) neg,
15 (select nvl(sum(amount),0)
16 from entryitems
17 where comp# = 1234 and
18 paymentdate <= sysdate and
19 state = 'X' and
20 (entryitems.recKey = mr.record#
21 or
22 entryitems.recKey in
23 (select record# from mrecord
childrec
24 where childrec.comp# = 1234
and
25 childrec.parentent =
mr.record#
26 )
27 )
28 ) pos
29 from mrecord mr
30 where mr.comp# = 1234 and
31 mr.whencreated <= sysdate and
32* mr.type in ('A','B')
/

OPS$GS> l
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=38 Card=89
Bytes=2314)
1 0 SORT (AGGREGATE)
2 1 FILTER
3 2 TABLE ACCESS (BY INDEX ROWID) OF 'ENTRYITEMS' (Cost=63
Card=72 Bytes=1440)
4 3 INDEX (RANGE SCAN) OF 'IX_ENTRYITEMS_PARENTENT'
(NON-UNIQUE) (Cost=12 Card=1454)
5 2 TABLE ACCESS (BY INDEX ROWID) OF 'MRECORD' (Cost=3
Card=1 Bytes=10)
6 5 INDEX (UNIQUE SCAN) OF 'PK_MRECORD' (UNIQUE) (Cost=2
Card=2884447)
7 0 SORT (AGGREGATE)
8 7 FILTER
9 8 TABLE ACCESS (BY INDEX ROWID) OF 'ENTRYITEMS' (Cost=63
Card=72 Bytes=1440)
10 9 INDEX (RANGE SCAN) OF 'IX_ENTRYITEMS_PARENTENT'
(NON-UNIQUE) (Cost=12 Card=1454)
11 8 TABLE ACCESS (BY INDEX ROWID) OF 'MRECORD' (Cost=3
Card=1 Bytes=10)
12 11 INDEX (UNIQUE SCAN) OF 'PK_MRECORD' (UNIQUE) (Cost=2
Card=2884447)
13 0 INLIST ITERATOR
14 13 TABLE ACCESS (BY INDEX ROWID) OF 'MRECORD' (Cost=38
Card=89 Bytes=2314)
15 14 INDEX (RANGE SCAN) OF 'IX_MRECORD_CLRDATE'
(NON-UNIQUE) (Cost=5 Card=106)

Statistics
----------------------------------------------------------
0 recursive calls
4 db block gets
28024824 consistent gets
48 physical reads
0 redo size
19852 bytes sent via SQL*Net to client
961 bytes received via SQL*Net from client
44 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
630 rows processed

da*************@hotmail.com (Daniel Roy) wrote in message news:<37***********************@posting.google.com >...
Try replacing the "in" by an "exists", as it can perform much better,
especially if the list of possible values is large. I can't think of
any way to remove the "or", but I've never seen an "or" slow down a
select statement considerably. Note also that if the query returns 500
rows, it doesn't mean necessarily that it should run fast. What
determines the speed is the number of rows it has to go through, and
not the number of rows returned. It would be much easier for us to
help if we had the execution plan. Also make sure that your stats are
up-to-date.

Daniel

Jul 19 '05 #5
I think I finally figure it out. Let me do some more testing and see
whether I got it correct and the response is acceptable.

Thanks,

da*************@hotmail.com (Daniel Roy) wrote in message news:<37***********************@posting.google.com >...
Try replacing the "in" by an "exists", as it can perform much better,
especially if the list of possible values is large. I can't think of
any way to remove the "or", but I've never seen an "or" slow down a
select statement considerably. Note also that if the query returns 500
rows, it doesn't mean necessarily that it should run fast. What
determines the speed is the number of rows it has to go through, and
not the number of rows returned. It would be much easier for us to
help if we had the execution plan. Also make sure that your stats are
up-to-date.

Daniel

Jul 19 '05 #6
gs**@yahoo.com (Prince Kumar) wrote in message news:<62**************************@posting.google. com>...
I think I finally figure it out. Let me do some more testing and see
whether I got it correct and the response is acceptable.

Thanks,


can you then please post your final solution? Some of us are curious.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Eugenio | last post by:
Excuse me in advance fo my little English. I've got this stored procedure **************************************************************************** ********** declare @Azienda as...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
14
by: signaturefactory | last post by:
I am trying the following query in and oleDbCommand: SELECT PartLocations.LocationName, Sum(PartsJournal.Quantity) AS SumOfQuantity, PartsJournal.PartsLotNumber FROM PartLocations INNER JOIN...
1
by: Ben | last post by:
I have written a procedure which calls the CORREL function of Excel to run correlation analysis on two arrays, then populate a table with the resulting correlation coefficient. This process loops...
7
by: Frankie | last post by:
I'm trying to run the following search query: $query = sprintf ("SELECT itemNumber, thumbnailURL, title, description, price FROM apparel,hats WHERE apparel.title OR apparel.description OR...
1
by: Gary Wessle | last post by:
Hi I wrote a code to calculate the correlation between 2 sequence of double numbers presented as vectors. did I go about this the right way? thanks #include <vector> using std::vector;
6
by: MVM | last post by:
Hi, I am attempting to run a query in MS SQL server between two tables that have a one to many relationship. The tables are linked on GID. What I want is one instance of every record from Table...
18
by: robert | last post by:
Is there a ready made function in numpy/scipy to compute the correlation y=mx+o of an X and Y fast: m, m-err, o, o-err, r-coef,r-coef-err ? Or a formula to to compute the 3 error ranges? ...
6
by: Prince Kumar | last post by:
I have the following query, which is takling couple of minutes to return 500+ rows! I have all the proper indexes and statistics is upto date. I correlation after the OR clause is the culprit....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.