Home > Database > Mysql Tutorial > body text

How does MySQL determine the size of VARCHAR

王林
Release: 2023-05-27 21:20:05
forward
1265 people have browsed it

First of all, it is not recommended to use the TEXT type, because using TEXT will seriously affect efficiency. 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, and set the length to 255, and then enter the smallest json string:

How does MySQL determine the size of VARCHAR

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

SELECT
   id,rule_type,rule_value,CHAR_LENGTH(rule_value) as rule_value_length,rule_mark	
FROM
	test_table
WHERE
	rule_type = 'RANDOM_STR'
Copy after login

You can see the rule_value The length is 74:

How does MySQL determine the size of VARCHAR

Because the value of rule_value is a json array, the value in the array may become larger. This At this time, we need to ask the product confirmation, what is the maximum number of arrays in it?

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

74*10=740
Copy after login

, which is 740 characters, we can put the previous rule_value# The length of ##VARCHAR(255) can be changed to VARCHAR(740).

Finally, I need to add some knowledge:

  • TEXT can save up to 65535 characters, MEDIUMTEXT can store up to 16777215 characters, LONGTEXT can store up to 4294967295 characters, but it is generally not recommended to use;

  • View

    MySQL version command: select version();

  • In

    MySQL version 4 Previously, it was calculated in bytes, and after version 4, it was calculated in characters;

  • LENGTH()Returns the length of the string in bytes. CHAR_LENGTH()Returns the length of the string in characters.

The above is the detailed content of How does MySQL determine the size of VARCHAR. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template