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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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! |
| |||
| 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 -- |
| ||||
| 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! |
| Thread Tools | |
| Display Modes | |
|
|