前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mysql索引长度计算

Mysql索引长度计算

作者头像
MySQL轻松学
发布2018-03-09 13:57:39
3.8K0
发布2018-03-09 13:57:39
举报
文章被收录于专栏:MYSQL轻松学MYSQL轻松学
代码语言:javascript
复制
(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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-09-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MYSQL轻松学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档