【MySQL】索引的增删查

上篇博客讲解了索引的底层结构

本篇介绍索引的使用

文章目录

  • [一. 主键索引](#一. 主键索引)
  • [二. 唯一键索引](#二. 唯一键索引)
  • [三. 普通索引](#三. 普通索引)
  • [四. 全文索引](#四. 全文索引)
  • [五. 查询索引](#五. 查询索引)
  • [六. 删除索引](#六. 删除索引)
  • 结束语

一. 主键索引

MySQL默认会按照主键索引进行排序

关键字:primary key

即使建表时没有指明主键,MySQL也会选择合适的属性作主键

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


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

主键索引的特点:

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

二. 唯一键索引

  • 第一种方式:在创建表时,在某列后直接指定unique唯一属性
sql 复制代码
mysql->create table user4(
	 ->id int primary key,
	 name varchar(30) unique
	 );
  • 第二种方式:创建表时,在表的后面指定某列或某几列为unique
sql 复制代码
mysql->create table uesr5(
	 ->id int primary key,
	 ->name varchar(30),
	 ->unique(name)
	 );
  • 第三种方式:在创建表后,使用alter增添unique
sql 复制代码
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(列)
sql 复制代码
mysql->create table user8(
	 ->id int primary key,
	 ->name varchar(30),
	 ->email varchar(30),
	 ->index(name)
	 );
  • 第二种方式:在创建完表以后指定某列为普通索引
sql 复制代码
mysql->create table user9(
	 ->id int primary key,
	 ->name varchar(20),
	 ->email varchar(30)
	 );

mysql->alter table user9 add index(name);
  • 第三种方式:创建一个索引名为idx_name的索引
sql 复制代码
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(列,列)联合全文索引

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

我们建立一个表

sql 复制代码
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数据
    使用如下查询方式,没有使用全文索引
sql 复制代码
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可以去掉一些框架符号

sql 复制代码
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  <==key为NULL,代表没有使用索引
      key_len: NULL
          ref: NULL
         rows: 6
     filtered: 16.67
        Extra: Using where
1 row in set, 1 warning (0.03 sec)
  • 全文索引的使用
    语法:math(列) against (检索内容)
sql 复制代码
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查询

sql 复制代码
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有主键索引和唯一键索引两个索引,所以显示出来两个

sql 复制代码
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 表名 信息比较简略

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

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

六. 删除索引

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

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

  • 第三种方式:直接drop
sql 复制代码
drop index 索引名 on 表名

结束语

索引创建原则:

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

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

相关推荐
子非衣8 分钟前
MySQL修改JSON格式数据示例
android·mysql·json
qwy7152292581639 分钟前
13-R数据重塑
服务器·数据库·r语言
Bio Coder11 分钟前
R语言安装生物信息数据库包
开发语言·数据库·r语言
钊兵1 小时前
数据库驱动免费下载(Oracle、Mysql、达梦、Postgresql)
数据库·mysql·postgresql·oracle·达梦·驱动
weixin_425878232 小时前
Redis复制性能优化利器:深入解析replica-lazy-flush参数
数据库·redis·性能优化
左灯右行的爱情2 小时前
Redis数据结构总结-listPack
数据结构·数据库·redis
隔壁老王1563 小时前
mysql实时同步到es
数据库·mysql·elasticsearch
openinstall全渠道统计3 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
想要打 Acm 的小周同学呀3 小时前
Redis三剑客解决方案
数据库·redis·缓存