【Mysql优化】key和index区别
mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的。
1).key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查询用的)。包括primary key, unique key, foreign key 等。
primary key 有两个作用,一是约束作用(constraint),用来规范一个存储主键和唯一性,但同时也在此key上建立了一个index;
unique key 也有两个作用,一是约束作用(constraint),规范数据的唯一性,但同时也在这个key上建立了一个index;
foreign key也有两个作用,一是约束作用(constraint),规范数据的引用完整性,但同时也在这个key上建立了一个index;
可见,mysql的key是同时具有constraint和index的意义,这点和其他数据库表现的可能有区别。(至少在Oracle上建立外键,不会自动建立index),因此创建key也有如下几种方式:
(1)在字段级以key方式建立, 如 create table t (id int not null primary key);
(2)在表级以constraint方式建立,如create table t(id int, CONSTRAINT pk_t_id PRIMARY key (id));
(3)在表级以key方式建立,如create table t(id int, primary key (id));
其它key创建类似,但不管那种方式,既建立了constraint,又建立了index,只不过index使用的就是这个constraint或key。
2).index是数据库的物理结构,它只是辅助查询的,它创建时会在另外的表空间(mysql中的innodb表空间)以一个类似目录的结构存储。索引要分类的话,分为前缀索引、全文本索引等;
因此,索引只是索引,它不会去约束索引的字段的行为(那是key要做的事情)。
如,create table t(id int, index inx_tx_id (id));
3).最后的释疑:
(1).我们说索引分类,分为主键索引、唯一索引、普通索引(这才是纯粹的index)等,也是基于是不是把index看作了key。
比如 create table t(id int, unique index inx_tx_id (id)); --index当作了key使用
(2).最重要的也就是,不管如何描述,理解index是纯粹的index,还是被当作key,当作key时则会有两种意义或起两种作用。
例如:
1.创建表:添加一个主键和一个唯一key
CREATE TABLE t(
id INT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(40),
idCard CHAR(18),
sex CHAR(1),
UNIQUE KEY idCard(idCard))
2.查看索引:
语法:
mysql> show index from tblname; mysql> show keys from tblname;
用show index from t\G 与用 show keys from t\G显示结果一样。
mysql> show index from t\G
*************************** 1. row ***************************
Table: t
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: t
Non_unique: 0
Key_name: idCard
Seq_in_index: 1
Column_name: idCard
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.08 sec)
3.添加一个索引并查看索引与key
发现用用show index from t\G 与用 show keys from t\G显示结果一样。
mysql> alter table t add index sex(sex);
Query OK, 0 rows affected (0.46 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from t\G
*************************** 1. row ***************************
Table: t
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: t
Non_unique: 0
Key_name: idCard
Seq_in_index: 1
Column_name: idCard
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: t
Non_unique: 1
Key_name: sex
Seq_in_index: 1
Column_name: sex
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (0.06 sec)
4.查询建表语句:
发现索引的创建变为key的方式。。。。。
mysql> show create table t\G
*************************** 1. row ***************************
Table: t
Create Table: CREATE TABLE `t` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) DEFAULT NULL,
`idCard` char(18) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `idCard` (`idCard`),
KEY `sex` (`sex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.02 sec)
参考;https://www.cnblogs.com/pcyy/p/7943759.html
【Mysql优化】key和index区别的更多相关文章
- Mysql索引详解及优化(key和index区别)
MySQL索引的概念 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库 ...
- mysql中key 、primary key 、unique key 与index区别
一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id ) NOT NULL auto_increment, ) default NU ...
- 【Mysql】key 、primary key 、unique key 与index区别
参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...
- Mysql中Key与Index的区别
mysql的key和index多少有点令人迷惑,这实际上考察对数据库体系结构的了解的. 1 key 是数据库的物理结构,它包含两层意义,一是约束(偏重于约束和规范数据库的结构完整性),二是索引(辅助查 ...
- 待续--mysql中key 、primary key 、unique key 与index区别
mysql中key .primary key .unique key 与index区别
- MySQL 优化之 ICP (index condition pushdown:索引条件下推)
ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行 ...
- Mysql中的索引()key 、primary key 、unique key 与index区别)
CREATE TABLE pre_forum_post ( pid int(10) unsigned NOT NULL COMMENT '帖子id', fid mediumint(8) unsigne ...
- mysql -- 优化之ICP(index condition pushdown)
一.为了方法说明ICP是什么.假设有如下的表和查询: create table person( id int unsigned auto_increment primary key, home_add ...
- mysql中key和index的关系
原文链接:https://blog.csdn.net/top_code/article/details/50599840
随机推荐
- Python 3基础教程27-字典
这篇来介绍Python中的字典.字典一般用大括号包裹起来,里面的元素都是有键和值组成. # 字典 # 我们随便设计几个城市的明天的最高温度tem ={'北京':22,'上海':23,'深圳':24,' ...
- C#下16进制和BCD码转换代码
private static Byte[] ConvertFrom(string strTemp) { try { if (Convert.ToBoolean(strTemp.Length & ...
- Captcha 验证码Example
maven依赖 防止和spring中的servlet冲突 <dependency> <groupId>com.github.penggle</groupId> &l ...
- LeetCode - 66. Plus One(0ms)
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- kaldi常用文件查看指令
目录 1. ark特征文件 2. FST文件 3. mdl模型文件 4. 决策树文件 5. ali.gz对齐文件 资料来自kaldi官方文档. 转载注明出处. 1. ark特征文件 copy-feat ...
- cocos2d-x环境搭建 摘自百度文库
cocos2d-x环境搭建 引言:笔者在网上寻觅了很多资料,最终发现了这份实际可用的文档,供大家参考.源地址:http://wenku.baidu.com/view/93f7b0f1102de2bd9 ...
- Internet History,Technology and Security
Internet History,Technology and Security(简单记录) First Week High Stakes Research in Computing,and Comm ...
- lintcode-123-单词搜索
123-单词搜索 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. ...
- hdu2421(数学,因式分解素数筛)
Xiaoming has just come up with a new way for encryption, by calculating the key from a publicly view ...
- 【bzoj2957】楼房重建 分块+二分查找
题目描述 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子.为了简化问题,我们考虑这些事件发生在一个二 ...