MySQL Search Engine Summary

Reprinted: http: //mp.weixin.qq.com/s __biz = MzA5Mjg2NTQxOA == & mid = 2650420350 & idx = 1 & sn = 61673a528ec43bbd84e53b35a54b1ae8 & chksm = 8868029bbf1f8b8d7c330c05ce2e671b769a03189de41d7c629bcf28dadf2d5e135c3c883c94 & mpshare = 1 & scene = 1 & srcid = 03220h54UTF98MdH0f4xN1UG # rd?

 

What is a storage engine?

A relational database table is a data structure used to store and organize information. A table can be understood as a table consisting of rows and columns, similar to Excel's spreadsheet form. Some tables are simple, some are complex, some are not used to store any long-term data at all, and some are very fast to read, but poor to insert data; and in the actual development process, we may need A variety of tables, different tables, means that different types of data are stored, and there will be differences in data processing, then. For MySQL, it provides many types of storage engines, we can choose different storage engines according to the needs of data processing, so as to maximize the use of the powerful functions of MySQL . This blog post will summarize and analyze the characteristics of each engine, as well as the applicable occasions, and will not be entangled in deeper things. My learning method is to first learn to use it, know how to use it, and then to know how it can be used. The following is a brief introduction to the storage engines supported by MySQL.

MyISAM

MyISAM tables are operating system independent, which means they can be easily ported from a Windows server to a Linux server; whenever we create a table for the MyISAM engine, three files are created on the local disk, and the file names indicate . For example, if I create a tb_Demo table for the MyISAM engine, the following three files will be generated:

1.tb_demo.frm, storage table definition;
2.tb_demo.MYD, storage data;
3.tb_demo.MYI, storage index.

MyISAM tables cannot handle transactions, which means that tables with transaction processing requirements cannot use the MyISAM storage engine. The MyISAM storage engine is particularly suitable for use in the following situations:

1. Select the intensive table. MyISAM storage engine is very fast when sifting through large amounts of data, which is its most prominent advantage.
2. Insert intensive table. The concurrent insert feature of MyISAM allows simultaneous selection and insertion of data. For example: the MyISAM storage engine is great for managing mail or Web server log data.

InnoDB

InnoDB is a robust transactional storage engine , which has been used by many Internet companies and provides a powerful solution for users to operate very large data storage. MySQL version 5.6.13 installed on my computer, InnoDB is the default storage engine. InnoDB also introduces row-level locking and foreign key constraints, and using InnoDB is ideal in the following situations:

1. Update the dense table. The InnoDB storage engine is particularly suitable for handling multiple concurrent update requests.
2. Transactions. The InnoDB storage engine is the standard MySQL storage engine that supports transactions.
3. Automatic disaster recovery. Unlike other storage engines, InnoDB tables can automatically recover from disasters.
4. Foreign key constraints. The only storage engine that supports foreign keys in MySQL is InnoDB.
5. Support to automatically increase the column AUTO_INCREMENT attribute.

In general, if transaction support is required and there is a high frequency of concurrent reads, InnoDB is a good choice.

MEMORY

使用MySQL Memory存储引擎的出发点是速度。为得到最快的响应时间,采用的逻辑存储介质是系统内存。虽然在内存中存储表数据确实会提供很高的性能,但当mysqld守护进程崩溃时,所有的Memory数据都会丢失。获得速度的同时也带来了一些缺陷。它要求存储在Memory数据表里的数据使用的是长度不变的格式,这意味着不能使用BLOB和TEXT这样的长度可变的数据类型,VARCHAR是一种长度可变的类型,但因为它在MySQL内部当做长度固定不变的CHAR类型,所以可以使用。

一般在以下几种情况下使用Memory存储引擎:

1.目标数据较小,而且被非常频繁地访问。在内存中存放数据,所以会造成内存的使用,可以通过参数max_heap_table_size控制Memory表的大小,设置此参数,就可以限制Memory表的最大大小。

2.如果数据是临时的,而且要求必须立即可用,那么就可以存放在内存表中。

3.存储在Memory表中的数据如果突然丢失,不会对应用服务产生实质的负面影响。

Memory同时支持散列索引和B树索引。B树索引的优于散列索引的是,可以使用部分查询和通配查询,也可以使用<、>和>=等操作符方便数据挖掘。散列索引进行“相等比较”非常快,但是对“范围比较”的速度就慢多了,因此散列索引值适合使用在=和<>的操作符中,不适合在<或>操作符中,也同样不适合用在order by子句中。

可以在表创建时利用USING子句指定要使用的版本。例如:

create table users
(
    id smallint unsigned not null auto_increment,
    username varchar(15) not null,
    pwd varchar(15) not null,
    index using hash (username),
    primary key (id)
)engine=memory;

  上述代码创建了一个表,在username字段上使用了HASH散列索引。下面的代码就创建一个表,使用BTREE索引。

create table users
(
    id smallint unsigned not null auto_increment,
    username varchar(15) not null,
    pwd varchar(15) not null,
    index using btree (username),
    primary key (id)
)engine=memory;

MERGE

MERGE存储引擎是一组MyISAM表的组合,这些MyISAM表结构必须完全相同,尽管其使用不如其它引擎突出,但是在某些情况下非常有用。说白了,Merge表就是几个相同MyISAM表的聚合器;Merge表中并没有数据,对Merge类型的表可以进行查询、更新、删除操作,这些操作实际上是对内部的MyISAM表进行操作。Merge存储引擎的使用场景。

对于服务器日志这种信息,一般常用的存储策略是将数据分成很多表,每个名称与特定的时间端相关。例如:可以用12个相同的表来存储服务器日志数据,每个表用对应各个月份的名字来命名。当有必要基于所有12个日志表的数据来生成报表,这意味着需要编写并更新多表查询,以反映这些表中的信息。与其编写这些可能出现错误的查询,不如将这些表合并起来使用一条查询,之后再删除Merge表,而不影响原来的数据,删除Merge表只是删除Merge表的定义,对内部的表没有任何影响。

ARCHIVE

Archive是归档的意思,在归档之后很多的高级功能就不再支持了,仅仅支持最基本的插入和查询两种功能。在MySQL 5.5版以前,Archive是不支持索引,但是在MySQL 5.5以后的版本中就开始支持索引了。Archive拥有很好的压缩机制,它使用zlib压缩库,在记录被请求时会实时压缩,所以它经常被用来当做仓库使用。

如何选择合适的存储引擎?
选择标准可以分为:
(1)是否需要支持事务;
(2)是否需要使用热备;
(3)崩溃恢复:能否接受崩溃;
(4)是否需要外键支持;

然后按照标准,选择对应的存储引擎即可。 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326312178&siteId=291194637