Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does MySQL determine the size of VARCHAR?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how MySQL determines the size of VARCHAR". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to determine the size of VARCHAR in MySQL" can help you solve the problem.

First of all, it is not recommended to use the TEXT type, because using TEXT can seriously affect efficiency, and the best way is to use VARCHAR and determine the maximum length of the field.

We can first define a field rule_value in the table, set the length to 255, and then enter the smallest json string into it:

Use the CHAR_LENGTH function of MySQL to calculate the size of the rule_value:

SELECT id,rule_type,rule_value,CHAR_LENGTH (rule_value) as rule_value_length,rule_mark FROM test_tableWHERE rule_type = 'RANDOM_STR'

You can see that the length of the rule_value is 74:

Because the value of rule_value is an json array, the value in the array may become larger, so at this time, we need to ask the product to confirm, how many arrays are there?

After inquiry, the maximum number of arrays is 10, so the length of the calculated rule_value is:

7410 740

That is, 740 characters, we can change the length of the previous rule_value VARCHAR (255) to VARCHAR (740).

Finally, we need to add a little bit of knowledge:

A maximum of 65535 characters can be stored in TEXT, a maximum of 16777215 characters in MEDIUMTEXT, and a maximum of 4294967295 characters in LONGTEXT, but it is generally not recommended.

View MySQL version command: select version ()

Before MySQL version 4, it was calculated by bytes, and after version 4 by characters.

LENGTH () returns the length of the string in bytes. CHAR_LENGTH () returns the length of a string in characters.

This is the end of the introduction to "how MySQL determines the size of VARCHAR". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report