1、表的主键、外键必须有索引
2、数据量超过300行的表应该有索引
3、经常与其他表进行连接的表,在连接字段上应该建立索引
4、唯一性太差的字段不适合建立索引
5、更新太频繁地字段不适合创建索引
6、经常出现在 Where子句中的字段,特别是大表的字段,应该建立索引
7、索引应该建在选择性高的字段上
8、索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引
1、普通索引
最基本的索引类型,没有唯一性之类的限制
创建普通索引的方式:
直接创建:
mysql> create index index_id on info(id); #info是表名,id是表中的字段
mysql> show index from info;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
| info | 1 | index_id | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
修改表结构的方式添加索引:
mysql> use student
mysql> alter table info add index index_name(name);
mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
| info | 1 | index_id | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
| info | 1 | index_name | 1 | name | A | 5 | NULL | NULL | | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
创建表时创建:
create table hobby(id int(2) not null primary key, con varchar(20), index index_id (id));
mysql> show index from hobby;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| hobby | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| hobby | 1 | index_id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
2、唯一性索引
与普通索引的区别是索引列的所有值只能出现一次,即必须唯一
创建唯一索引的方式
直接创建
mysql> create unique index index_id on zf(id); #zf表名,id表中字段
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from zf;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| zf | 0 | index_id | 1 | id | A | 2 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
创建表时创建
mysql> create table text(id int(2) not null,name varchar(20),unique index index_id(id));
Query OK, 0 rows affected (0.01 sec)
mysql> show index from text;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| text | 0 | index_id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
修改表的方式添加索引
mysql> alter table text add unique index index_name(name);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from text;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| text | 0 | index_id | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| text | 0 | index_name | 1 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
3、主键索引
是一种特殊的唯一索引,指定为primary key,一个表只能有一个主键,与唯一索引的区别是不允许有空值(非空且唯一)
创建主键索引的方式
创建表时创建
mysql> create table test1(id int(2) not null primary key,name char(20));
Query OK, 0 rows affected (0.01 sec)\
#或者#
mysql> create table test2(id int(2) not null,name char(20),primary key(id));
Query OK, 0 rows affected (0.01 sec)
修改表时创建
mysql> create table test3(id int(2) not null,name char(20));
mysql> alter table test3 add primary key(id);
4、组合索引(单列索引与多列索引)
可以是单列上创建的索引,也可以是在多列上创建的索引,遵循最左原则,从左往右依次执行。
mysql> create table test4(id int(2) not null,name char(20),index index_test4(id,name));
Query OK, 0 rows affected (0.02 sec)
mysql> show index from test4;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test4 | 1 | index_test4 | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| test4 | 1 | index_test4 | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
5、全文索引
创建表时创建全文索引(必须为字符型字段)
mysql> create table test5(id int(2) not null,name char(20),fulltext(name));
Query OK, 0 rows affected (0.04 sec)
mysql> show index from test5;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test5 | 1 | name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
在已有的表添加全文索引
mysql> create fulltext index index_name on test4(name);
Query OK, 0 rows affected, 1 warning (0.05 sec)
mysql> show index from test4;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test4 | 1 | index_test4 | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| test4 | 1 | index_test4 | 2 | name | A | 0 | NULL | NULL | YES | BTREE | | |
| test4 | 1 | index_name | 1 | name | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
修改表的方式添加全文索引
mysql> alter table info add fulltext index_city(address);
mysql> show index from info;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| info | 0 | PRIMARY | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
| info | 1 | index_id | 1 | id | A | 5 | NULL | NULL | | BTREE | | |
| info | 1 | index_name | 1 | name | A | 5 | NULL | NULL | | BTREE | | |
| info | 1 | index_city | 1 | address | NULL | 5 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
4 rows in set (0.00 sec)
方法一:drop
mysql> drop index index_city on info;
#drop index 索引名 on 表名;
方法二:alter
mysql> alter table info drop index index_name;
#alter table 表名 drop index 索引名
MySQL事务默认是自动提交的,当SQL语句提交时事务便自动提交。
事务控制语句:
begin #事务的开始
commit #提交事务
rollback #回滚
savepoint 存档点名称 #存档点
release savepoint 存档点名称 #删除存档点
rollback to 存档点名称 #回滚到某个存档点
set transaction #设置事务
事务处理命令控制事务
begin:开始一个事务
commit:提交一个事务
rollback:回滚一个事务
使用set命令进行控制
set autocommit=0:禁止自动提交
set autocommit=1:开启自动提交(默认)
创建的数据表存储引擎必须是innodb,才支持事务(5.7版本默认就是innodb)。
mysql> insert into zf values(5,'zhaoxue','无锡');
Query OK, 1 row affected (0.00 sec)
mysql> savepoint a; #建立存档点a
Query OK, 0 rows affected (0.00 sec)
mysql> select * from zf;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
| 4 | yangyang | 杭州 |
| 5 | zhaoxue | 无锡 |
+----+----------+---------+
4 rows in set (0.01 sec)
mysql> insert into zf values(6,'zhuling','扬州');
Query OK, 1 row affected (0.00 sec)
mysql> savepoint b; #建立存档点b
Query OK, 0 rows affected (0.00 sec)
mysql> select * from zf;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
| 4 | yangyang | 杭州 |
| 5 | zhaoxue | 无锡 |
| 6 | zhuling | 扬州 |
+----+----------+---------+
5 rows in set (0.00 sec)
mysql> insert into zf values(7,'zhuli','南京');
Query OK, 1 row affected (0.00 sec)
mysql> rollback to b; #回滚到存档点b的状态
Query OK, 0 rows affected (0.00 sec)
mysql> select * from zf;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
| 4 | yangyang | 杭州 |
| 5 | zhaoxue | 无锡 |
| 6 | zhuling | 扬州 |
+----+----------+---------+
5 rows in set (0.00 sec)
mysql> rollback to a; #回滚到存档点a的状态
Query OK, 0 rows affected (0.00 sec)
mysql> select * from zf;
+----+----------+---------+
| id | name | address |
+----+----------+---------+
| 1 | lisi | 苏州 |
| 3 | wangwu | 北京 |
| 4 | yangyang | 杭州 |
| 5 | zhaoxue | 无锡 |
+----+----------+---------+
4 rows in set (0.00 sec)
mysql> rollback to b;
ERROR 1305 (42000): SAVEPOINT b does not exist
#存档点b已经不存在,因为现在在a的状态
mysql> commit;
无法向后回滚,只能向前滚
两种引擎:
MyISAM、 InnoDB(innodb支持事务,myisam不支持事务)
MySQL存储引擎是 MySQL数据库服务器中的组件,负责为数据库执行实际的数据I/O操作
使用特殊存储引擎的主要优点之一在于:
仅需提供特殊应用所需的特性;
数据库中的系统开销较小;
具有更有效和更高的数据库性能。
MySQL系统中,存储引擎处于文件系统之上,在数据保存到数据文件之前会传输到存储引擎,之后按照各个存储引擎的存储格式进行存储
特点:
MyISAM不支持事务,也不支持外键
访问速度快
对事务完整性没有要求
MyISAM在磁盘.上存储成三个文件
●.frm文件存储表定义
●数据文件的扩展名为.MYD (MYData)
●索引文件的扩展名是.MYI (MYIndex)
表级锁定形式,数据在更新时锁定整个表
数据库在读写过程中相互阻塞 会在数据写入的过程阻塞用户数据的读取 也会在数据读取的过程中阻塞用户的数据写入
数据单独写入或读取,速度过程较快且占用资源相对
MyIAM支持的存储格式
●静态表
●动态表
●压缩表
适用场景:
特点:
适用场景:
业务需要事务的支持
行级锁定对高并发有很好的适应能力,但需确保查询是通过索引来完成
业务数据更新较为频繁的场景
如:论坛、微博等
业务数据一致性要求较高
如:银行业务
硬件设备内存较大(因为事务都先放内存),利用innodb较好的缓存能力来提高内存利用率,减少磁盘IO的压力
需要考虑每个存储引擎提供的核心功能及应用场景
支持的字段和数据类型
所有引擎都支持通用的数据类型,但不是所有的引擎都支持其它的字段类型,如二进制对象
锁定类型:不同的存储引擎支持下同级别的锁定
表锁定
行锁定
索引的支持
建立索引在搜索和恢复数据库中的数据时能显著提高性能
不同的存储弓|擎提供不同的制作索引|的技术
有些存储引擎根本不支持索引
事务处理的支持
提高在向表中更新和插入信息期间的可靠性
可根据企业业务是否要支持事务选择存储引擎
mysql> alter table test engine=myisam;
# alter table 表名 engine=引擎;
查看修改结果
方法二:之后创建的所有数据库都会是这个引擎
修改my.cnf,指定默认存储引擎并重启服务
在[mysqld]下面添加default-storage-engine=InnoDB
方法三:
创建表时指定存储引擎
create table 表名(字段)engine=引擎
mysql> create table test6(id int(2),name char(20)) engine=myisam;
查看结果
方法四(5.7版本不支持):
Mysql_convert_table_format转化存储引擎
Mysql_convert_table_format --user=root --password=密码
–sock=/tmp/mysql.sock-engine=引擎 库名 表名