索引

索引:提高数据库的性能,只要执行正确的create index,查询速度就可以提高成千上百倍,但是查询速度的提高是以插入、更新、删除的速度未代价的,这些操作,增加了大量的IO
常见的索引为:

  • 主键索引(primary key)
  • 唯一索引(unique)
  • 普通索引(index)
  • 全文索引(fulltext)

1.创建索引

1.1创建主键索引

  • 第一种方式:-- 在创建表的时候,直接在字段名后指定 primary key
create table user1(id int primary key, name varchar(30));
  • 第二种方式-- 在创建表的最后,指定某列或某几列为主键索引
create table user2(id int, name varchar(30), primary key(id));
  • 第三种方式–表都创建完了
create table user3(id int, name varchar(30));
alter table user3 add primary key(id);

特点

  • 一个表中,最多有一个主键,当然也可以有多个复合主键
  • 主键索引效率高(主键不可重复)
  • 创建主键索引的列,它的值不能为空,且不能重复
  • 主键索引的列基本都是int

1.2创建唯一键索引

  • 创建表时,在字段后指定
create table user4(id int primary key, name varchar(30) unique);
  • 创建表时,在表后指定
create table user5(id int primary key, name varchar(30), unique(name));
  • 表都创建完了
create table user6(id int primary key, name varchar(30)); alter table user6 add unique(name);

特点

  1. 一个表中可以有多个唯一索引
  2. 查询效率高
  3. 若在某一列建立唯一索引,必须保证这一列不能有重复数据
  4. 若一个唯一索引上指定not null,等价主键索引

1.3创建普通索引

  • 在表定义的最后,指定某列为索引
create table user8(id int primary key, 
     name varchar(20), 
     email varchar(30), 
     index(name) );
  • 创建完表后,指定某列为普通索引
create table user9(id int primary key, 
     name varchar(20), 
     email varchar(30)); 
alter table user9 add index(name);
  • 创建一个索引名为 索引名 的索引
create table user10(id int primary key, 
     name varchar(20), 
     email varchar(30)); 
create index idx_name on user10(name);

特点

  1. 一个表中可以有多个普通索引
  2. 若某列需要创建索引,电脑该列有重复的值,那我们就可以使用普通索引

1.4全文索引的创建
当对文章字段或有大量文字的字段进行检索时,会使用到全文索引。要使用全文索引必须要求表的索引时MYISAM,畏怯默认全文索引支持英文,不支持中文
1.4.1创建全文索引

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 ...');

查询所有body中有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 ... |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)

我们用explain工具查看是否用到索引

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.00 sec)

怎样使用全文索引

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 ...             |
+----+-------------------+------------------------------------------+
2 rows in set (0.00 sec)

我们再用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
      key_len: 0
          ref: const
         rows: 1
     filtered: 100.00
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

2.查询索引

  • show keys from 表名;
mysql> show keys from goods\G;
*************************** 1. row ***************************
        Table: goods  --表名
   Non_unique: 0--0表示唯一索引
     Key_name: PRIMARY--主键索引
 Seq_in_index: 1
  Column_name: goods_id--索引所在列
    Collation: A
  Cardinality: 0
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE索引底层以二叉树实现
      Comment:
Index_comment:
  • show index from 表名;
  • desc 表名; (信息比较简略)

3.删除索引

  • 删除主键索引: alter table 表名 drop primary key;
  • 其他索引的删除: alter table 表名 drop index 索引名; 索引名就是show keys from 表名中的 Key_name 字段
mysql> alter table user10 drop index idx_name;
  • drop index 索引名 on 表名
mysql> drop index name on user8;

索引创建原则:

  1. 比较频繁作为查询条件的字段应该创建主键索引
  2. 唯一性太差的字段不适合单独创建索引
  3. 更新非常频繁的字段不适合作创建索引
  4. 不会出现在where子句中的字段不该创建索引
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值