SQL Server multi computed columns reference the same column

Copyright: copy, please indicate the source https://blog.csdn.net/weixin_39392627/article/details/86710466

env:Windows Server 2016

       SQL Server 2012 SP1

See this title must have felt that there is nothing to talk about, plus direct line calculated fields can not it do?

I was also think so, but not really ~ ~

Scenario: There is a table TESTTB01 create a field there, the data form is datetimeoffset, there are time zone +08: 00, also +07: 00. Date information needed to determine which day. But in different time zones make a difference dates.

Therefore want to use computational fields to find information is part of the day, and optimize query performance.

Documents about 500 million Brushes

1. Direct reference table in the same field, the establishment of three lines calculated fields.

command:

ALTER TABLE [dbo].[TESTTB01] ADD CreateDate AS (CONVERT(date, Created)) PERSISTED;
GO

ALTER TABLE [dbo].[TESTTB01] ADD CreateUTC AS (Created AT TIME ZONE 'UTC') PERSISTED;
GO

ALTER TABLE [dbo].[TESTTB01] ADD CreateUTCDate AS (CONVERT(date, (Created AT TIME ZONE 'UTC'))) PERSISTED;
GO

Value of the line more than four hours, error

Msg 4936, Level 16, State 1, Line 4

Computed column 'CreateUTC' in table 'TESTTB01' cannot be persisted because the column is non-deterministic.

Msg 4936, Level 16, State 1, Line 6

Computed column 'CreateUTCDate' in table 'TESTTB01' cannot be persisted because the column is non-deterministic.

 

2. Try to re-establish air test table: TESTTB01_NEW

command:

CREATE TABLE [dbo].[TESTTB01_NEW](
[Id] [uniqueidentifier] NOT NULL,
[Status] [int] NOT NULL,
[Created] [datetimeoffset](7) NOT NULL,
 CONSTRAINT [PK_TESTTB01_NEW] PRIMARY KEY NONCLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

3. Establish function and set the computational field

command:

CREATE FUNCTION dbo.FnCreateUTC(@DATE datetimeoffset)  
RETURNS datetimeoffset
AS  
BEGIN  
     DECLARE @CreateUTC DATETIMEOFFSET;  
     SELECT  @CreateUTC= (Created AT TIME ZONE 'UTC') FROM dbo.TESTTB01_NEW
     RETURN(@CreateUTC);  
END;  
GO

ALTER TABLE [dbo].[TESTTB01_NEW] ADD CreateUTC AS dbo.FnCreateUTC(created) PERSISTED;
GO

Error messages can not be used PERSISTED

Msg 4936, Level 16, State 1, Line 49

Computed column 'CreateUTC' in table 'TESTTB01_NEW' cannot be persisted because the column is non-deterministic

 

4. PERSISTED not used

command:

ALTER TABLE [dbo].[TESTTB01_NEW] ADD CreateUTC AS dbo.FnCreateUTC(created) ;
GO

 

The computational supplemented on two fields, a use function, a function not used

command:

ALTER TABLE [dbo].[TESTTB01_NEW] ADD CreateUTC AS dbo.FnCreateUTC(created);
GO

ALTER TABLE [dbo].[TESTTB01_NEW] ADD CreateUTCDate AS (CONVERT(date, (Created AT TIME ZONE 'UTC')));
GO

 

Conclusion: Each field can only be calculated with reference to a type field, but is not limited to the use of function

In Step 3 is a bad writing, poor performance of this function, function content into the much better performance

     DECLARE @CreateUTCDate DATETIMEOFFSET;  

     SET @CreateUTCDate= (CONVERT(date, (@DATE AT TIME ZONE 'UTC')))

 

Guess you like

Origin blog.csdn.net/weixin_39392627/article/details/86710466