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

Improve performance in Query..

I have a table called work_order which has over 1 million records and a
contractor table which has over 3000 records.

When i run this query ,it takes long time since its grouping by
contractor and doing multiple sub SELECTs.

is there any way to improve performance of this query ??

-------------------------------------------------
SELECT ckey,cnam,t1.contractor_id,count(*) as tcnt,
(SELECT count(*) FROM work_order t2 WHERE
t1.contractor_id=t2.contractor_id and rrstm=1 and rcdt is NULL) as r1,
(SELECT count(*) FROM work_order t3 WHERE
t1.contractor_id=t3.contractor_id and rrstm=2 and rcdt is NULL) as r2,
(SELECT count(*) FROM work_order t4 WHERE
t1.contractor_id=t4.contractor_id and rrstm=3 and rcdt is NULL) as r3,
SELECT count(*) FROM work_order t5 WHERE
t1.contractor_id=t5.contractor_id and rrstm=4 and rcdt is NULL) as r4,
(SELECT count(*) FROM work_order t6 WHERE
t1.contractor_id=t6.contractor_id and rrstm=5 and rcdt is NULL) as r5,
(SELECT count(*) FROM work_order t7 WHERE
t1.contractor_id=t7.contractor_id and rrstm=6 and rcdt is NULL) as r6,
SELECT count(*) FROM work_order t8 WHERE
t1.contractor_id=t8.contractor_id and rcdt is NULL) as open_count,
(SELECT count(*) FROM work_order t9 WHERE
t1.contractor_id=t9.contractor_id and vendor_rec is not NULL) as
Ack_count,
(SELECT count(*) FROM work_order t10 WHERE
t1.contractor_id=t10.contractor_id and (rtyp is NULL or rtyp<>'R') and
rcdt is NULL) as open_norwo
FROM work_order t1,contractor WHERE
t1.contractor_id=contractor.contractor_id and
contractor.tms_user_id is not NULL GROUP BY
ckey,cnam,t1.contractor_id ORDER BY cnam
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
2 4892
Try this:

SELECT ckey, cnam, W.contractor_id, COUNT(*) AS tcnt,
COUNT(CASE WHEN rrstm=1 AND rcdt IS NULL THEN 1 END) AS r1,
COUNT(CASE WHEN rrstm=2 AND rcdt IS NULL THEN 1 END) AS r2,
COUNT(CASE WHEN rrstm=3 AND rcdt IS NULL THEN 1 END) AS r3,
COUNT(CASE WHEN rrstm=4 AND rcdt IS NULL THEN 1 END) AS r4,
COUNT(CASE WHEN rrstm=5 AND rcdt IS NULL THEN 1 END) AS r5,
COUNT(CASE WHEN rrstm=6 AND rcdt IS NULL THEN 1 END) AS r6,
COUNT(CASE WHEN rcdt IS NULL THEN 1 END) AS open_count,
COUNT(CASE WHEN vendor_rec IS NOT NULL THEN 1 END) AS ack_count,
COUNT(CASE WHEN COALESCE(rtyp,'')<>'R' AND rcdt IS NULL THEN 1 END)
AS open_norwo
FROM work_order AS W
JOIN contractor AS C
ON W.contractor_id = C.contractor_id
AND C.tms_user_id IS NOT NULL
GROUP BY ckey, cnam, W.contractor_id
ORDER BY cnam

Please always include DDL with your posts so that we don't have to guess at
your columns, keys and constraints.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
David,

The query u replied improved the preformance from
10 mins to 6 seconds.

Thanks for the Immediate and MOST efficient query.

--Jay

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

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

Similar topics

2
by: ALex_1998 | last post by:
Hi Dear All, I have a large query as below: select count (distinct b.bus_acct_id) from M_DATE M1, M_BIZ_ACCT M2, BIZ_ACCT B, C_PRDT_PKG C
3
by: Leader | last post by:
Hi All, I am getting slower performance of select statements in MS SQL. I am finding select statements in MS SQL are even slower than MS ACCESS. Is there any way to improve the performance of...
17
by: Felix | last post by:
Dear Sql Server experts: First off, I am no sql server expert :) A few months ago I put a database into a production environment. Recently, It was brought to my attention that a particular...
7
by: Bing Wu | last post by:
Hi Folks, I have a very large table containing 170 million rows of coordinats: CREATE TABLE "DB2ADMIN"."COORDINATE" ( "FID" INTEGER NOT NULL , "AID" INTEGER NOT NULL , "X" REAL NOT NULL ,...
8
by: Michael C | last post by:
Anyone have any hints on improving the performance of C# UI? I'm filling a TreeView and ListView with information returned by a SQLDataReader and information read from the Registry. I'm working...
2
by: lelandhuang | last post by:
I am developing reporting service and using lots of 'LEFT OUTER JOIN', I am worried about the performance and want to use some subquery to improve the performance. Could I do that like below, ...
0
by: Swami | last post by:
I have 2 questions relating to website design in asp .net: 1. In a website that I am building I have everything as a user control. Even the header, which contains the navigation tabs is in a user...
5
by: Sangs | last post by:
I have a table called DMPD_Product_Lookup_Dom. It is a lookup table which contains values for certain fields of other tables in the database. This takes long time to run. Is there any way to...
3
by: oravm | last post by:
Hi, I re-write a query and used bulk collect to improve the performance of the batch process. The query below has NO compile error but when execute query there is error 'ORA-01403: no data...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.