| Author |
Topic |
|
ann06
Posting Yak Master
171 Posts |
Posted - 2008-04-13 : 08:27:54
|
| i have three tables company as co, Procurement as po,contracts as crCo_id as primary for coco_id forign for poco_id forign for cri want the query to get me only one record for every co_id if it has a company in certain PO or CR ( i used max fnctn to get me only one record)result:CO_ID,PODOCNO,CRDOCNO----- ------- -------1 350 4002 355 10643 NULL 5004 600 NULLI used the left outer join in this mannerSELECT CO.CO_ID, MAX(PR.DOCNO) AS pO_DOCNO ,MAX(CR.DOCNO) AS CR_DOCNOFROM COMPANY CO lEFT OUTER JOIN PROCUREMENT PR ON OG.CO_ID=PR.CO_ID lEFT OUTER JOIN CONTRACT CR ON CO.CO_ID=CR.CO_ID GROUP BY OG.CO_IDthe result is ok but the problem its taking infinte time if i add more tables to the outer join, i have more tables and each with huge number of recordsany better way to do this ?? its true performance is a big deaaaal |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-13 : 09:37:25
|
| What indexes do you have and how big are the tables.Have you looked at the query plan?==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-13 : 11:33:50
|
| You could probably look to see if you can spot any table scans in your query execution plan. |
 |
|
|
ann06
Posting Yak Master
171 Posts |
Posted - 2008-04-14 : 05:43:41
|
| no the problem is for every record in company there is like 100 records in po and 100 records in contracts so when doing outer join its returning 100 x 100 record and if there is another table it will multiply also with the records of the third tabledex:co_id po_id co_id CR_id1 10 1 201 11 1 231 15 1 251 17when we do the outer join result will be 4 x 3 = 12 recordsco_id po_id cr_id1 10 201 11 201 15 201 17 201 10 231 11 231 15 231 17 231 10 251 11 251 15 251 17 25any better way to do this join ? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 05:47:50
|
quote: Originally posted by ann06 SELECT CO.CO_ID, MAX(PR.DOCNO) AS pO_DOCNO ,MAX(CR.DOCNO) AS CR_DOCNOFROM COMPANY CO lEFT OUTER JOIN PROCUREMENT PR ON OG.CO_ID=PR.CO_ID lEFT OUTER JOIN CONTRACT CR ON CO.CO_ID=CR.CO_ID GROUP BY OG.CO_ID
That this code, there is absolutely no guarantee that the two values derives from same record... E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 05:50:47
|
If above is of no matter, try thisSELECT CO.CO_ID, PR.DOCNO, CR.DOCNOFROM COMPANY AS COLEFT JOIN ( SELECT CO_ID, MAX(DOCNO) AS DOCNO FROM PROCUREMENT GROUP BY CO_ID ) AS PR ON PR.CO_ID = CO.CO_IDLEFT JOIN ( SELECT CO_ID, MAX(DOCNO) AS DOCNO FROM CONTRACT GROUP BY CO_ID ) AS CR ON CR.CO_ID = CO.CO_ID E 12°55'05.25"N 56°04'39.16" |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-14 : 06:28:53
|
| >> no the problem is for every record in company there is like 100 records in po and 100 records in contracts so when doing outer join its returning 100 x 100 record and if there is another table it will multiply also with the records of the third tabledThat's a problem with your code not with the left outer join.What do you want to return?==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
ann06
Posting Yak Master
171 Posts |
Posted - 2008-04-14 : 07:20:52
|
| Thanks Very Much so the problem was from the select i should minimize the select when using outer join? |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-14 : 07:28:58
|
If you refer to the post made 04/14/2008 : 05:50:47, the answer is yes. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|