Published on

July 18, 2010

Understanding Computed Columns in SQL Server

Computed columns are a powerful feature in SQL Server that allow you to create virtual columns in a table. These columns are not physically stored in the table, but are computed at runtime based on the values of other columns in the table. In this article, we will explore the concept of computed columns and their impact on storage and performance.

Storage Considerations

When creating a computed column, it is important to understand how it affects the storage space of the table. By default, a computed column is not materialized and does not take up any additional space in the database. The computed value is calculated on-the-fly whenever the column is accessed.

However, if you mark a computed column as PERSISTED, it is computed immediately and stored in the data table. This means that the computed value is physically stored in the table, which can result in increased storage requirements. It is important to consider the trade-off between storage space and performance when deciding whether to mark a computed column as PERSISTED.

Performance Considerations

The performance of computed columns can be influenced by whether they are marked as PERSISTED or not. In general, PERSISTED columns can provide better performance compared to non-persisted columns. This is because the computed value is pre-calculated and stored in the table, eliminating the need for computation at runtime.

However, there are scenarios where non-persisted columns can offer better performance. For example, if the computed column is rarely accessed or if the underlying data changes frequently, it may be more efficient to compute the value on-the-fly rather than storing it in the table.

Indexing Computed Columns

Another important aspect to consider when working with computed columns is indexing. Indexing a computed column can significantly improve the performance of queries that involve the computed column. By creating an index on a computed column, you can speed up search and retrieval operations.

It is worth noting that creating an index on a computed column does not increase the row length of the table. The index is stored separately from the table data, allowing for efficient storage and retrieval of the computed values.

Conclusion

Computed columns are a powerful feature in SQL Server that allow you to create virtual columns based on the values of other columns in a table. By understanding the storage and performance implications of computed columns, you can make informed decisions when designing your database schema.

In this article, we discussed how computed columns can impact storage space and performance. We explored the difference between persisted and non-persisted computed columns, and the considerations for indexing computed columns. By carefully considering these factors, you can optimize the performance and efficiency of your database.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.