【MySQL】索引的增删查

上篇博客讲解了索引的底层结构
本篇介绍索引的使用

一. 主键索引

MySQL默认会按照主键索引进行排序
关键字:primary key
即使建表时没有指明主键,MySQL也会选择合适的属性作主键

  • 第一种方式:在创建表时,直接在字段后指定primary key
mysql->create table user1(
	 ->id int primary key,
	 ->name varchar(30)
	 ->);
  • 第二种方式:在创建表的最后,指定某列或某几列为主键索引
mysql->create table user2(
	 ->id int,
	 ->name varchar(30),
	 ->primary key(id)
	 ->);
  • 第三种方式:创建表以后再添加主键
mysql->create table user3(
	 ->id int,
	 ->name varchar(30)
	 ->);


mysql->alter table user3 add primary key(id);

主键索引的特点:

  1. 一个表中,最多有一个主键索引,当然可以是复合主键
  2. 主键索引的效率高(主键不可重复)
  3. 创建主键索引的列,它的值不能为null,且不能重复
  4. 主键索引的列基本上是int

二. 唯一键索引

  • 第一种方式:在创建表时,在某列后直接指定unique唯一属性
mysql->create table user4(
	 ->id int primary key,
	 name varchar(30) unique
	 );
  • 第二种方式:创建表时,在表的后面指定某列或某几列为unique
mysql->create table uesr5(
	 ->id int primary key,
	 ->name varchar(30),
	 ->unique(name)
	 );
  • 第三种方式:在创建表后,使用alter增添unique
mysql->creaete table user6(
	 ->id int primary key,
	 ->name varchar(30)
	 );

mysql->alter table user6 add unique(name);

整体和主键索引的创建差不多

唯一索引的特点:

  1. 一个表中,可以有多个唯一索引
  2. 查询效率高
  3. 如果在某一列建立唯一索引,必须保证这列不能有重复数据
  4. 如果一个唯一索引上指定not null ,等价与主键索引,但实际存储还是有所差异

三. 普通索引

  • 第一种方式:在表的定义最后,指定某列为索引indix(列)
mysql->create table user8(
	 ->id int primary key,
	 ->name varchar(30),
	 ->email varchar(30),
	 ->index(name)
	 );
  • 第二种方式:在创建完表以后指定某列为普通索引
mysql->create table user9(
	 ->id int primary key,
	 ->name varchar(20),
	 ->email varchar(30)
	 );

mysql->alter table user9 add index(name);
  • 第三种方式:创建一个索引名为idx_name的索引
mysql->create table user10(
	 ->id int primary key,
	 ->name varchar(30),
	 ->email varchar(30)
	 );

mysql->create index idx_name on user10(name);

普通索引的特点:

  1. 一个表可以有多个普通索引,普通索引在实际开发中用的比较多
  2. 如果某列需要创建索引,但是该列有重复的值,那么我们就应该使用普通索引

四. 全文索引

文章字段或者大量文字的字段进行检索时,会使用到全文索引。MySQL提供全文索引机制,默认的全文索引支持英文,不支持中文。如果对中文进行全文索引,可以使用sphinx的中文版(coreseek)

注意:

  1. MySQL 5.6 以前的版本,只有 MyISAM 存储引擎支持全文索引;
  2. MySQL 5.6 及以后的版本,MyISAM 和 InnoDB 存储引擎均支持全文索引;

语法:fulltext(列) 也可以fulltext(列,列)联合全文索引

  • 第一种方法:建表时在最后指定
create table articles(
	id int unsigned auto_increment not null primary key,
	title varchar(200),
	body text,
	fulltext (title,body)
)engine=MyISAM;
  • 第二种方法:建表后创建主键索引
create fulltext index articles_fulltext
    on articles(title,body);
  • 第三种方法:建表后使用alter add 添加全文索引
alter table articles
    add fulltext index articles_fulltext(title,body);

我们建立一个表

create table articles(
	id int unsigned auto_increment not null primary key,
	title varchar(200),
	body text,
	fulltext (title,body)
)engine=MyISAM;

INSERT INTO articles (title,body) VALUES
('MySQL Tutorial','DBMS stands for DataBase ...'),
('How To Use MySQL Well','After you went through a ...'),
('Optimizing MySQL','In this tutorial we will show ...'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL vs. YourSQL','In the following database comparison ...'),
('MySQL Security','When configured properly, MySQL ...');
  • 查询有没有database数据
    使用如下查询方式,没有使用全文索引
mysql> select * from articles where body like '%database%';
+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
+----+-------------------+------------------------------------------+

可以使用explain工具看一下,是否使用到索引
结尾\G可以去掉一些框架符号

mysql> explain select * from articles where body like '%database%'\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: articles
   partitions: NULL
         type: ALL
possible_keys: NULL
          key: NULL  <==keyNULL,代表没有使用索引
      key_len: NULL
          ref: NULL
         rows: 6
     filtered: 16.67
        Extra: Using where
1 row in set, 1 warning (0.03 sec)
  • 全文索引的使用
    语法:math(列) against (检索内容)
mysql> select * from articles where match(title,body) against ('database');
+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------+
|  5 | MySQL vs. YourSQL | In the following database comparison ... |
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |
+----+-------------------+------------------------------------------+

再次使用explain查询

mysql> explain select * from articles where match(title,body) against ('database') \G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: articles
   partitions: NULL
         type: fulltext
possible_keys: title
          key: title  <==此时key使用了title
      key_len: 0
          ref: const
         rows: 1
     filtered: 100.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

五. 查询索引

  • 第一种方法show keys from 表名

因为test1有主键索引和唯一键索引两个索引,所以显示出来两个

mysql> show keys from test1 \G;
*************************** 1. row ***************************
        Table: test1    <=表名
   Non_unique: 0        <=0表示唯一索引
     Key_name: PRIMARY  <=主键索引
 Seq_in_index: 1
  Column_name: id       <=索引在哪列
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: 
   Index_type: BTREE    <=B+树形式的索引
      Comment: 
Index_comment: 
*************************** 2. row ***************************
        Table: test1
   Non_unique: 1
     Key_name: body
 Seq_in_index: 1
  Column_name: body
    Collation: NULL
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: FULLTEXT
      Comment: 
Index_comment: 
2 rows in set (0.00 sec)
  • 第二种方式show index from 表名
    和第一种方式的结果相同

  • 第三种方式desc 表名 信息比较简略

mysql> desc test1;
                           索引
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
| body  | text    | YES  | MUL | NULL    |       |
+-------+---------+------+-----+---------+-------+

所以建议使用第一种方式或者第二种方式

六. 删除索引

  • 第一种方法:删除主键索引
alter table drop primary key;
  • 第二种方式:删除其他索引
alter table 表名 drop index 索引名;

索引名就是show keys from 表名种的key_name字段

  • 第三种方式:直接drop
drop index 索引名 on 表名

结束语

索引创建原则:

  • 比较频繁作为查询条件的字段应该创建索引
  • 唯一性太差的字段不适合单独创建索引,即使频繁作为查找条件
  • 更新非常频繁的字段不适合作创建索引
  • 不会出现在where子句中的字段不该创建索引

如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值