Unix Technical Forum

Improve performance in Query..

This is a discussion on Improve performance in Query.. within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a table called work_order which has over 1 million records and a contractor table which has over ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:48 PM
Jaidev Paruchuri
 
Posts: n/a
Default 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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:48 PM
David Portas
 
Posts: n/a
Default Re: Improve performance in Query..

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
--


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:48 PM
Jaidev Paruchuri
 
Posts: n/a
Default Re: Improve performance in Query..

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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:21 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com