--终端 A mysql> create database myisam_test; --创建数据库 Query OK, 1 row affected (0.00 sec)
mysql> use myisam_test; Database changed
mysql>create table mtest(
-> id intprimary key,
-> name varchar(11) not null-> )engine=MyISAM; --使用 engine=MyISAM Query OK, 0 rows affected (0.01 sec)--终端 B [root@VM-0-3-centos mysql]# ls myisam_test/ -al --mysql 数据目录下 total 28 drwxr-x--- 2 mysql mysql 4096 Jun 13 13:33 . drwxr-x--x 13 mysql mysql 4096 Jun 13 13:32 .. -rw-r----- 1 mysql mysql 61 Jun 13 13:33 db.opt -rw-r----- 1 mysql mysql 8586 Jun 13 13:33 mtest.frm --表结构数据 -rw-r----- 1 mysql mysql 0 Jun 13 13:33 mtest.MYD --该表对应的数据,当前没有数据,所以是 0 -rw-r----- 1 mysql mysql 1024 Jun 13 13:33 mtest.MYI --该表对应的主键索引数据
可见,MyISAM 这种用户数据与索引数据分离的索引方案,叫做非聚簇索引
--终端 A mysql> create database innodb_test; --创建数据库 Query OK, 1 row affected (0.00 sec)
mysql> use innodb_test; Database changed
mysql>create table itest(
-> id intprimary key,
-> name varchar(11) not null-> )engine=InnoDB; --使用 engine=InnoDB Query OK, 0 rows affected (0.02 sec)--终端 B [root@VM-0-3-centos mysql]# ls innodb_test/ -al total 120 drwxr-x--- 2 mysql mysql 4096 Jun 13 13:39 . drwxr-x--x 14 mysql mysql 4096 Jun 13 13:38 .. -rw-r----- 1 mysql mysql 61 Jun 13 13:38 db.opt -rw-r----- 1 mysql mysql 8586 Jun 13 13:39 itest.frm --表结构数据 -rw-r----- 1 mysql mysql 98304 Jun 13 13:39 itest.ibd --该表对应的主键索引和用户数据,虽然现在一行数据没有,但是该表并不为 0,因为有主键索引数据
CREATE TABLE articles ( id INT UNSIGNED AUTO_INCREMENT NOT NULLPRIMARY 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 数据
如果使用如下查询方式,虽然查询出数据,但是没有使用到全文索引
可以用 explain 工具看一下,是否使用到索引
如何使用全文索引呢?
通过 explain 来分析这个 sql 语句
key 用到了 title
(5)查询索引
show keys from 表名\G
show index from 表名\G;
desc 表名;
(6)删除索引
第一种 - 删除主键索引:
alter table 表名 dropprimary key;
第二种 - 其他索引的删除:
索引名就是 show keys from 表名中的 Key_name 字段
alter table 表名 drop index 索引名;
第三种:
drop index 索引名 on 表名
drop index name on user8;
(7)索引创建原则
比较频繁作为查询条件的字段应该创建索引
唯一性太差的字段不适合单独创建索引,即使频繁作为查询条件
更新非常频繁的字段不适合作创建索引
不会出现在 where 子句中的字段不该创建索引
其他概念:
复合索引,用多列构建的索引
//添加复合索引 alter table test1 add index(name,email); //删除复合索引 alter table test4 drop index name;