sql server - SQL: Persisted computed field in table sometimes not used during query execution -
possible duplicate:
why execution plan include user-defined function call computed column persisted?
in sql server 2008 i'm running sql profiler on long running query , can see persisted computed column being repeatedly recalculated. i've noticed before , anecdotally i'd seems occur on more complex queries and/or tables @ least few thousand rows.
this recalculation cause of long execution speeds dramatically if comment out 1 column returned results (the field computed running xpath against xml field).
edit: offending sql has following structure:
declare @orderby nvarchar(50); select a.[id], case when @orderby = 'col1' a.[computedcol1] when @orderby = 'col2' c.[computedcol2] else c.[computedcol3] end [order] [stuff] inner join [stuffcode] sc on a.[code] = sc.[code]
all columns nvarchar(50) except computedcol3 nvarchar(250).
the query optimizer tries pick cheapest plan, may not make right choice. persisting column putting in main table (in clustered index or heap) in order pull out these values, normal data access paths still required.
this means engine may choose other indexes instead of main table satisfy query, , choose recalculate computed column if thinks doing combined chosen i/o access pattern cost less. in general, fair amount of cpu cheaper little i/o, no internal analysis of cost of expression done, if column calls expensive udf may make wrong decision.
putting index on column make difference. note don't have make column persisted put index on it. if after making index, engine still making mistakes, check see if have proper statistics being collected , updated on indexes on table.
it if posted structure of table (just important columns) , definitions of indexes, along ideas of execution plan looks when things go badly.
one thing consider may better recompute column in cases, make sure it's correct force engine go before doing so.
Comments
Post a Comment