索引从本质上来说也是一种表,这样的表存储被列为索引的列项值和指向真正完整记录的指针。索引对用户透明。仅仅被数据库引擎用来加速检索真实记录。有索引的表。insert和update操作会耗费很多其它时间而select则会变快。由于insert和update操作同一时候也要insert和update索引值。但这些操作对用户也透明。

索引通常运用在where、join、order by语句中[1]。

在mysql中,index和key是同义词,两者能够互换[2]。一张表最多能有16个索引。每一个索引最多由15个列组成。mysql中有4种类型索引。索引根据表类型(也既是存储引擎)有不同存储方式[3]。

  • primary key 能够指定一列或多列组合成索引,用来唯一确定一条记录,每一个指定为primary key的项必须唯一,且不能为空。一张表至多有一个primary key。设置了 primary key 的表自己主动为 primary key

    建立索引。
  • unique key 与 primary key 相似,能够指定一列或多列组合成索引,用来唯一确定一条记录。

    每一个指定为 unique key 的项必须唯一,同意有NULL值。一张表能够有多个 unique key。

  • fulltext类型索引仅仅能用于MyISAM表,也仅仅能用于char。varchar,text列项。
  • 普通 index。没有唯一性限制,也没有非空限制。
存储引擎 可能的索引类型
MyISAM btree
InnoDB btree
Memory/Heap hash,btree
NDB btree,hash

索引能够在创建表的时候创建:

create table table_name (create_column_definition [, ...] );

当中 create_column_definition 能够替换成:

  • column_name column_definetion
  • [constraint [symbol]] primary key (column_name, ...) [index_type]
  • [constraint [symbol]] unique [index|key] (column_name, ...) [index_type]
  • {index|key} [index_name] (column_name, ...) [index_type]
  • {fulltext} [index | key] (column_name, ...) [index_type]

    当中 column_definetion 能够替换成:
  • data_type [not null | null] [default default_value]
  • [auto_increment] [unique [key] | [primary] key]
  • [comment 'string'] [reference_definition]

比如:

create table test(
`id` int unsigned not null auto_increment,
`data0` varchar(20),
`data1` varchar(20),
primary key (`id`),
);
create table test(
id int unsigned not null auto_increment primary key,
`data0` varchar(20),
`data1` varchar(20)
);

表创建之后也能够加入索引:

1)使用alter命令:

alter table table_name
[alter_specification [, alter_specification] ... ];

当中 alter_sepcification 能够替换成随意一种:

  • add [constraint [symbol]] primary key (index_cloumn_name, ... ) [index_type]
  • add [constraint [symbol]] unique [index|key] [index_name] (index_cloumn_name, ... ) [index_type]
  • add {index | key} [index_name] (index_cloumn_name, ... ) [index_type]
  • add [fulltext] [index|key] [index_name] (index_cloumn_name, ... ) [index_type]

当中 index_cloumn_name 能够替换成:column_name [(length) [asc|desc]]

当中 index_type 能够替换成:using {btree|hash}

比如:

alter table test add unique key `index_data0` (`data0` (10));

2)使用create命令:

create [unique|fulltext|spatial] index index_name
on table_name (index_cloumn_name, ... ) [index_type];

当中 index_cloumn_name 能够替换成:column_name [(length) [asc|desc]]

当中 index_type 能够替换成:using {btree|hash}

须要注意的几点:

  • create命令不能用于创建primary key
  • 能够为char,varchar。binary,varbinary设置索引前缀长度。这意味着能够仅仅索引这些字段的前面某部分。

  • blob和text若为索引项类型,必须指定索引前缀长度[5]。

比如:

create index `index_data1` on test (`data1` (10));

删除索引:

alter table table_name drop primary key;
alter table table_name drop {index | key} index_name;

当创建多列索引之后,查询所有索引或索引的前n列(与定义索引顺序一致),能够使用该索引[6]。比如:

create table test(
id int not null auto_increment,
last_name char(30) not null,
first_name char(30) not null,
primary key (id),
index name (last_name, first_name)
);

下面这些查询能用到索引 name:

select * from test where last='xyb';
select * from test where last='xyb' and first_name='love';
select * from test where last='xyb' and (first_name='love' or first_name='Charlotte');
select * from test where last='xyb' and first_name >= 'l' and first_name <= 'n';

下面这些chauncey不能用到索引 name:

select * from test where first_name='Charlotte';
select * from test where last='xyb' or first_name='Charlotte';

综合解说索引能够參见链接[7]

參考链接:

[1]http://www.tutorialspoint.com/mysql/mysql-indexes.htm

[2]http://stackoverflow.com/questions/3844899/difference-between-key-primary-key-unique-key-and-index-in-mysql

[3]https://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html

[4]https://dev.mysql.com/doc/refman/5.0/en/alter-table.html

