env: Windows Server 2016
SQL Server 2016 SP2
The encounter problems as implicit conversion, and the last is to compare different:
Last variable table is declared field data types cause, the cause is physical Table.
The problems encountered in addition to the implicit conversion, as well as the problem of CPU usage.
The largest table has nearly 20 million transactions. On the same server across DB join 4 tables.
1. Adjust before execution
message:
Type conversion in expression ([XXXX].[dbo].[XXXX].[XXXX]=CONVERT(nvarchar(512),[XXXX].[dbo].[XXXX].[XXXX],0)) may affect "SeekPlan" in query plan choice, Type conversion in expression ([XXXX].[dbo].[XXXX].[XXXX]=CONVERT(nvarchar(512),[XXXX].[dbo].[XXXX].[XXXX],0)) may affect "SeekPlan" in query plan choice
Although the syntax of the query have to use MAXDOP = 4, but you can also lose a lot of the discovery process CPU
2. Adjust the table column data type, will be changed to VARCHAR NVARCHAR
command:
ALTER TABLE dbo.TESTTB01 ALTER COLUMN Token NVARCHAR(512) NULL
GO
ALTER TABLE dbo.TESTTB02 ALTER COLUMN Token NVARCHAR(512) NULL
GO
3. Perform syntax again
We only execute select portions will be removed insert into statement.
We can find Adjusted CPU no longer need to do a lot of typed conversion. But there is a doubt, the execution time is slightly elongated?
But users can also accept
4. that the following data types from the official website of priorities
Data type precedence (Transact-SQL)
NVARCHAR priority than VARCHAR, it will move to VARCHAR NVARCHAR.
Also personally feel can also optimize part is nvarchar (512), which put data is less than nvarchar (100).
SQL Server uses the following precedence order for data types:
- user-defined data types (highest)
- sql_variant
- xml
- datetimeoffset
- datetime2
- datetime
- smalldatetime
- date
- time
- float
- real
- decimal
- money
- smallmoney
- bigint
- int
- smallint
- tinyint
- bit
- ntext
- text
- image
- timestamp
- uniqueidentifier
- nvarchar (including nvarchar(max) )
- nchar
- varchar (including varchar(max) )
- char
- varbinary (including varbinary(max) )
- binary (lowest)