How to Create Computed Columns in SQL Server (Step-by-Step) | Easy Beginners Tutorial

Опубликовано: 10 Июнь 2024
на канале: The SQL Guy
212
3

How to add and index Computed Columns in SQL Server. A really simple overview of what we can and can't do with Computed Columns

Content

00:00 Tutorial overview
00:31 How to add a Computed Column
01:33 Adding a PERSISTED column
01:50 Performance Comparison PERSISTED vs Non PERSISTED
04:04 Can we UPDATE Computed Columns?
04:44 Problems with Non-Deterministic Computed Columns
05:25 Adding Computed columns on VARCHAR columns
06:25 Dropping Computed Columns and Indexes

What are Computed Columns?

Computed columns in SQL Server are virtual columns that are not physically stored in the table unless they are marked as PERSISTED. The performance difference of these two is marginal, which I cover in the video

Computed Columns are defined using expressions that can include other columns in the table, constants, functions, and operators.

Among other things, Computed columns provide a powerful way to encapsulate business logic directly within the database schema, promoting code reuse and simplifying data retrieval. For example, it be a great substitute for complex code by effectively adding another column to a table. This is a huge benefit when performing SQL performance tuning.

What are Computed Columns used for?

1. Data Validation and Integrity: By using computed columns, you can enforce data validation rules. For example, a column that computes age based on a date of birth ensures that the age is always up-to-date and consistent. I give a good example of this in the tutorial

2. Indexing for Performance: When a computed column is marked as PERSISTED, it can be indexed. This can significantly improve query performance, especially for complex calculations that are frequently queried.

3. Simplified Querying: Computed columns simplify SQL queries by abstracting complex expressions. Instead of repeating the same calculations in multiple queries, the logic can be encapsulated in a computed column.

Benefits of Computed Columns

1. Enhanced Query Performance: When properly indexed, computed columns can significantly boost query performance. Indexes on persisted computed columns enable faster searches and aggregations.

2. Simplified Maintenance: Maintenance becomes easier as business logic changes can be made in one place. Updates to the logic are automatically reflected wherever the computed column is used.

Cons of Computed Columns

1. Performance Overhead: As mentioned in the tutorial, Non-persisted computed columns are calculated on-the-fly during query execution, which can lead to performance overhead, especially for complex calculations or large datasets

2. Storage Considerations: Persisted computed columns consume additional storage space. While this can improve read performance, it comes at the cost of increased storage requirement. Personally I don't see this is a big 'con' however, as the overheads of additional storage is far outweighed by the performance benefits that Computed Columns bring

Computed Columns Best Practices

1. Indexing Strategy: Implement an effective indexing strategy for computed columns. Indexes should be created based on query patterns and usage frequency to maximize performance gains.

2. Regular Maintenance: Regularly monitor and maintain computed columns and their indexes. Perform index rebuilds and updates as needed to maintain optimal performance

3. Optimize Expressions:
Ensure that the expressions used in computed columns are optimized for performance. Avoid using complex or resource-intensive calculations that can degrade query performance.


Смотрите видео How to Create Computed Columns in SQL Server (Step-by-Step) | Easy Beginners Tutorial онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь The SQL Guy 10 Июнь 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 21 раз и оно понравилось людям.