mysql storage engine study notes

Familiar storage engines are: Myisam, innodb

What is a storage engine:
A format in which data tables store data.
Different formats are used to store data, and the characteristics of different formats are also different. For example: the features of the innodb storage engine are:
support for transactions, support for row-level locks, and support for foreign keys

Check the database file location:

mysql> show global variables  like '%datadir%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)

Check

myisam

The structure, data, and index of this search engine have separate files.
The following three files support physical copy and paste.

[root@localhost mysql_optimize]# pwd
/var/lib/mysql/mysql_optimize
[root@localhost mysql_optimize]# ls
   db.opt   
   student.frm  # 表结构文件
   student.MYD   # 表数据文件
   student.MYI  # 表索引文件

innodb storage engine

The innodb storage engine data table has a separate "structure file"
1. By default, the indexes, data, and files of all innodb data tables in the current database are merged together.

[root@localhost mysql_optimize]# ls -lh 
total 132K
-rw-r-----. 1 mysql mysql   65 Jun 12 11:41 db.opt
-rw-r-----. 1 mysql mysql 8.5K Jun 12 12:14 student1.frm
-rw-r-----. 1 mysql mysql  96K Jun 12 12:14 student1.ibd
-rw-r-----. 1 mysql mysql 8.5K Jun 12 11:45 student.frm
-rw-r-----. 1 mysql mysql   56 Jun 12 12:13 student.MYD
-rw-r-----. 1 mysql mysql 2.0K Jun 12 12:13 student.MYI

2. The location of the data index:
a storage file in the "data, index" set of all innodb data tables in the database

[root@localhost mysql]# stat  ibdata1 
  File: ‘ibdata1’
  Size: 79691776    Blocks: 155648     IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 67162011    Links: 1
Access: (0640/-rw-r-----)  Uid: (   27/   mysql)   Gid: (   27/   mysql)
Context: system_u:object_r:mysqld_db_t:s0
Access: 2017-06-12 04:30:33.791881824 -0400
Modify: 2017-06-12 12:14:05.487200858 -0400
Change: 2017-06-12 12:14:05.487200858 -0400
 Birth: -
[root@localhost mysql]# date 
Mon Jun 12 12:22:32 EDT 2017

Separate "data/index" files

To set up its "data/index" file separately for each innodb data
Each innodb data table is finally formed in two formats: *.frm data/index file

Check if it is turned on:

mysql> show variables like 'innodb_file_per_table%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.00 sec)
mysql> set global innodb_file_per_table=1;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'innodb_file_per_table%'
    -> ;
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.01 sec)

* innodb way sort on write and read*

CREATE TABLE "student2" (
  "id" int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  "name" varchar(20) NOT NULL COMMENT '名字',
  "height" decimal(6,2) NOT NULL DEFAULT '0.00' COMMENT '身高',
  "introduce" text COMMENT '个人简介',
  PRIMARY KEY ("id")
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;

mysql> select * from student2;
+----+--------+--------+-------------+
| id | name   | height | introduce   |
+----+--------+--------+-------------+
| 10 | jack   | 176.23 | I am jack   |
| 16 | yuehan | 176.23 | I am yuehan |
| 19 | mary   | 176.23 | I am mary   |
| 23 | bob    | 176.23 | I am bob    |
+----+--------+--------+-------------+
4 rows in set (0.00 sec)

myisam does not sort when reading and writing data

insert into student values(10,'jack',176.23,'I am jack');
insert into student values(23,'bob',176.23,'I am bob');
insert into student values(16,'yuehan',176.23,'I am yuehan');
insert into student values(19,'mary',176.23,'I am mary');
mysql> mysql> select * from student;
+----+--------+--------+-------------+
| id | name   | height | introduce   |
+----+--------+--------+-------------+
|  1 | tom    | 175.34 | I am tom    |
|  2 | tom    | 175.34 | I am tom    |
| 10 | tom    | 175.23 | I am jack   |
| 23 | bob    | 176.23 | I am bob    |
| 16 | yuehan | 176.23 | I am yuehan |
| 19 | mary   | 176.23 | I am mary   |
+----+--------+--------+-------------+
6 rows in set (0.00 sec)

It is found that the writing order of the data is consistent with the reading order.
Enlightenment : When storing data in the Myisam data table, it is not sorted and stored in the order of writing. The advantage of this is that the writing speed is very fast.
Innodb data table, the order of writing data is inconsistent with the order of storage, and the book needs to be placed in the place where it should be placed according to the order of the primary key. Write speed is slower than myisam

concurrency

Myisam is slightly lower concurrently, and the speed of multiple simultaneous requests is slightly slower.
Locking mechanism: Locking the entire data table innodb every time
and issuing it is higher, multiple people request at the same time, high speed and sales.
Locking mechanism: row lock, only one record information is locked at a time.

compression mechanism

If a myisam data table stores a lot of data, it will occupy a very large hard disk space. For optimization, the myisam data table can be compressed. This saves resources on hard disk space.

mysql> insert into student select null,name,height,introduce from student ;
Query OK, 786432 rows affected (3.23 sec)
Records: 786432  Duplicates: 0  Warnings: 0
mysql> flush table student; 
Query OK, 0 rows affected (0.00 sec)
[root@localhost mysql_optimize]# myisampack student 
Compressing student.MYD: (1572864 records)
- Calculating statistics
- Compressing file
58.45%     
Remember to run myisamchk -rq on compressed tables

* Rebuild the index* Rebuild the index
based on the compressed data
Rebuild the index tool: myisamchk

[root@localhost mysql_optimize]# myisamchk -rq student 
- check record delete-chain
- recovering (with sort) MyISAM-table 'student'
Data records: 1572864
- Fixing index 1
[root@localhost mysql_optimize]# ls -lh  
-rw-r-----. 1 mysql mysql 8.5K Jun 12 11:45 student.frm
-rw-r-----. 1 mysql mysql  18M Jun 12 14:34 student.MYD
-rw-r-----. 1 mysql mysql  16M Jun 12 14:47 student.MYI

After compression, data can no longer be written to the myisam table.
Some data tables are suitable for compression, and data that changes infrequently are suitable, such as national zip code information and user shipping address information.

mysql> insert into student select null,name,height,introduce from student ;
ERROR 1036 (HY000): Table 'student' is read only

* Decompression*
If you must write to the compressed data table, decompress it:
myisamchk –unpakc table
name At the same time as decompression, the index will be automatically rebuilt

[root@localhost mysql_optimize]# myisamchk --unpack student
- recovering (with sort) MyISAM-table 'student'
Data records: 1572864
- Fixing index 1

mysql> flush table student ;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into student values(null,'jack',176.23,'I am jack');
Query OK, 1 row affected (0.01 sec)

memory storage engine

memory storage engine,

  • Features: Internal data runs very fast
  • Features: If the server is powered off, the data in the storage engine will be emptied.

Guess you like

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