理论+实验 详解MySQL索引,事务与存储引擎

目录

  • 一 索引介绍
    • 1.1 索引的概念
    • 1.2 索引的作用
    • 1.3 索引的分类
    • 1.4 创建索引的原则依据
    • 1.5 查看索引的方法
    • 1.6 删除索引的方法
  • 二 事务介绍
    • 2.1 事务的概念
    • 2.2 事务的ACID特点
    • 2.3 事务控制语句
    • 2.4 示例
  • 三 存储引擎介绍
    • 3.1 存储引擎概念介绍
    • 3.2 MyISAM的介绍
    • 3.3 MyISAM适用的生产场景举例
    • 3.4 lnnoDB特点介绍
    • 3.5 lnnoDB适用生产场景分析
    • 3.6 企业选择存储引擎依据
    • 3.7 修改存储引擎

一 索引介绍

1.1 索引的概念

理论+实验 详解MySQL索引,事务与存储引擎_第1张图片

1.2 索引的作用

理论+实验 详解MySQL索引,事务与存储引擎_第2张图片

1.3 索引的分类

准备工作

[root@localhost ~]# mysql -uroot -p    ##登录数据库
Enter password: 
mysql> show databases;    ##查看数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> create database cwj;    ##创建数据库cwj
Query OK, 1 row affected (0.00 sec)

mysql> show databases;    ##查看是否成功创建
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
| cwj                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use cwj;    ##进入cwj
Database changed
mysql> create table info (    ##创建info表
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> address varchar(50) default 'nanjing',
    -> age int(3) not null);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into info (name,address,age) values    ##info表中插入数据    ('zhangsan','beijing',20),('lisi','shanghai,2Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from info;    ##查看内容
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
+----+----------+----------+-----+
2 rows in set (0.00 sec)

mysql> desc info;    ##查看表结构
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(4)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| address | varchar(50) | YES  |     | nanjing |                |
| age     | int(3)      | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

理论+实验 详解MySQL索引,事务与存储引擎_第3张图片
创建普通索引
第一种方法–用create创建普通索引

mysql> create index index_age on info (age);    ##针对info表中,age创建索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
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         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

mysql> drop index index_age on info;    ##删除索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

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         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

第二种方法–用alter方法创建普通索引


mysql> alter table info add index index_age (age);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

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         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          1 | index_age |            1 | age         | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

第三种方法–创建表的时候创建普通索引

mysql>  create table user (
    -> id int(4) not null primary key auto_increment,
    -> name varchar(10) not null,
    -> score decimal not null,
    -> hobby int(2) not null default '1',
    -> index index_scrore (score));
Query OK, 0 rows affected (0.01 sec)
mysql> show index from user;
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY      |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_scrore |            1 | score       | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

理论+实验 详解MySQL索引,事务与存储引擎_第4张图片
创建唯一索引

mysql> drop index index_age on info;    ##删除之前的索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> create unique index unique_name on info (name);    ##创建唯一索引
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
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         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| info  |          0 | unique_name |            1 | name        | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql> drop index unique_name on info;    ##删除唯一索引
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

说明:还有两种方法和上面创建普通索引方法一样
理论+实验 详解MySQL索引,事务与存储引擎_第5张图片
主键索引

mysql> create table user2 (    ##创建主键索引
    -> id int(4) not null  auto_increment,
    -> name varchar(10) not null,
    -> age int(3) not null,
    -> primary key (`id`));
Query OK, 0 rows affected (0.00 sec)

mysql> show index from user2;    ##查看主键索引
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user2 |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

理论+实验 详解MySQL索引,事务与存储引擎_第6张图片
组合索引

mysql> create table user3 (    ##创建组合索引
    -> name varchar(10) not null,
    -> age int(3) not null,
    -> sex tinyint(1) not null,
    -> index user3(name,age,sex));
Query OK, 0 rows affected (0.00 sec)

mysql> show keys from user3;    ##查看
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user3 |          1 | user3    |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| user3 |          1 | user3    |            2 | age         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| user3 |          1 | user3    |            3 | sex         | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

理论+实验 详解MySQL索引,事务与存储引擎_第7张图片
全文索引

mysql> create table user4 (    ##创建全文索引
    -> id int(11) not null auto_increment,
    -> tile char(255) character set utf8 collate utf8_general_ci not null,
    -> content text character set utf8 collate utf8_general_ci not null,
    -> primary key (`id`),
    -> fulltext (content));
Query OK, 0 rows affected (0.03 sec)
mysql> show keys from user4;    ##查看
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user4 |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| user4 |          1 | content  |            1 | content     | NULL      |           0 |     NULL | NULL   |      | FULLTEXT   |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

