N does not affect the size and performance of the database. Even n is Max, it may not affect the storage space. If you have official documents, you can check whether N affects the performance, I just heard the story and couldn't get the arguments. Today I will give an example to prove the above. If anything is wrong, please make a brick.
)
)
Go
--Insert 0.1 million data records. Each CNT contains 4000 characters (the maximum value of N in nvarchar (n), which is about 1.6 GB.
Begin Transaction
Declare @ I Int ;
Set @ I = 0 ;
While @ I < 100000
Begin
Insert Into [ Testnvarchar4000 ] Values ( Left ( Replicate ( Cast ( @ I As Nvarchar ) + ' I'm Liu yongfa. ' , 1000 ), 4000 ))
Insert Into [ Testnvarcharmax ] Values ( Left ( Replicate ( Cast ( @ I As Nvarchar ) + ' I'm Liu yongfa. ' , 1000 ), 4000 ))
Set @ I = @ I + 1
End
Commit
--Clear the cache or restart the SQL service to test the query speed and LOB reading status (lob indicates a large object)
--Test testnvarcharmax
DBCCDropcleanbuffers
DBCCFreeproccache
Set Statistics Io On
Set Statistics Time On
Select Count ( * )
From Testnvarcharmax
Where CNT Like ' % Liu yongfa % '
Set StatisticsTimeOff
Set StatisticsIoOff
--Test testnvarchar4000
DBCCDropcleanbuffers
DBCCFreeproccache
Set Statistics Io On
Set Statistics Time On
Select Count ( * )
From Testnvarchar4000
Where CNT Like ' % Liu yongfa % '
set Statistics time off
set Statistics Io off
-- result:
-- (one row is affected)
-- table 'testnvarcharmax '. Scan count 3, logical reads 100000, physical reads 8494, pre-read 99908, lob logic reads 0, lob physical reads 0, and LOB pre-read 0.
-- SQL Server execution time:
-- CPU time = 1172 milliseconds, occupied time = 30461 milliseconds.
--(One row is affected)
--Table 'testnvarchar4000 '. Scan count 3, logical reads 100000, physical reads 8523, pre-read 99916, lob logic reads 0, lob physical reads 0, and LOB pre-read 0.
--
--SQL Server execution time:
--CPU time = 968 milliseconds, occupied time = 30038 milliseconds.
--The results show that the read time is basically the same and no lob is read. The above data is tested for multiple times.
--The length of the first 1000 fields in testnvarcharmax is + 1. to test whether the length exceeds 4000 characters, lob is used to read
UpdateTestnvarcharmaxSetCNT=CNT+'1' WhereID<=1000
-- Result:
-- (One row is affected)
-- Table 'testnvarcharmax '. Scan count 3, logical reads 100000, physical reads 8292, pre-read 99696, lob logic reads 900, lob physical reads 83, and LOB pre-read 0.
--
-- SQL Server execution time:
-- CPU time = 1124 milliseconds, occupied time = 30318 milliseconds.
-- The result shows that lob is used for reading. However, the time difference is not big.
-- Double the field value and test again:
Update Testnvarcharmax Set CNT = CNT + CNT Where ID <= 1000
-- Result:
-- (One row is affected)
-- Table 'testnvarcharmax '. Scan count 3, logical reads 100000, physical reads 8164, pre-read 99521, lob logic reads 1000, lob physical reads 101, and LOB pre-read 0.
--
-- SQL Server execution time:
-- CPU time = 1094 milliseconds, occupied time = 31095 milliseconds.
From the above results, we can basically prove the point of view mentioned in the article. N and Max in nvarchar (n) or nvarchar (max) are only a symbol and will not affect space occupation and performance, unless Max stores more than 4000 characters, we only need to pay attention to the actual business needs,
For more results, see: http://www.yongfa365.com/Item/SQLServer-varchar-nvarchar.html