This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3258|Reply: 1

[Source] MSSQL uses nvarchar(max) and varbinary(max) to store the difference

[Copy link]
Posted on 9/9/2023 9:36:22 AM | | | |
Requirements: If there is a field in the project that does not perform where and order queries, it can be stored using nvarchar(max) and varbinary(max) data types, and you need to consider the storage size, query speed, and transmission size.

ntext, text, and image

The ntext, text, and image data types will be removed in future releases. Avoid using these data types in new development efforts, and consider modifying applications that currently use these data types.Use nvarchar(max), varchar(max), and varbinary(max) instead.

varbinary [ ( n | max ) ]

Variable-length binary data. n has a value range of 1 to 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes. The length of the entered data can be 0 bytes. The ANSI SQL synonym for varbinary is binary varying.

Documentation:The hyperlink login is visible.

nvarchar [ ( n | max ) ]

Variable-size string data. n is used to define the string size (in double bytes), and it can be a value between 1 and 4,000. max indicates that the maximum storage size is 2^31-1 character (2 GB). The storage size is twice as large as n bytes + 2 bytes. For UCS-2 encoding, the storage size is twice as many as n bytes + 2 bytes, and the number of characters that can be stored is also n. For UTF-16 encoding, the storage size is still twice as many bytes as n bytes + 2 bytes, but the number of characters that can be stored may be less than n because supplementary characters use two double bytes (also known as proxy item pairs). ISO synonyms for nvarchar are national char varying and national character varying.

Documentation:The hyperlink login is visible.

New table

Database version: Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) Sep 24 2019 13:48:23 Copyright (C) 2019 Microsoft Corporation Enterprise Edition (64-bit)

Create two new tables, the fields are stored with nvarchar(max) and varbinary(max) data types, and the script is as follows:

Create a new console application based on .NET 6

To create a console application, you need to reference Microsoft.Data.SqlClient, the code is as follows:

Inserted separatelyThe same 300,000 pieces of data, the query is as follows:



Check the occupied storage space as shown in the following figure:



You can see thatnvarchar(max) type32.555 MB, the varbinary(max) type is occupied18.602 MB





Previous:National Bank Xbox Series X unlocking cross-region tutorial
Next:SQL Server searches for specified content across all stored procedures, functions, and views
 Landlord| Posted on 9/9/2023 9:38:11 AM |
Inserting 300,000 pieces of data, each taking 1 second, is as follows:



Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com