(root:hostname:Fri May 29 14:10:50 2015)[liangxl]> show create table liang_2;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------
| Table | Create Table
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------
| liang_2 | CREATE TABLE `liang_2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`email` varchar(20) DEFAULT NULL,
`addr` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `ind_liang_2_name` (`name`(5)),
KEY `ind_liang_2_email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)
(root:hostname:Fri May 29 14:10:56 2015)[liangxl]> create index ind_liang_2_addr on liang_2(addr);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root:hostname:Fri May 29 14:11:13 2015)[liangxl]> explain select * from liang_2 where addr='asdsdadasdads';
+----+-------------+---------+------+------------------+------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+------------------+------------------+---------+-------+------+-------------+
| 1 | SIMPLE | liang_2 | ref | ind_liang_2_addr | ind_liang_2_addr | 62 | const | 4 | Using where |
+----+-------------+---------+------+------------------+------------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
(root:hostname:Fri May 29 14:11:32 2015)[liangxl]> explain select * from liang_2 where email='asas@ss';
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------------+
| 1 | SIMPLE | liang_2 | ref | ind_liang_2_email | ind_liang_2_email | 63 | const | 4 | Using where |
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------------+
1 row in set (0.01 sec)
(root:hostname:Fri May 29 14:11:51 2015)[liangxl]> explain select * from liang_2 where name='aaaab';
+----+-------------+---------+------+------------------+------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+------------------+------------------+---------+-------+------+-------------+
| 1 | SIMPLE | liang_2 | ref | ind_liang_2_name | ind_liang_2_name | 18 | const | 1 | Using where |
+----+-------------+---------+------+------------------+------------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
(root:hostname:Fri May 29 14:12:25 2015)[liangxl]> \s;
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
结论:
1、int:固定占用4个字节
2、不同字符集占用字节数不同:
3、latin1:2个字节
4、utf8:3个字节
5、是否为空占用1个字节,not null不占用字节
6、变长字段需要记录长度占用2个字节
7、索引长度计算公式:varchar(20)*3+1+2=63