1. mysql executes the query plan, Key_len represents the number of bytes used by the index, and the number of bytes is related to three conditions.
mysql> CREATE TABLE T1 (v1 char (10));
Query OK, 0 rows affected
Mysql> CREATE index INDEX_V1 on T1 (v1);
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
2, whether to allow NULL, allow NULL to be one more byte, identify whether the value is null. As follows:
mysql> ALTER TABLE t1 modify column V1 char (TEN) not null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 30 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set
mysql> ALTER TABLE t1 modify column V1 char (ten) null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| id | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 31 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 Row in Set
3, whether it is variable length, longer than two bytes to represent the length, as follows:
mysql> ALTER TABLE t1 modify column v1 varchar () null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 33 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set
mysql> ALTER TABLE t1 modify column V1 char (ten) null;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| id | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 31 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 Row in Set
4, character type, n in varchar (n) is the number of characters, not how many bytes of memory are consumed. For different encodings, the same character takes up memory differently. Latin1 one character occupies 1 bytes, gb2312 one character occupies 2 bytes, utf8 one character occupies 3 bytes.
Mysql> ALTER TABLE t1 modify column V1 char (TEN) CharSet latin1;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 11 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set
mysql> ALTER TABLE t1 modify column V1 char (TEN) CharSet gb2312;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 21 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set
mysql> ALTER TABLE t1 modify column V1 char (TEN) charset UTF8;
Query OK, 0 rows affected
records:0 duplicates:0 warnings:0
Mysql> DESC SELECT * from t1;
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
| 1 | Simple | T1 | Index | NULL | INDEX_V1 | 31 | NULL | 1 | Using Index |
+----+-------------+-------+-------+---------------+----------+---------+------+------+-------------+
1 row in Set
The amount of memory that MySQL occupies