tsql - How to improve performance for update statement? -
suppose have lookup table mylookup(lkid, lkname, ...)
then have other 2 tables:
mytab(id, parentid, name, lookname, ...) yourtab(id, parentid, ...) --id, parentid coming mytab
then have update try lkid , it's parent lkid, sql like:
update yourtab set columnx = case when (select lkid mylookup join mytab b on a.lkname = b.lkname b.id = c.parentid ) > 3 , (select lkid mylookup join mytab b on a.lkname = b.lkname b.id = c.parentid ) > (select lkid mylookup lkname = c.lkname ) 1 else 0 end yourtab c
this sql performance not good. (select lkid mylookup join mytab b on a.lkname = b.lkname b.id = c.parentid ) called 2 times each row in yourtb.
how rewrite sql improve performance case?
maybe this..
it's untested need sqlfiddle sample data understand inner workings.
update yourtab set columnx = case when not null ( select lkid mylookup inner join mytab b on a.lkname = b.lkname b.id = c.parentid , a.lkid > 3 , a.lkname > c.lkname) 1 else 0 end yourtab c
Comments
Post a Comment