[5]https://dev.mysql.com/doc/refman/5.0/en/create-table.html

[6]https://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html

[7]http://blog.csdn.net/tianmohust/article/details/7930482

mysql索引简单介绍的更多相关文章

  1. PHP 17: MySQL的简单介绍

    原文:PHP 17: MySQL的简单介绍 这一章将简单介绍MySQL的基本知识. 本文来自http://lib.hackbase.com/html/8/35125.htm. MySQL是最受欢迎的开 ...

  2. MySQL索引知识介绍

    前言: 索引是MySQL数据库中的重要对象之一,索引的目的在于提高查询效率.可以类比字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取即可.索引是表的目录,在查找内容之前可以先 ...

  3. MySQL 索引的介绍与应用

    Mysql索引 一. mysql 索引 索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息. 二:MySQL索引类型 按存储结构区分:聚集索引(又称聚类索引,簇 ...

  4. mysql,简单介绍一下索引

    汉字很多,人力有时尽,人不可能记住所有的字,为了解决这个问题,于是有了字典.数据库里的数据很多,为了方便检索,于是有了索引. 索引,是一种数据结构,在这种数据结构中实现了高级的查找算法,索引可以帮助我 ...

  5. mysql索引简单分析

    索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将对整个表进 ...

  6. mysql索引详细介绍

    博客: https://blog.csdn.net/tongdanping/article/details/79878302#%E4%B8%89%E3%80%81%E7%B4%A2%E5%BC%95% ...

  7. mysql 索引type介绍

    以下全部详细解析explain各个属性含义: 各属性含义:    id: 查询的序列号    select_type: 查询的类型,主要是区别普通查询和联合查询.子查询之类的复杂查询 SIMPLE:查 ...

  8. mysql索引基本介绍

    转载:https://blog.csdn.net/weixin_34392906/article/details/93707682 转载于:https://www.cnblogs.com/maohui ...

  9. Oracle索引简单介绍与示例

    索引的三大特性 1索引高度 在SQL检索数据(SELECT)的时候,索引的高度的不同对检索的效率有明显的差别,数据库访问索引需要读取的数据块通常是索引的高度+1个数据块数,也就是说索引的高度越高,访问 ...

随机推荐

  1. MySQL性能调优与架构设计读书笔记

    可扩展性设计之数据切分 14.2 数据的垂直切分 如何切分,切分到什么样的程度,是一个比较考验人的难题.只能在实际的应用场景中通过平衡各方面的成本和利益,才能分析出一个真正适合自己的拆分方案. 14. ...

  2. (转)第三方登录(QQ登录)开发流程详解

    近排由于工作的繁忙,已经一个星期没写博文做分享了,接下来我对网站接入第三方登录----QQ登录的实现逻辑做一个详细的讲解. 对于整个流程的详细文档可以到QQ互联官网(http://wiki.conne ...

  3. 关于.net类型转换判断问题

    做项目时,发现这个问题,由于数据库中的字段可能为null,如果在.net程序直接转换时,比如 ltTime.Text =DateTime.Parse(dt.Rows[0]["m_Time&q ...

  4. 请帮我看看这个页面,红色部份如何改才能保存到ACCess数据库中

    <% if session("shiwei_username")="" then %> <script language="java ...

  5. innodb的innodb_buffer_pool_size和MyISAM的key_buffer_size

    一. key_buffer_size 对MyISAM表来说非常重要. 如果只是使用MyISAM表,可以把它设置为可用内存的 30-40%.合理的值取决于索引大小.数据量以及负载 -- 记住,MyISA ...

  6. Linux下快速搭建DNS服务器

    一.术语解释:TTL Time To Live 缓冲保留时间ORIGIN 属于哪个域@ 代指域IN 开头需要空格SOA 一行记录类型的开始参数:forwarders {} 指向自己无法解析的域名跳转到 ...

  7. 使用DML语句【weber出品必属精品】

    DML语句包含以下语法: INSERT:往一个表中增加新行 DELETE:从一个表中删除掉现有的行 UPDATE:更改一个表中现有的行 INSERT语句语法:INSERT INTO TABLE(COL ...

  8. iOS 天气应用代码中文介绍

    天气应用 解释请求参数 q: 表示Location(可以给出城市名字;或者直接给城市的经纬度) 例子:q=beijing 例子 q=48.834,2.394 num_of_days: 需要预报的天数 ...

  9. HTTP Content-type 对照表

    文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流) application/octet-stream .tif ...

  10. sql server使用说明

    什么是sql server? SqlServer是微软的一款数据库系统产品. 是DBMS中的一种. 当每一个数据库安装到每一台电脑后,都会与计算机名称(有的是IP地址)关联.因为服务器用途的电脑不能经 ...