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:
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'
You can see the rule_value
The length is 74:
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
, 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;
MySQL version command:
select version();
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!