473,398 Members | 2,120 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,398 software developers and data experts.

Correlated Subquery Efficiency

Hello All,

I have a SQL Query with multiple correlated Subqueries in it. When it
gets executed it runs rather slow due to the size of the QT table.
Does anybody have any suggestions how to alter this query to make it
run faster, or any index suggestions to assist it with.

Query is as follows:
SELECT SH_ORDER, SH_CUST, SH_ADD_DATE, SH_CUST_REF, SH_DESC, SH_EXCL,

(SELECT SUM(QT_CHARGE) AS QT_CHARGE_SUM
FROM QT INNER JOIN
JU ON QT_PROC_CODE = JU_PROC_CODE
WHERE (QT_NUMBER = ' ' + SH_NOTE_2) AND (JU_PROC_GRP < 2)
AND (QT_QUOTE_JOB = 0))AS [PREPCOST],

(SELECT SUM(QT_CHARGE) AS QT_CHARGE_SUM
FROM QT INNER JOIN
JU ON QT_PROC_CODE = JU_PROC_CODE
WHERE (QT_NUMBER = ' ' + SH_NOTE_2) AND (QT_QUOTE_JOB = 0)
AND (JU_PROC_GRP > 1) AND (JU_CATEG = 1)) AS [MATCOST],

(SELECT SUM(QT_CHARGE) AS QT_CHARGE_SUM
FROM QT INNER JOIN
JU ON QT_PROC_CODE = JU_PROC_CODE
WHERE (QT_NUMBER = ' ' + SH_NOTE_2) AND (QT_QUOTE_JOB = 0)
AND (JU_PROC_GRP > 1) AND (JU_CATEG = 3)) AS [OUTCOST],

(SELECT SUM(QT_CHARGE) AS QT_CHARGE_SUM
FROM QT INNER JOIN
JU ON QT_PROC_CODE = JU_PROC_CODE
WHERE (QT_NUMBER = ' ' + SH_NOTE_2) AND (QT_QUOTE_JOB = 0)
AND (JU_PROC_GRP > 1) AND
((JU_CATEG = 0) OR (JU_CATEG = 2) OR (JU_CATEG = 4))) AS [LABCOST]

FROM SH
WHERE SH_ADD_DATE = '5/FEB/2004'

thanks a lot for any help
Jason
Jul 20 '05 #1
1 5379
[posted and mailed, please reply in news]

Jason (ha******@primus.com.au) writes:
I have a SQL Query with multiple correlated Subqueries in it. When it
gets executed it runs rather slow due to the size of the QT table.
Does anybody have any suggestions how to alter this query to make it
run faster, or any index suggestions to assist it with.


Yes, it looks like derived table could be a winner here. Try the
transformation below. It may not be correct every detail, so test
carefully:

SELECT SH_ORDER, SH_CUST, SH_ADD_DATE, SH_CUST_REF, SH_DESC, SH_EXCL,
PREPCOST, MATCOST, OUTCOST, LABCOST
FROM SH
JOIN (SELECT QT_NUMBER,
SUM(CASE WHEN JU_PROC_GRP < 2
THEN QT_CHARGE
ELSE 0
END) AS PREPCOST,
SUM(CASE WHEN JU_PROC_GRP > 1 AND JU_CATEG = 1
THEN QT_CHARGE
ELSE 0
END) AS MATCOST,
SUM(CASE WHEN JU_PROC_GRP > 1 AND JU_CATEG = 3
THEN QT_CHARGE
ELSE 0
END) AS OUTCOST,
SUM(CASE WHEN JU_PROC_GRP > 1 AND JU_CATEG IN (0, 2, 4)
THEN QT_CHARGE
ELSE 0
END) AS LABPCOST
FROM QT INNER JOIN
JU ON QT_PROC_CODE = JU_PROC_CODE
WHERE QU_QUOTE_JOB = 0
GROUP BY QT_NUMBER) AS x
ON x.QT_NUMBER = ' ' + SH_NOTE_2
WHERE SH_ADD_DATE = '5/FEB/2004'


--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2

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

Similar topics

0
by: Murali | last post by:
Hi All I was reading thro the posting(s) of Thomas Kyte and his nifty approach to doing updates without the need for unnecessary correlated subqueries. An alternative to correlated subquery...
8
by: Venkata C | last post by:
Hi! Does anyone here know of a way to goad DB2 into converting a correlated subquery to a non-correlated one? Does DB2 ever do such a conversion? We have a query of the form SELECT .. FROM A...
1
by: Mike MacSween | last post by:
tblProductions one to many to tblEvents tblEvents contains StartDate I want a report where the data are grouped by tblProductions.ProdID, and sorted by the earliest date in each Production. ...
14
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some...
8
by: Howard, Steven | last post by:
I have created a web app that stores and displays all the messages from my database maintenance jobs that run each night. The web app uses Java servlets and has PostgreSQL 7.0 as the back end. ...
4
by: sql_server_user | last post by:
Hi, I have a history table with about 400 million rows, with a unique composite nonclustered index on two columns (object id and time period) which is used for most of the queries into the...
5
by: steven.fafel | last post by:
I am running 2 versions of a correlated subquery. The two version differ slightly in design but differ tremendously in performance....if anyone can answer this, you would be awesome. The "bad"...
4
by: muzu1232004 | last post by:
Can anyone explain me when we use correlated subqueries rather than nested subqueries. Do all the correlated subqueries can be written in nested subqueries form as well ? What are the major...
4
by: lopez | last post by:
hi dere, i'm new to oracle.. can anybody help me with correlated subquery thanks in advance. regards, lopez (knowledge is power)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.