码迷,mamicode.com
首页 > 其他好文 > 详细

Computed column Usage

时间:2016-02-02 18:57:52      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

顾名思义,计算列是通过计算产生值得列,Computed_Column_Name as Computed_Expression,计算列的数据类型是由计算表达式的结果确定的。

计算列有两个特性:Persisted,Deterministic 和自动确定Data Type

1, 如果没有为计算列指定Persisted 关键字,那么计算列是不会物理存储的,是个Virtual Column,只存储计算的逻辑,而不占用物理存储空间;如果为计算列指定Persisted 关键字,那么计算列将占用物理存储空间,将计算值存储在物理设备中。

2, 根据计算列表达式的不同,可以将计算列区分为可确定性和不可确定性。Deterministic 是指Computed Column 的value 是能确定下来的,如果该row中的相关联column没有变化,那么Computed Column 的value是不变的;

3, 在创建计算列时,不能指定Data type, 计算列的数据类型是由计算表达式的结果确定的。

nullability 需要显示指定,否则,SQL Server 默认是 nullable。

 

Example:使用binary_checksum计算多个字符串 columns 的CheckSum,只需要进行int类型的比较,速度会快很多。

if object_id(Ndbo.City_Staging,NU) is not null
drop table dbo.City_Staging

create table dbo.City_Staging
(
    [Country] [nvarchar](512) NOT NULL,
    [State] [nvarchar](512) NOT NULL,
    [City] [nvarchar](512) NOT NULL,

    [Value_binary_checksum] as binary_checksum(
                    lower(ltrim(rtrim([Country]))),
                    lower(ltrim(rtrim([State]))),
                    lower(ltrim(rtrim([City])))
                    ) persisted not null
)
with(data_compression=page);


Appendix:MSDN Syntax

<computed_column_definition> ::= 
column_name AS computed_column_expression 
[ PERSISTED [ NOT NULL ] ]
[ 
    [ CONSTRAINT constraint_name ]
    { PRIMARY KEY | UNIQUE }
        [ CLUSTERED | NONCLUSTERED ]
        [ 
            WITH FILLFACTOR = fillfactor 
          | WITH ( <index_option> [ , ...n ] )
        ]
        [ ON { partition_scheme_name ( partition_column_name ) 
        | filegroup | "default" } ]

    | [ FOREIGN KEY ] 
        REFERENCES referenced_table_name [ ( ref_column ) ] 
        [ ON DELETE { NO ACTION | CASCADE } ] 
        [ ON UPDATE { NO ACTION } ] 
        [ NOT FOR REPLICATION ] 

    | CHECK [ NOT FOR REPLICATION ] ( logical_expression ) 
] 


computed_column_expression                                

Is an expression that defines the value of a computed column. A computed column is a virtual column that is not physically stored in the table, unless the column is marked PERSISTED. The column is computed from an expression that uses other columns in the same table. For example, a computed column can have the definition: cost AS price * qty. The expression can be a noncomputed column name, constant, function, variable, and any combination of these connected by one or more operators. The expression cannot be a subquery or contain alias data types.

Computed columns can be used in select lists, WHERE clauses, ORDER BY clauses, or any other locations in which regular expressions can be used, with the following exceptions:  

  • Computed columns must be marked PERSISTED to participate in a FOREIGN KEY or CHECK constraint.

  • A computed column can be used as a key column in an index or as part of any PRIMARY KEY or UNIQUE constraint, if the computed column value is defined by a deterministic expression and the data type of the result is allowed in index columns.

    For example, if the table has integer columns a and b, the computed column a+b may be indexed, but computed column a+DATEPART(dd, GETDATE()) cannot be indexed because the value may change in subsequent invocations.  

  • A computed column cannot be the target of an INSERT or UPDATE statement.

Based on the expressions that are used, the nullability of computed columns is determined automatically by the Database Engine. The result of most expressions is considered nullable even if only nonnullable columns are present, because possible underflows or overflows also produce NULL results. Use the COLUMNPROPERTY function with the AllowsNull property to investigate the nullability of any computed column in a table. An expression that is nullable can be turned into a nonnullable one by specifying ISNULL with the check_expression constant, where the constant is a nonnull value substituted for any NULL result. REFERENCES permission on the type is required for computed columns based on common language runtime (CLR) user-defined type expressions.

PERSISTED               

Specifies that the SQL Server Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated. Marking a computed column as PERSISTED lets you create an index on a computed column that is deterministic, but not precise. For more information, see Indexes on Computed Columns. Any computed columns that are used as partitioning columns of a partitioned table must be explicitly marked PERSISTED. computed_column_expression must be deterministic when PERSISTED is specified.

 

Computed column Usage

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5178225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!