MySQL-- index and transaction storage engine MyLSAM and InnoDB (theory section)

The concept of index

Database indexing and similar books in the catalog

在一本书中,无须阅读整本书,利用目录就可以快速查找所需信息
书中的目录是一个词语列表,其中注明了包含各个词的页码

Database Index

在数据库中,索引数据库程序无须对整个表进行扫描,就可以在其中找到所需数据
数据库中的索引是某个表中一列或者若干列值的集合,以及物理标识这些值的数据页的逻辑指针清单

The role of the index

设置了合适的索引之后,数据库利用各种快速的定位技术,能够大大加快查询速率;
特别是当表很大时,或者查询涉及到多个表时,使用索引可使查询加快成千倍;
可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本;
通过创建唯一性索引保证数据表数据的唯一性;
可以加快表与表之间的连接;
在使用分组和排序时,可大大减少分组和排序时间;

Category Index

General index

这是最基本的索引类型,而且它没有唯一性之类的限制

Unique index

这种索引和前面的"普通索引"基本相同,但有一个区别:索引列的所有值都只能出现一次,即必须唯一

Primary key

主键是一种唯一性索引,但它必须制定为"PRIMARY KEY"

Full-text index

全文索引的类型是FULLTEXT,可以在VARCHAR或者TEXT类型的列上创建

Single and multi-column index Index

索引可以是单列上创建的索引,也可以是在多列上创建的索引

Creating an index based on the principle of

表的主键,外键必须有索引;
数据量超过300行的表应该有索引;
经常与其他表进行连接的表,在连接字段上应该建立索引;
唯一性太差的字段不适合建立索引;
更新太频繁的字段不适合创建索引;
经常出现在where子句中的字段,特别是大表的字段,应该建立索引;
索引应该建在选择性高的字段上;
索引应该建在小字段上,对于大的文本字段甚至超长字段,不要建索引;

Method of creating an index

According to business needs, after selecting a suitable index can be used to create an index CREATE INDEX

CREATE INDEX加上各个索引关键字便可创建各个类型的索引

Create a regular index

CREATE INDEX <索引的名字> ON tablename (列的列表);

Create a regular index

CREATE INDEX salary_index ON IT_salary(薪资);

Create a unique index

CREATE UNIQUE INDEX <索引的名字> ON tablename (列的列表);

Create a unique index example

CREATE UNIQUE INDEX salary_unique_index ON IT_salary(姓名);

Create a primary key index

CREATE TABLE tablename ( [...],PRIMARY KEY (列的列表));
ALTER TABLE tablename ADD PRIMARY KEY (列的列表);

Examples of primary key index

ALTER TABLE IT_salary ADD PRIMARY KEY (员工ID);

Index View

SHOW INDEX FROM tablename;
SHOW KEYS FROM tablename;

View index example

SHOW INDEX FROM IT_salary;
SHOW KEYS FROM IT_salary;

The concept Affairs

 A transaction is a mechanism, a sequence of operations comprises a set of database operations command and all commands to be submitted as a whole along with the request to undo or system, i.e., the set of database commands are either executed or not executed;
 a transaction is a logical unit of work indivisible when performing concurrent operations on the database system, the control unit is the smallest transaction;
 usage scene database system to multiple users simultaneously operated, such as banks, insurance companies and securities trading systems and the like;
 by the integrity of the transaction in order to ensure data consistency;

Features ACID transactions

Atomic:
A transaction is a complete operation, each element of the transaction is indivisible (atoms)
of all elements of the transaction as a whole must be committed or rolled back
if any element in the transaction fails, the entire transaction fails
consistency:
when the transaction is completed, the data must be in a consistent state: before the beginning of the transaction, the data stored in the database in a consistent state; in the ongoing transaction, data may be in an inconsistent state; when the transaction is completed successfully, the data must go back again known consistent state of
isolation:
all concurrent transactions to modify the data are isolated from each other, indicating that the transaction must be independent, he should not in any way affect or depend on other matters
transaction to modify the data can be used in another after the end of the transaction to access the same data access transactions before the start of the data, or re-use the same data to another data
persistence:
transaction durability means that regardless of whether the system is a failure, the result of the transaction are permanent
once the transaction is committed , the effect of the transaction will be permanently reserved Database

Operation of its services

默认情况下的MySQL的事务是自动提交的,当SQL语句提交时事务便自动提交

Manual transaction method for controlling:

事务处理命令控制
使用set设置事务出来方式

Transaction processing commands to control transactions:

begin:开始一个事务
commit:提交一个事务
rollback:回滚一个事务

Use the set command to control:

set autocommit=0:禁止自动提交
set autocommit=1:开启自动提交

