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