上篇博客讲解了索引的底层结构
本篇介绍索引的使用
MySQL默认会按照主键索引进行排序
关键字:primary key
即使建表时没有指明主键,MySQL也会选择合适的属性作主键
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);
主键索引的特点:
mysql->create table user4(
->id int primary key,
name varchar(30) unique
);
mysql->create table uesr5(
->id int primary key,
->name varchar(30),
->unique(name)
);
mysql->creaete table user6(
->id int primary key,
->name varchar(30)
);
mysql->alter table user6 add unique(name);
整体和主键索引的创建差不多
唯一索引的特点:
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);
mysql->create table user10(
->id int primary key,
->name varchar(30),
->email varchar(30)
);
mysql->create index idx_name on user10(name);
普通索引的特点:
当文章字段或者大量文字的字段进行检索时,会使用到全文索引。MySQL提供全文索引机制,默认的全文索引支持英文,不支持中文
。如果对中文进行全文索引,可以使用sphinx的中文版(coreseek)
注意:
语法: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 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 ...');
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 <==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 (检索内容)
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 index 索引名 on 表名
索引创建原则:
如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。