Storage Engine Concepts

 Data in MySQL using different technologies stored in the file, each technology uses a different storage mechanism, indexing techniques, lock level and ultimately provide different functions and capabilities of these different technologies and supporting functions in MySQL called storage engine
 storage engine is MySQL the storage or storage format of data stored in the file system
 currently two commonly used MySQL storage engines:
  MyISQM
  InnoDB
 MySQL storage engine in MySQL database server is the component responsible for the actual implementation of the database data I / O operations
 of the main advantages of special storage engine is to provide only the desired specific characteristics of the application, the database system overhead smaller, more potent and higher performance database
 MySQL system, the engine is stored on the file system, the data stored in the data file prior to transmission and storage engine, after storage format for storage in accordance with the respective storage engine

MySQL-- index and transaction storage engine MyLSAM and InnoDB (theory section)

MyISQM Introduction

ISAM features:

ISAM执行读取操作的速度很快
而且不占用大量的内存和存储资源
他不支持事务处理
不能够容错

MyISAM features:

1、不支持事务
2、表级锁定形式,数据在更新时锁定整个表
3、数据库在读写过程中相互阻塞
●会在数据写入的过程阻塞用户数据的读取
●也会在数据读取的过程中阻塞用户的数据写入
4、可通过key_buffer_size来设置缓存索引,提高访问性能,减少磁盘IO的压力
●但缓存只会缓存索引文件,不会缓存数据
5、采用MyISAM存储引擎数据单独写入或读取,速度过程较快且占用资源相对少
6、MyISAM存储引擎它不支持外键约束,只支持全文索引
7、每个MyISAM在磁盘.上存储成三个文件,每- -个文件的名字以表的名字开始,扩展名指出文件类型
8、MyISAM在磁盘.上存储的文件
●.frm文件存储表定义
●数据文件的扩展名为.MYD (MYData)
●索引文件的扩展名是.MYI (MYIndex)

Examples of suitable production scenarios MyISAM

公司业务不需要事务的支持
一般单方面读取数据比较多的业务,或单方面写入数据比较多的业务
MyISAM存储引擎数据读写都比较频繁场景不适合
使用读写并发访问相对较低的业务
数据修改相对较少的业务
对数据业务一致性要求不是非常高的业务
服务器硬件资源相对比较差

InnoDB features introduced

支持事务:支持4个事务隔离级别
行级锁定,但是全表扫描仍然会是表级锁定
读写阻塞与事务隔离级别相关
具有非常高效的缓存特性:能缓存索引,也能缓存数据
表与主键以簇的方式存储
支持分区、表空间,类似oracle数据库
支持外键约束,5.5以前不支持全文索引,5.5版本以后支持全文索引
对硬件资源要求还是比较高的场合

InnoDB applicable production scene analysis

业务需要事务的支持
行级锁定对高并发有很好的适应能力,但需确保查询是通过索引来完成
业务数据更新较为频繁的场景,如:论坛,微博等
业务数据一致性要求较高,例如:银行业务
硬件设备内存较大,利用Innodb较好的缓存能力来提高内存利用率,减少磁盘I0的压力

Based enterprises to choose the storage engine

We need to consider each different storage engine that provides the core functionality and should be shipped scene

Support of fields and data types

所有引擎都支持通用的数据类型
但不是所有的引擎都支持其他的字段类型,如二进制对象

Lock Type: different storage engines support different levels of locking

表锁定
行锁定

Index support

建立索引在搜索和恢复数据库中的数据的时候能够显著提高性能
不同的存储引擎提供不同的制作索引的技术
有些存储引擎根本不支持索引

Supports transaction processing

事务处理功能通过提供在向表中更新和插入信息期间的可靠性
课根据企业业务是否支持事务选项选择存储引擎

Configuring Storage Engine

After selecting the appropriate storage engine in the enterprise, you can modify the

Modify the

查看数据库可配置的存储引擎
查看表正在使用的存储引擎
配置存储引擎为所选择的类型

Use show engines view supported by the system storage engine

方法1:
show table status from  库名  where  name='表名;
方法2:
show create table    表名;

Modify the storage engine

方法1: alter table修改;
alter table table_ name engine=引擎;
方法2:修改my.cnf,指定默认存储引擎并重启服务
default-storage-engine=InnDB
方法3: create table创建表时指定存储引擎
create table   表名   (字段) engine=   引擎
方法4: Mysql_ convert_ table_ format 转化存储引擎
Mysql_ convert_ table_ format -user=root -password=密码
- sock=/tmp/mysql.sock - engine =引擎 库名 表名

thanks for reading!

Guess you like

Origin blog.51cto.com/14080162/2452678