Project scenario:
提示:这里简述项目相关背景:
Using the MySQL database, because the storage length of the database field is set too small, the storage limit of the set character type has been reached, resulting in an error when the data is stored
Problem Description
提示:这里描述项目中遇到的问题:
Because the storage length of the database field is set too small, an error is reported for data storage, and the error message is as follows:
Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Data too long for column 'remark' at row 1
Cause Analysis:
Tip: Fill in the analysis of the problem here:
The character length that can be stored in the database field is set too small, and the storage type of the field is not selected as the appropriate type, and the character length stored in different data types is different.
Therefore, we need to set a reasonable storage space for the fields in the database, otherwise it will lead to storage failure or waste of space.
solution:
Tip: Fill in the specific solution to the problem here:
The appropriate field type should be selected, and the appropriate character length should be set according to actual needs
Summarize:
Field Type Design and Selection
1. There should not be too many fields in a single table*
A maximum of 30 is recommended
More fields will lead to performance degradation and increase the difficulty of development
**
2. Use small and simple suitable data types*
a. String type
Use char for fixed length , varchar for non-fixed length , and allocate appropriate and sufficient space
When char is queried, the space at the end will be removed
b. Decimal type
In general, you can use float or double , which takes up less space, but storage may lose precision
Decimal can store precise decimals, use decimal when storing financial data or longitude requirements
c. Time and date
Usually try to use timestamp , because it takes up less space, and it will automatically convert the time zone, no need to care about the regional time difference
datetime and timestamp can only store the smallest granularity is seconds, you can use the BIGINT type to store timestamps at the microsecond level
d. Big data blob and text
blob and text are string data types designed for storing very large data, but it is generally recommended to avoid their use
MySQL will treat each blob and text as an independent object, and the storage engine will do special processing when storing. When the value is too large, InnoDB uses a dedicated external storage area for storage, stores the pointer in the row, and then stores the actual value externally. These can cause serious performance overhead
Blob is a binary string, and text is a non-binary string , both of which can store a large amount of information. Blob mainly stores pictures, audio information, etc., while text can only store plain text files.
Third, try to set the column to NOT NULL
a. When a column that can be NULL is indexed, it takes up more storage space. Generally, changing a column that can be NULL to NOT NULL brings less improvement.
b. For columns that can be NULL, MySQL needs to do special processing when using indexes and value comparisons, which consumes a certain amount of performance and is more difficult to optimize
Suggestion: It is usually best to specify columns as NOT NULL unless you really need to store NULL values
4. Try to use integer as the primary key
a. Integer types are usually the best choice for identity columns because they are fast and can use AUTO_INCREMENT
b. String types should be avoided as identity columns as they are space consuming and generally slower than numeric types
c. More attention is also required for completely "random" strings. For example: strings generated by MD5(), SHAI() or UUID(). The new values generated by these functions are also arbitrarily distributed over a large space, which can cause INSERT and some SELECT statements to be slow
5. Data type and corresponding length setting
Each table column has a corresponding data type, which limits (or allows) the data stored in that column.
Common data types are: string, numeric, date and time, binary data types.
(1), string data type
(2), numeric data type
(3), date and time data type
(4), binary data type