Sunday, March 25, 2012

computed columns

i need some info about when a computed column is recalculated. i am using a
udf and return value depends on columns from other tables. what if these
columns are updated ?
is the computed columns updated too then?
thanksNon-indexed computed columns are calculated whenever they are accessed.
Indexed computed columns are re-calculated whenever data is updated or
inserted.
David Portas
SQL Server MVP
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1127815998.052029.86950@.g44g2000cwa.googlegroups.com...
> Non-indexed computed columns are calculated >whenever they are accessed.
thanks

> Indexed computed columns are re-calculated whenever >data is updated or
> inserted.
is it re-calculated when the data in the other tables that the computed
column depends on updated too?

> --
> David Portas
> SQL Server MVP
> --
>|||As I understand it you want to use a computed column based on a scalar
UDF that references other tables. Such a column would not qualify to be
part of an index since the function would be non-deterministic. The
computed column would therefore behave like any other non-indexed
computed column: it would be calculated every time the computed value
is accessed in a query or other data retrieval operation, NOT when
dependent data is updated (whether or not that data is in other
tables).
In my opinion this would be an unwise use of a computed column. The
main use-case that I can think of for computed columns is to index an
expression that wouldn't otherwise be valid in an indexed view. Since
this doesn't apply to your case I think it would be a mistake to use a
computed column, especially since a join in a view is almost certainly
much more efficient than retirieving data through a scalar function.
Views are the place to denormalize your data.
David Portas
SQL Server MVP
--|||i got it now, thanks so much for the great info!
"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1127818936.527212.196370@.o13g2000cwo.googlegroups.com...
> As I understand it you want to use a computed column based on a scalar
> UDF that references other tables. Such a column would not qualify to be
> part of an index since the function would be non-deterministic. The
> computed column would therefore behave like any other non-indexed
> computed column: it would be calculated every time the computed value
> is accessed in a query or other data retrieval operation, NOT when
> dependent data is updated (whether or not that data is in other
> tables).
> In my opinion this would be an unwise use of a computed column. The
> main use-case that I can think of for computed columns is to index an
> expression that wouldn't otherwise be valid in an indexed view. Since
> this doesn't apply to your case I think it would be a mistake to use a
> computed column, especially since a join in a view is almost certainly
> much more efficient than retirieving data through a scalar function.
> Views are the place to denormalize your data.
> --
> David Portas
> SQL Server MVP
> --
>

No comments:

Post a Comment