How to guess how long the string corresponding to the text type field of the mysql database can be?

Background of the problem: Suppose a table has a text type field, which is specially used to store a large amount of description information, but when inserting data, the data length is too long, causing the data insertion to fail, but at this time, I don’t want to change it. Large field types feel a waste of space. When the business permits, it is proposed that when the data length exceeds the length of the text field, the insertion operation can be omitted.

Solution:

First of all, the length of the text field is 65535, but the description information cannot be ruled out in Chinese. Then utf8mb4 can occupy 4 bytes at most. We estimate according to the maximum possible. Then, according to the business situation, guess how many Chinese characters may be at most. By doing subtraction, you can get the upper limit length of the string data that can be inserted, and then use this upper limit length as the judgment condition to decide whether to add data.

Guess you like

Origin blog.csdn.net/weixin_38106322/article/details/112386020