Comparison of varchar and char in a database

Source: Internet
Author: User

Some information is transferred from Elsewhere .....

I. Data storage overhead

1. Char (n) is fixed-length , that is, when you enter a character that is less than the number you specify, char (8), the character you enter is less than 8 o'clock, and it will then fill the empty value back . When you enter a character that is larger than the specified number, it intercepts the characters that are out of the bounds .

In the program, will return to you 8 bits, the back of the space to fill up;

In the database, char (8), occupies 16 bytes (1 characters = 2 bytes);

2.varchar (n) is a variable-length, non-Unicode character data with a length of n bytes. n must be a numeric value between 1 and 8000. storage size is the actual length of bytes of input data, not n bytes . The input data character length can be zero.

Two. Inserting data

The null value of the 1.char column occupies storage space.

2. The null value of the varchar column does not occupy storage space.

Inserting the same number of NULL values, the varchar column is significantly more efficient than the Char column.

When inserting non-null data, the efficiency of the varchar column is significantly higher than the Char column, regardless of whether the column involved in inserting the data is indexed.

Three. Updating data

If an index is not indexed on the updated column, char is less efficient than varchar, but the efficiency difference is small.

If an index is established on an updated column, char is less efficient than varchar, and the efficiency varies greatly.

Four. Modify the structure

Regardless of whether the type of columns that are added or deleted is char or varchar, the operation can be done faster and with no difference in efficiency.

There is a significant efficiency difference between char and varchar for increasing the width of the column, and the varchar column does not take much time, and it takes a long time to modify the Char column.

Five. Data retrieval

The data retrieval of the varchar type is slightly better than the scan of char, regardless of whether it is indexed.


What kind of application do we use in the actual development?

Char is used when determining the length of a string, data changes frequently, and data retrieval requirements;

VarChar is used when you are unsure of the length of the string, few changes to the data, and frequent queries.

Comparison of varchar and char in a database

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.