tags:

views:

60

answers:

2

Assume I have the following query:

Update LPMBonusReport Set BoxID = (Select ContainerSerialNumber 
       From Wip.vwWorkItem Where SerialNumber = LPMBonusReport.SubID)

The object Wip.vwWorkItem is a view.

Assume there are 100 rows in the LPMBonusReport table. Would the view get materialized 100 times, once for each row in LPMBonusReport, or would it get materialized just once?

Thanks.

+3  A: 

The optimizer would build a (single) execution plan based on LPMBonsReport and the tables comprising the view. Run and review your query in an SSMS query window with "Include Actual Execution Plan" on (it's an option in the "Query" menu.)

Philip Kelley
+2  A: 

I'd prefer to eliminate the subquery and see this written as:

Update BR
    Set BoxID = WI.ContainerSerialNumber
    from LPMBonusReport BR
        inner join Wip.vwWorkItem WI
            on BR.SubID = WI.SerialNumber 
Joe Stefanelli
@joe - You are correct. That is a better approach. I need to check the execution plan, but this seems to perform much better as well.
Randy Minder
@randy. Did this work out for you? If so, would you please mark as accepted. Thanks.
Joe Stefanelli
@Joe - I liked your suggestion, but it didn't answer my question. My question dealt with how the query optimizer would handle the materialization of the view given my original query.
Randy Minder
@randy: Fair enough. I am curious since you mentioned you were going to compare execution plans. Did my version perform better?
Joe Stefanelli