1、mysql執行查詢計划,key_len表示索引使用的字節數,這個字節數和三個條件有關。
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、是否允許為null,允許為null 會多一個字節,標識取值是否為NULL。如下:
mysql> alter table t1 modify column v1 char(10) 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(10) 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、是否為變長,變長多出兩個字節表示長度,如下:
mysql> alter table t1 modify column v1 varchar(10) 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(10) 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、字符類型,varchar(n) 中的n是字符個數,不是占用多少個字節的內存。對於不同的編碼,同樣一個字符占用的內存不一樣。latin1 一個字符占用1個字節,gb2312 一個字符占用2個字節,utf8 一個字符占用3個字節。
mysql> alter table t1 modify column v1 char(10) 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(10) 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(10) 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