sql - Multiple joins to get the same lookup column for different values -


we have rather large sql query, rather poorly performing. 1 of problems (from analysing query plan) number of joins have.

essentially have values in our data need on table.to value display user. problem have join on same table 4 times because there 4 different columns need same up.

hopefully diagram might make clearer

raw_event_data event_id,   datetime_id,         lookup_1, lookup_2, lookup_3, lookup_4 1,          2013-01-01_12:00,    1,        5,        3,         9           2,          2013-01-01_12:00,    121,      5,        8,         19 3,          2013-01-01_12:00,    11,       2,        3,         32 4,          2013-01-01_12:00,    15,       2,        1,         0  lookup_table lookup_id,      lookup_desc 1,              desc1 2,              desc2 3,              desc3 ... 

our query looks this

select      raw.event_id,      raw.datetime_id,     lookup1.lookup_desc,     lookup2.lookup_desc,     lookup3.lookup_desc,     lookup4.lookup_desc,      raw_event_data raw, lookup_table lookup1,lookup_table lookup2,lookup_table lookup3,lookup_table lookup4 raw.event_id = 1 , raw.lookup_1 *= lookup1 , raw.lookup_2 *= lookup2.lookup_id , raw.lookup_3 *= lookup3.lookup_id , raw.lookup_4 *= lookup4.lookup_id 

so output

 1, 2013-01-01_12:00, desc1, desc5, desc3, desc9 

as said query works, joins killing performance. simplistic example give there, in reality there 12 joins above , won't selecting specific event, rather range of events.

the question is, there better way of doing joins.

correlated subqueries might way go:

   select r.event_id         , r.datetime_id         , (select lookup1.lookup_desc lookup_table lookup1 lookup1.lookup_id = r.lookup_1) desc_1         , (select lookup2.lookup_desc lookup_table lookup2 lookup2.lookup_id = r.lookup_2) desc_2         , (select lookup3.lookup_desc lookup_table lookup3 lookup3.lookup_id = r.lookup_3) desc_3         , (select lookup4.lookup_desc lookup_table lookup4 lookup4.lookup_id = r.lookup_4) desc_4      raw_event_data r     r.event_id = 1         ; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -