문제

I am developing a TSQL query using SSMS 2008 R2 and I want to optimize this sproc that currently uses many left joins. There is a lookup table that contains one row / lookup value. So my TSQL code looks like this:

Table A = main record table
Table B = Lookup Table
  row1 x unique_identifier, y varchar(100)
  row2 x unique_identifier, y varchar(100)
  row3 x unique_identifier, y varchar(100)
  row4 x unique_identifier, y varchar(100)

so each row in this lookup table has two values: one unique_identifier and the other value is a varchar. And so currently my sproc code looks like:

select *
FROM A
LEFT JOIN B ON B.X = A.X WHERE B.X = 123456
LEFT JOIN B2 ON B2.X = A.X WHERE B2.X = 123457
LEFT JOIN B3 ON B3.X = A.X WHERE B3.X = 123458
LEFT JOIN B4 ON B4.X = A.X WHERE B4.X = 123459
LEFT JOIN B5 ON B5.X = A.X WHERE B5.X = 123451

I'm sure there must be a more efficient way to do this though. And my real sproc code actually joins this lookup table 30 times instead of 5 times. I have also tried using a temp table but that didn't seem to optimize it either. Any recommendations for how to replace all of the LEFT JOINs?

OK, so I am updating this question now. This is my exact query:

SELECT DISTINCT 
    lut.[description] as UDT_Injuries,  
    lut2.[description] as abduction, 
    lut3.[description] as ARREST_PARENT
FROM evolv_cs.dbo.incident_header_x x with (nolock)
LEFT JOIN user_defined_lut_rv lut on lut.user_defined_lut_id = x.UDT_Injuries
LEFT JOIN user_defined_lut_rv lut2 on lut2.user_defined_lut_id = x.ABDUCTION
LEFT JOIN user_defined_lut_rv lut3 on lut3.user_defined_lut_id = x.ARREST_PARENT
WHERE lut.[description] IS NOT NULL OR lut2.[description] IS NOT NULL OR lut3.[description] IS NOT NULL 

and the first 10 rows of output for this query:

UDT_Injuries    abduction   ARREST_PARENT
NULL    NULL    Outside of facility
Client  NULL    NULL
Client  NULL    Outside of facility
None    NULL    NULL
None    NULL    Outside of facility
Other adult NULL    NULL
Other adult NULL    Outside of facility
Parent  NULL    NULL
Peer    NULL    NULL
Sibling NULL    NULL

FYI, I looked at the article on PIVOT and UNPIVOT, but I do not want to aggregate any data. Instead I want to translate key id values from incident_header_x into varchar values from user_defined_lut_rv.

Sample data from incident_header_x looks like:

UDT_Injuries    ABDUCTION   ARREST_PARENT
5B84773B-99BF-4EB9-8545-EFC06A35FE29    NULL    NULL
NULL    4F18EDE9-BBBA-4430-9EF4-4E28EAC1E6D4    NULL
5B84773B-99BF-4EB9-8545-EFC06A35FE29    NULL    NULL
NULL    NULL    4F18EDE9-BBBA-4430-9EF4-4E28EAC1E6D4
NULL    NULL    5B84773B-99BF-4EB9-8545-EFC06A35FE29

Does this make sense now what I am trying to achieve? To translate these id values into varchar values from the user_defined_lut_rv lookup table?

도움이 되었습니까?

해결책

In short, no - there is no more efficient or different way to do this. Are you having performance issues, or are you finding the the code unwieldy?

Anyway you may wish to do some research on 'the one true lookup table', which this looks similar (not identical) to, to get some ideas of disadvantages and advantages in this design.

다른 팁

As the join condition is always the same, it seems you are looking for a pivot: http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

If this is not what you are looking for, please provide more details. Example data and expected results would be a great start.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top