1.4 创建索引的原则依据

理论+实验 详解MySQL索引,事务与存储引擎_第8张图片
理论+实验 详解MySQL索引,事务与存储引擎_第9张图片

1.5 查看索引的方法

理论+实验 详解MySQL索引,事务与存储引擎_第10张图片

1.6 删除索引的方法

理论+实验 详解MySQL索引,事务与存储引擎_第11张图片

二 事务介绍

2.1 事务的概念

理论+实验 详解MySQL索引,事务与存储引擎_第12张图片

2.2 事务的ACID特点

理论+实验 详解MySQL索引,事务与存储引擎_第13张图片
理论+实验 详解MySQL索引,事务与存储引擎_第14张图片

2.3 事务控制语句

理论+实验 详解MySQL索引,事务与存储引擎_第15张图片

2.4 示例

mysql> begin;    ##开始事务
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('wangwu','beijing',15);    插入数据
Query OK, 1 row affected (0.00 sec)  

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | beijing  |  15 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

mysql> savepoint a;    ##保存点a
Query OK, 0 rows affected (0.00 sec)

mysql> insert into info (name,address,age) values ('zhaoliu','beijing',16);    ##插入数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | beijing  |  15 |
|  4 | zhaoliu  | beijing  |  16 |
+----+----------+----------+-----+
4 rows in set (0.00 sec)

mysql> savepoint b;    ##保存点b
Query OK, 0 rows affected (0.00 sec)

mysql> rollback to a;    ##回滚到a
Query OK, 0 rows affected (0.00 sec)

mysql> select * from info;
+----+----------+----------+-----+
| id | name     | address  | age |
+----+----------+----------+-----+
|  1 | zhangsan | beijing  |  20 |
|  2 | lisi     | shanghai |  22 |
|  3 | wangwu   | beijing  |  15 |
+----+----------+----------+-----+
3 rows in set (0.00 sec)

mysql> rollback to b;    ##回滚不回去
ERROR 1305 (42000): SAVEPOINT b does not exist
mysql> commit;
Query OK, 0 rows affected (0.00 sec)    ##提交事务

三 存储引擎介绍

3.1 存储引擎概念介绍

理论+实验 详解MySQL索引,事务与存储引擎_第16张图片
理论+实验 详解MySQL索引,事务与存储引擎_第17张图片

3.2 MyISAM的介绍

理论+实验 详解MySQL索引,事务与存储引擎_第18张图片
理论+实验 详解MySQL索引,事务与存储引擎_第19张图片

3.3 MyISAM适用的生产场景举例

理论+实验 详解MySQL索引,事务与存储引擎_第20张图片

3.4 lnnoDB特点介绍

理论+实验 详解MySQL索引,事务与存储引擎_第21张图片

3.5 lnnoDB适用生产场景分析

理论+实验 详解MySQL索引,事务与存储引擎_第22张图片

3.6 企业选择存储引擎依据

理论+实验 详解MySQL索引,事务与存储引擎_第23张图片
理论+实验 详解MySQL索引,事务与存储引擎_第24张图片

3.7 修改存储引擎

理论+实验 详解MySQL索引,事务与存储引擎_第25张图片
修改存储引擎
第一种方法

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql> alter table info engine=MyISAM;    ##修改表的存储引擎
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> show create table info \G    ##查看表存储引擎
*************************** 1. row ***************************
       Table: info
Create Table: CREATE TABLE "info" (
  "id" int(4) NOT NULL AUTO_INCREMENT,
  "name" varchar(10) NOT NULL,
  "address" varchar(50) DEFAULT 'nanjing',
  "age" int(3) NOT NULL,
  PRIMARY KEY ("id"),
  UNIQUE KEY "index_name" ("name")
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

第二种方法

[root@localhost ~]# vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
default-storage-engine=MyISAM    ##添加

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
[root@localhost ~]# mysql -uroot -p
Enter password: 
mysql> show variables like '%storage_engine%';    查看
+----------------------------------+--------+
| Variable_name                    | Value  |
+----------------------------------+--------+
| default_storage_engine           | MyISAM |
| default_tmp_storage_engine       | InnoDB |
| disabled_storage_engines         |        |
| internal_tmp_disk_storage_engine | InnoDB |
+----------------------------------+--------+
4 rows in set (0.00 sec)

第三种方法

[root@localhost ~]# yum -y install perl-DBI perl-DBD-MySQL
[root@localhost ~]# /usr/local/mysql/bin/mysql_convert_table_format --user=root --password='abc123' --sock=/tmp/mysql.sock auth

你可能感兴趣的:(实验,理论)