Does n and Max in nvarchar (N) and nvarchar (max) affect performance?

Source: Internet
Author: User
Write 1 a few days ago Article : Analysis of the length, performance, and occupied space of varchar (N) and nvarchar (N) 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.

 

-- --------------------------------------------------------------------------------
--
Subject: whether N and Max in nvarchar (N) and nvarchar (max) affect performance
--
Author: yongfa365 http://www.yongfa365.com/yongfa365@qq.com
--
Createdate: 2010-11-22 23:31:04
--
Environment: Microsoft SQL Server 2008 R2 (RTM)-10.50.1600.1 (Intel x86)
--
APR 2 2010 15:53:02
--
Copyright (c) Microsoft Corporation
--
Enterprise Evaluation edition on Windows NT 5.2 <x86> (build 3790: Service Pack 2)
--
--------------------------------------------------------------------------------
--
Create two tables, one for nvarchar (4000) and the other for nvarchar (max)

Create   Table   [ DBO ] . [ Testnvarchar4000 ] (
ID
Int   Identity ( 1 , 1 ),
CNT
Nvarchar ( 4000 )
)
Go
Create   Table   [ DBO ] . [ Testnvarcharmax ] (
ID
Int   Identity ( 1 , 1 ),
CNT
Nvarchar ( Max )
)

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.