1、索引
(1)索引的概念
- 索引是一个排序的列表,在这个列表中存储着索引的值和包含这个值的数据所在行的物理地址(类似于C语言的链表通过指针指向数据记录的内存地址)
- 使用索引后可以不用扫描全表来定位某行的数据,而是先通过索引表找到该行数据对应的物理地址,然后访问相应的数据,因此能加快数据库的查询速度。
- 索引就好比是一本书的目录,可以根据目录中的页码快速找到所需的内容。
- 索引是表中一列或者若干列值排序的方法。
- 建立索引的目的是加快对表中记录的查找或排序。
(2)索引的作用
- 设置了合适的索引之后,数据库利用各种快速定位技术,能够大大加快查询速度,这是创建索引的最主要的原因。
- 当表很大或查询涉及到多个表时,使用索引可以成千上万倍地提高查询速度。
- 可以降低数据库的I/O成本,并且索引还可以降低数据库的排序成本。
- 通过创建唯一性索引,可以保证数据表中每一行数据的唯一性。
- 可以加快表与表之间的连接。
- 在使用分组和排序时,可大大减少分组和排序的时间。
- 建立索引在搜索和恢复数据库中的数据时能显著提高性能
(3)索引的副作用
- 索引需要占用额外的磁盘空间 。
- 对于MyISAM引擎而言,索引文件和数据文件是分离的,索引文件用于保存数据记录的地址。
- 而InnoDB引擎的表数据文件本身就是索引文件
- 更新一个包含索引的表,需要比更新一个没有索引的表花费更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜素的列(以及表)上面创建索引
(4)创建索引的原则依据
索引虽可以提升数据库查询的速度,但并不是任何情况下都适合创建索引。因为索引本身会消耗系统资源,在有索引的情况下,数据库会先进行索引查询,然后定位到具体的数据行,如果索引使用不当,反而会增加数据库的负担。
- 表的主键、外键必须有索引。因为主键具有唯一性,外键关联的是主表的主键,查询时可以快速定位。
- 记录数超过300行的表应该有索引。如果没有索引,每次查询都需要把表遍历一遍,会严重影响数据库的性能。
- 经常与其他表进行连接的表,在连接字段上应该建立索引。
- 唯一性太差的字段不适合建立索引。
- 更新太频繁地字段不适合创建索引。
- 经常出现在where子句中的字段,特别是大表的字段,应该建立索引。
- 在经常进行 GROUP BY、ORDER BY 的字段上建立索引;
- 索引应该建在选择性高(冗余度低)的字段上。
- 索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引。
(5)索引的分类和创建
1)普通索引
最基本的索引类型,没有唯一性之类的限制
直接创建索引
create index 索引名 on 表名(列名[ ( length) ]);
- (列名(length) ): length是可选项。如果忽略length的值,则使用整个列的值作为索引。如果指定使用列前的length个字符来创建索引,这样有利于减小索引文件的大小。在不损失精确性的情况下,长度越短越好。
- 索引名建议以"index”结尾。
mysql> desc zx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> create index name_index on zx(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc zx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| sex | char(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> show create table zx\G
*************************** 1. row ***************************
Table: zx
Create Table: CREATE TABLE "zx" (
"id" int(11) NOT NULL,
"name" varchar(20) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
PRIMARY KEY ("id"),
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
修改表方式创建
alter table 表名 add index 索引名 (列名);
mysql> desc zx1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table zx1 add index name_index (name);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table zx1\G
*************************** 1. row ***************************
Table: zx1
Create Table: CREATE TABLE "zx1" (
"id" int(11) NOT NULL,
"name" varchar(20) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
PRIMARY KEY ("id"),
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
创建表的时候指定索引
create table 表名 ( 字段1 数据类型,字段2 数据类型,[...],index 索引名 (列名));
mysql> create table scj (id int, name varchar(20), age int, sex char(2), index name_index (name));
Query OK, 0 rows affected (0.01 sec)
mysql> show create table scj\G
*************************** 1. row ***************************
Table: scj
Create Table: CREATE TABLE "scj" (
"id" int(11) DEFAULT NULL,
"name" varchar(20) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
KEY "name_index" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
2)唯一索引
与普通索引类似,但区别是唯一索引列的每个值都唯一。唯一索引允许有空值(注意和主键不同)。如果是用组合索引创建,则列值的组合必须唯一。添加唯一键将自动创建唯一索引。
直接创建唯一索引
create unique index 索引名 on 表名(列名);
mysql> desc scj;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> create unique index id_name on scj(id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc scj;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
修改表方式创建
alter table 表名 add unique 索引名 (列名);
mysql> desc jlh;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table jlh add unique id_index (id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc jlh;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(20) | YES | MUL | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
创建表的时候指定
create table 表名 (字段1 数据类型,字段2 数据类型,[...],unique 索引名 (列名));
mysql> create table jlh1 (id int, name varchar(20), age int, sex char(2), unique name_index (name));
Query OK, 0 rows affected (0.00 sec)
mysql> desc jlh1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
3)主键索引
是一种特殊的唯一索引,必须指定为==‘‘PRIMARY KEY’’==。一个表只能有一个主键,不允许有空值。 添加主键将自动创建主键索引。
创建表的时候指定
create table 表名 ([...],primary key (列名));
mysql> create table zyr (id int, name varchar(20), age int, sex char(2), primary key(id));
Query OK, 0 rows affected (0.01 sec)
mysql> desc zyr;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> create table zyr1 (id int, name varchar(20), age int, sex char(2), primary key(id,name));
Query OK, 0 rows affected (0.00 sec)
mysql> desc zyr1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
修改表方式创建
alter table 表名 add primary key (列名);
mysql> create table ctt (id int, name varchar(20), age int, sex char(2));
Query OK, 0 rows affected (0.01 sec)
mysql> desc ctt;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table ctt add primary key (id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc ctt;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
4)组合索引
(单列索引与多列索引):可以是单列上创建的索引,也可以是在多列上创建的索引。需要满足最左原则,因为select语句的where条件是依次从左往右执行的,所以在使用select语句查询时,where条件使用的字段顺序必须和组合索引中的排序一致,否则索引将不会生效。
直接创建索引
create index 索引名 on 表名(字段1, 字段2, ....);
select 字段列表 from 表名 where 字段1=XX and 字段2=XX .... ;
#查询语句使用 and 做逻辑运算符时,字段顺序要与创建多列索引的字段顺序一致(要满足最左原则)
mysql> desc zx;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(2) | YES | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> create index zx_index on zx (sex, age, name);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table zx;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| zx | CREATE TABLE "zx" (
"id" int(11) NOT NULL,
"name" varchar(20) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) NOT NULL,
PRIMARY KEY ("id"),
KEY "zx_index" ("sex","age","name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from zx;
+----+------+------+-----+
| id | name | sex | age |
+----+------+------+-----+
| 3 | ctt | 女 | 23 |
| 2 | zx | 男 | 21 |
| 1 | tc | 男 | 22 |
| 4 | jlh | 男 | 24 |
+----+------+------+-----+
4 rows in set (0.00 sec)
mysql> select * from zx where sex='男' and age=22 and name='tc';
+----+------+------+-----+
| id | name | sex | age |
+----+------+------+-----+
| 1 | tc | 男 | 22 |
+----+------+------+-----+
1 row in set (0.00 sec)
mysql>
修改表方式创建
alter table 表名 add index 索引名(字段1, 字段2, ....);
select 字段列表 from 表名 where 字段1=XX and 字段2=XX .... ;
#查询语句使用 and 做逻辑运算符时,字段顺序要与创建多列索引的字段顺序一致(要满足最左原则)
mysql> select * from zx1;
+-----+-----------+------+------+
| id1 | name1 | sex | age |
+-----+-----------+------+------+
| 1 | tc | 男 | 22 |
| 2 | 曹彤彤 | 女 | 22 |
| 3 | 朱翔 | 男 | 21 |
| 4 | 蒋铄瀚 | 男 | 23 |
+-----+-----------+------+------+
4 rows in set (0.00 sec)
mysql> alter table zx1 add index zx_index (sex, name1, age);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table zx1;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| zx1 | CREATE TABLE "zx1" (
"id1" int(11) NOT NULL,
"name1" varchar(20) NOT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
UNIQUE KEY "id1" ("id1"),
UNIQUE KEY "name1" ("name1"),
KEY "zx_index" ("sex","name1","age")
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from zx1 where sex='男' and name1='tc' and age=22;
+-----+-------+------+------+
| id1 | name1 | sex | age |
+-----+-------+------+------+
| 1 | tc | 男 | 22 |
+-----+-------+------+------+
1 row in set (0.00 sec)
mysql>
5)全文索引
(fulltext):适合在进行模糊查询的时候使用,可用于在一篇文章中检索文本信息。在 MySQL5.6版本以前FULLTEXT索引仅可用于MyISAM引擎,在5.6版本之后innodb引擎也支持fulltext索引。全文索引可以在char、varchar或者text类型的列上创建。
直接创建索引
create fulltext index 索引名 on 表名 (列名);
mysql> select * from zx;
+----+------+------+------+
| id | name | sex | age |
+----+------+------+------+
| 2 | ctt | 女 | 23 |
| 4 | zyr | 男 | 20 |
| 3 | jlh | 男 | 21 |
| 1 | zx | 男 | 22 |
+----+------+------+------+
4 rows in set (0.00 sec)
mysql> create fulltext index zx_name on zx(age);
ERROR 1283 (HY000): Column 'age' cannot be part of FULLTEXT index
mysql> create fulltext index zx_name on zx(sex);
Query OK, 0 rows affected, 1 warning (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show create table zx\G
*************************** 1. row ***************************
Table: zx
Create Table: CREATE TABLE "zx" (
"id" int(11) NOT NULL,
"name" varchar(20) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
PRIMARY KEY ("id"),
KEY "name_index" ("name"),
KEY "zx_index" ("sex","age","name"),
FULLTEXT KEY "zx_name" ("sex")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
修改表方式创建
alter table 表名 add fulltext 索引名 (列名);
mysql> select * from zx2;
+----+------+------+------+
| id | name | sex | age |
+----+------+------+------+
| 1 | zx | 男 | 22 |
| 2 | ctt | 女 | 23 |
+----+------+------+------+
2 rows in set (0.00 sec)
mysql> alter table zx2 add fulltext zx_name (name);
Query OK, 0 rows affected, 1 warning (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 1
mysql> show create table zx2\G
*************************** 1. row ***************************
Table: zx2
Create Table: CREATE TABLE "zx2" (
"id" int(11) NOT NULL,
"name" varchar(20) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
FULLTEXT KEY "zx_name" ("name")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
创建表的时候指定索引
create table 表名 (字段1 数据类型,[...],fulltext 索引名 (列名));
#数据类型可以为char、varchar或者text
mysql> create table zx123 (id int, name varchar(20), sex char(2), age int, remark text, fulltext remark_name (remark));
Query OK, 0 rows affected (0.02 sec)
mysql> show create table zx123\G
*************************** 1. row ***************************
Table: zx123
Create Table: CREATE TABLE "zx123" (
"id" int(11) DEFAULT NULL,
"name" varchar(20) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
"remark" text,
FULLTEXT KEY "remark_name" ("remark")
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
中文全文索引查询
vim /etcmy.cfn
ngram_token_size=2 #指定查询的单词的最小字数
syetemctl restart mysqld
create table xy106 (id int, name varchar(20), age int, sex char(2), remark text, fulltext (remark) with parser ngram); #创建表
insert into xy106 values (1, 'zhang', 25, '男', '足球,篮球,台球');
insert into xy106 values (2, 'san', 26, '男', '吃饭,足球,乒乓球');
insert into xy106 values (3, 'li', 26, '男', '篮球,羽毛球,乒乓球');
insert into xy106 values (4, 'si', 26, '男', '篮球,乒乓球,足球');
[root@zx1 ~]# vim /etc/my.cnf
ngram_token_size=2
[root@zx1 ~]# systemctl restart mysqld.service
[root@zx1 ~]# mysql -u root -pabc123
mysql> create table xy106 (id int, name varchar(20), age int, sex char(2), remark text, fulltext (remark) with parser ngram);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into xy106 values (1, 'zhang', 25, '男', '足球,篮球,台球');
Query OK, 1 row affected (0.00 sec)
mysql> insert into xy106 values (2, 'san', 26, '男', '吃饭,足球,乒乓球');
Query OK, 1 row affected (0.00 sec)
mysql> insert into xy106 values (3, 'li', 26, '男', '篮球,羽毛球,乒乓球');
Query OK, 1 row affected (0.00 sec)
mysql> insert into xy106 values (4, 'si', 26, '男', '篮球,乒乓球,足球');
Query OK, 1 row affected (0.00 sec)
mysql> select * from xy106;
+------+-------+------+------+--------------------------------+
| id | name | age | sex | remark |
+------+-------+------+------+--------------------------------+
| 2 | san | 26 | 男 | 吃饭,足球,乒乓球 |
| 3 | li | 26 | 男 | 篮球,羽毛球,乒乓球 |
| 4 | si | 26 | 男 | 篮球,乒乓球,足球 |
| 1 | zhang | 25 | 男 | 足球,篮球,台球 |
+------+-------+------+------+--------------------------------+
4 rows in set (0.00 sec)
mysql> show create table xy106\G
*************************** 1. row ***************************
Table: xy106
Create Table: CREATE TABLE "xy106" (
"id" int(11) DEFAULT NULL,
"name" varchar(20) DEFAULT NULL,
"age" int(11) DEFAULT NULL,
"sex" char(2) DEFAULT NULL,
"remark" text,
FULLTEXT KEY "remark" ("remark") /*!50100 WITH PARSER "ngram" */
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql>
select 字段列表 from 表名 where match(字段) against('单词'); #默认使用自然语言模式
select 字段列表 from 表名 where match(字段) against('+单词1 -单词2' IN BOOLEAN MODE); #使用BOOLEAN模式,必须包含"单词1",且不能包含"单词2"
select 字段列表 from 表名 where match(字段) against('+单词1 +单词2' IN BOOLEAN MODE); #使用BOOLEAN模式,必须同时包含"单词1"和"单词2"
select 字段列表 from 表名 where match(字段) against('单词1 单词2' IN BOOLEAN MODE); #使用BOOLEAN模式,要么包含"单词1",要么包含"单词2"
mysql> select * from xy106;
+------+-------+------+------+--------------------------------+
| id | name | age | sex | remark |
+------+-------+------+------+--------------------------------+
| 2 | san | 26 | 男 | 吃饭,足球,乒乓球 |
| 3 | li | 26 | 男 | 篮球,羽毛球,乒乓球 |
| 4 | si | 26 | 男 | 篮球,乒乓球,足球 |
| 1 | zhang | 25 | 男 | 足球,篮球,台球 |
+------+-------+------+------+--------------------------------+
4 rows in set (0.00 sec)
mysql> select * from xy106 where match(remark) against('篮球');
+------+-------+------+------+--------------------------------+
| id | name | age | sex | remark |
+------+-------+------+------+--------------------------------+
| 3 | li | 26 | 男 | 篮球,羽毛球,乒乓球 |
| 4 | si | 26 | 男 | 篮球,乒乓球,足球 |
| 1 | zhang | 25 | 男 | 足球,篮球,台球 |
+------+-------+------+------+--------------------------------+
3 rows in set (0.00 sec)
mysql> select * from xy106 where match(remark) against('+篮球 -乒乓球' in boolean mode);
+------+-------+------+------+--------------------------+
| id | name | age | sex | remark |
+------+-------+------+------+--------------------------+
| 1 | zhang | 25 | 男 | 足球,篮球,台球 |
+------+-------+------+------+--------------------------+
1 row in set (0.00 sec)
mysql> select * from xy106 where match(remark) against('+篮球 +乒乓球' in boolean mode);
+------+------+------+------+--------------------------------+
| id | name | age | sex | remark |
+------+------+------+------+--------------------------------+
| 3 | li | 26 | 男 | 篮球,羽毛球,乒乓球 |
| 4 | si | 26 | 男 | 篮球,乒乓球,足球 |
+------+------+------+------+--------------------------------+
2 rows in set (0.00 sec)
mysql> select * from xy106 where match(remark) against('乒乓球 吃饭' in boolean mode);
+------+------+------+------+--------------------------------+
| id | name | age | sex | remark |
+------+------+------+------+--------------------------------+
| 2 | san | 26 | 男 | 吃饭,足球,乒乓球 |
| 3 | li | 26 | 男 | 篮球,羽毛球,乒乓球 |
| 4 | si | 26 | 男 | 篮球,乒乓球,足球 |
+------+------+------+------+--------------------------------+
3 rows in set (0.00 sec)
mysql>
(6)查看索引
show create table 表名;
show index from 表名;
show index from 表名\G; #纵向显示索引信息
show keys from 表名;
show keys from 表名\G;
(7)删除索引
1)直接删除索引
drop index 索引名 on 表名;
2)修改表方式删除索引
alter table 表名 drop index 索引名;
3)删除主键索引
alter table 表名 drop primary key;
(8)select查询语句执行速度慢如何优化?
1、升级 CPU 内存 硬盘 硬件性能
2、对 MySQL 配置进行优化
3、对查询语句的结构进行优化,比如将嵌套子查询优化成表连接查询;或连接表时,可以先用where条件对表进行过滤,然后做表连接
4、进行索引优化:先使用 explain 分析 select 语句,判断这个查询语句是否正确的使用了索引;再根据查询语句中的 where 条件字段建立相应的单列索引或者多列组合索引(多列组合索引要满足最左原则)