Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-11-19 : 04:45:18
|
| Query below causes a lock that lasts for over ten hours, how can I rewrite the vhe view and update to one query producing the same result?create view [dbo].[view_DeclareBuilding]asSELECT r2.LANKOD,r2.KOMKOD,r2.RNPREGBY as IdPrefixById,r2.RIDREGBY as IdById,count(r2.RNPREGBY) as declaresFROM BA_REGENH_01 r1 JOIN BA_REGBYG_50A r2 ON r1.FNR = r2.FNRWHERE AND NOT EXISTS (SELECT 1 FROM BA_FastighetUndantag fu WHERE fu.Fnr = r1.FNR)AND NOT EXISTS (SELECT 1 FROM BA_ByggnadUndantag bu WHERE bu.IdPrefixById = r2.RNPREGBY and bu.IdById = r2.RIDREGBY)group by r2.LANKOD, r2.KOMKOD, r2.RNPREGBY, r2.RIDREGBYUPDATE DESET nr_buildings = (select count(declares) as declares from [view_DeclareBuilding] DG where DE.LANKOD = DG.LANKOD and DE.KOMKOD = DG.KOMKOD group by DG.LANKOD, DG.KOMKOD)FROM BA_KOMKOD DE |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 05:08:04
|
| SELECTr2.LANKOD,r2.KOMKOD,count(r2.RNPREGBY) as declaresinto #aFROM BA_REGENH_01 r1JOIN BA_REGBYG_50A r2 ON r1.FNR = r2.FNRWHEREAND NOT EXISTS (SELECT 1 FROM BA_FastighetUndantag fu WHERE fu.Fnr = r1.FNR)AND NOT EXISTS (SELECT 1 FROM BA_ByggnadUndantag bu WHERE bu.IdPrefixById = r2.RNPREGBY and bu.IdById = r2.RIDREGBY)group by r2.LANKOD, r2.KOMKOD, r2.RNPREGBY, r2.RIDREGBYcreate clustered index ix on #a (LANKOD, KOMKOD)select LANKOD, KOMKOD, declares = count(declares) -- are you sure about the count here?into #bfrom #agroup by LANKOD, KOMKODcreate unique clustered index ix on #b (LANKOD, KOMKOD)UPDATE DESET nr_buildings = b declaresfrom BA_KOMKOD DEjoin #b on DE.LANKOD = b.LANKODand DE.KOMKOD = b.KOMKODDoing it this way minimises the locking for updates and will also show you which part is slow.If you need to make sure that underlying tables aren't updated during the process then you will have to hold locks but you should be able to optimise easily.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-11-19 : 05:27:31
|
| Thanks for the fast reply! The output your query produces does not match my former results, it writes 1 in every row.The view's count is set to 1 if where is true.The update counts every row in view and groups them by lankod and komkod giving more that 1 in every row. |
 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-19 : 05:36:14
|
| Don't immediately see why there is a difference.trySELECTr2.LANKOD,r2.KOMKOD,1 as declaresinto #aFROM BA_REGENH_01 r1JOIN BA_REGBYG_50A r2 ON r1.FNR = r2.FNRWHEREAND NOT EXISTS (SELECT 1 FROM BA_FastighetUndantag fu WHERE fu.Fnr = r1.FNR)AND NOT EXISTS (SELECT 1 FROM BA_ByggnadUndantag bu WHERE bu.IdPrefixById = r2.RNPREGBY and bu.IdById = r2.RIDREGBY)group by r2.LANKOD, r2.KOMKOD, r2.RNPREGBY, r2.RIDREGBYcreate clustered index ix on #a (LANKOD, KOMKOD)select LANKOD, KOMKOD, declares = sum(declares)into #bfrom #agroup by LANKOD, KOMKODcreate unique clustered index ix on #b (LANKOD, KOMKOD)UPDATE DESET nr_buildings = b.declaresfrom BA_KOMKOD DEjoin #bon DE.LANKOD = b.LANKODand DE.KOMKOD = b.KOMKODYou can see from #b the updtes that are going to be made.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|