Does the SQL Server Computing column occupy space?

Source: Internet
Author: User

I have a question on the internet today: Does the SQL Server Computing column occupy space?

In fact, you can find out the result by checking the msdn or Bol. When creating a calculated column, you can specify persisted as a parameter. You can use this parameter to specify that the database engine will physically store computing values in the table and update these computing values when any other column on which the calculation column depends is updated. And mark the calculated column
Persisted allows you to create an index for a deterministic but inaccurate computing column to improve performance.


If the persisted parameter is not used, the calculation column does not occupy disk space, but the value must be calculated when querying the calculation column. This will affect the performance (space for performance ).


Here we will test:

Use tempdb

Go

-- Createtable

Create Table udfeffect (idint,

Firstname varchar (100 ),

Lastname varchar (100 ))

Go

-- Insert onehundred thousand records

Insert into udfeffect (ID, firstname, lastname)

Select top 100000row_number ()
Over (orderby A. Name) rowid,

'Bob ',

Case whenrow_number ()
Over (orderby A. Name) % 2 = 1
Then 'Smith'

Else 'browser' end

From SYS. all_objects

Cross joinsys. all_objects B

Go

-- Check thespace used by table

Sp_spaceused
'Udfeffect'

Go

-- Addcomputed Column

Alter table dbo. udfeffectadd

Fullname as (firstname + ''+ lastname)

Go

-- Check thespace used by table

Sp_spaceused
'Udfeffect'

Go


We can see that the table size has not changed, and the data page has not grown.

-- Addcomputed column persisted

Alter table dbo. udfeffectadd

Fullname_p as (firstname + ''+ lastname) persisted

Go

-- Check thespace used by table

Sp_spaceused
'Udfeffect'

Go


Using the persisted parameter, we can see that the data is growing.

-- Clean updatabase

Drop table udfeffect

Go

The above script comes from: sqlserver
-Computed column-persisted and storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.