Differences between char, varchar, and nvarchar in SQL Server databases

Source: Internet
Author: User
1. charFixed Length, n characters at most.
2. varcharA variable string with a maximum length of n. (N is an integer. The maximum length of n varies with different databases)
Differences between char and varchar:Varchar requires char to save space, but it is slightly less efficient than char. Varchar saves space than char because varchar is a variable string. For example, if varchar (5) is used to store the string "abc", it only occupies three bytes of storage space, char (5) occupies 5 bytes ("abc "). Varchar is slightly less efficient than char because, when modifying varchar data, data migration (that is, redundant I/O) may be caused by different data lengths ). Oracle expresses this redundant I/O description as "Row Migration ).
Row Migration ):  "When a row's record is inserted at the beginning, it can be stored in a block. The row is increased due to the update operation, and the free space of the block is full, at this time, row migration is generated. In this case, oracle will migrate the entire row of data to a new block, and oracle will keep the original pointer of the migrated row pointing to the new block for storing row data, this means that the row id of the migrated ROW will not change. "Block is the smallest data organization and management unit in oracle, the storage space unit of data file disks, and Minimum unit of database I/O ( That is to say, both read and write are the size of a block. If the block is not full, the varchar field with the length of the updated content is changed, and the varchar field with the length of the updated content not changed, the number of I/O operations is the same and there is no additional consumption. The extra I/O only occurs when the block is full. Therefore, the performance difference between char and varchar is quite subtle, in most cases, it is negligible, so the meaning of the "slightly" difference described above). Therefore, the general development experience is (I personally think): "If this field is not stored as a fixed character, Replace char completely with varchar. There is no concern.".
3. nvarcharThe features of nvarchar must be compared with those of varchar. The differences between nvarchar and varchar are mainly in the data storage mode: 1). varchar: Byte StorageData varchar (6) can store up to 6 bytes of data, such as "HAHAHA" and "abcdef "...... Note: The number of bytes of a Chinese character in the database depends on the unicode encoding method. For example, utf8 occupies 3 bytes in mysql, and chin's Chinese_PRC_CI_AS occupies 2 bytes...2). nvarchar: Storage by characterData nvarchar (6), which can store up to 6 Characters/Chinese data, for example, "Haha", "abcdef "...... the maximum length of nvarchar (m) is n * m (n depends on the data encoding method). If nvarchar stores English characters, it also stores the length of n Bytes according to the encoding method. That is to say, if Use Nvarchar stores English characters, which wastes more than half of the storage space.... Summary: 1. the performance gap between char and varchar is very small, which can be ignored. 2. In applications with large data volumes, using char and nvarchar may result in a waste of storage space.

 

Reference blog: http://www.cnblogs.com/hyddd/ reference article: "Row Chaining and Row Migration" "SQLSERVER char and varchar performance test"
Related Article

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.