標籤:
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
mysql 佔用的記憶體大小