Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2005 Computed Column Is Persisted

I have some computed columns in a table and need to know if I should set Is Persisted to true. What are the advantages? Are there any disadvantages? What does 'Is Persisted' mean?

like image 474
hilary Avatar asked May 27 '09 14:05

hilary


People also ask

Is persisted in SQL computed column?

Computed columns can be persisted. It means that SQL Server physically stores the data of the computed columns on disk. When you change data in the table, SQL Server computes the result based on the expression of the computed columns and stores the results in these persisted columns physically.

How do you check if computed column is persisted?

A persisted computed column is one that is physically stored in the table. If you don't specify that it's persisted, then the column's value will be calculated each time you run a query against it. You can query the sys. computed_columns system catalog view to find out whether a computed column is marked as persisted.

Are there any disadvantages of using computed column?

Some Limitations. You can not reference columns from other tables for a computed column expression directly. You can not apply insert or update statements on computed columns.

Can a computed column be a primary key?

A Computed Column cannot be used in a DEFAULT, FOREIGN KEY or NOT NULL constraints. If the expression that is used to define the Computed Column value is deterministic, the Computed Column can be involved in a PRIMARY KEY or UNIQUE constraint.


2 Answers

"Persisted" means "stored physically" in this context.

It means that the computed value is computed once on insert (and on updates) and stored on disc, so it does not have to be computed again on every select.

Persisted also causes a performance penalty on insert and updates, since the column must be computed, but will increase the performance on subsequent select queries.

So, it depends on your usage pattern, which approach to follow: if you update infrequently, but query a lot, you should set persisted = true.

If you update frequently, or if you do not care about retrieval performance, you should consider setting persisted = false

like image 147
Manu Avatar answered Sep 18 '22 15:09

Manu


One more thing not mentioned in other answers: a computed column must be PERSISTED to be usable from FOREIGN KEYs.

like image 23
Branko Dimitrijevic Avatar answered Sep 20 '22 15:09

Branko Dimitrijevic