MySQL第五章节 存储引擎

1.查看默认存储引擎

1)查看默认存储引擎
mysql[(none)]>select @@default_storage_engine;
+--------------------------+
| @@default_storage_engine |
+--------------------------+
| InnoDB                   |
+--------------------------+
1 row in set (0.00 sec)

2) 模糊查找
mysql[(none)]>show variables like '%engine%' ;
+----------------------------------+--------+
| Variable_name                    | Value  |
+----------------------------------+--------+
| default_storage_engine           | InnoDB |
| default_tmp_storage_engine       | InnoDB |
| disabled_storage_engines         |        |
| internal_tmp_disk_storage_engine | InnoDB |
+----------------------------------+--------+
4 rows in set (0.31 sec)

3)查看所有存储引擎
mysql[(none)]>show engine;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

InnoDB: (默认5.5之后)
MyISAM :( 5.5以前的默认存储引擎)
了解:CSV ,MEMORY , BLACKHOLE , FEDERATED

第三方:
TokuDB
MyRocks
RocksDB

2.InnoDB 和 MyISAM 的区别

事务
行级锁
热备
ACSR
MVCC

了解:外键,复制,B+TREE,B*TREE

innodb 存储引擎物理存储结构

InnoDB最直观的存储方式
citi.frm 表的列定义
city.ibd 表的数据和索引
ibdata1(5.7) 共享表空间文件(UNDO回滚数据<8.0独立>,数据字典)
ib_logfile0~ib_logfileN repo log文件
ibtmp1(5.7) 存放临时表
ib_buffer_pool 缓冲区池的映射文件

InnoDB 的表空间管理模式
city表 ----> 独立表空间 ----> 表空间数据文件:IBD(city.idb) ----> 段 区 页

共享表空间模式(5.5默认):
目前遗留下来了,用来存储系统数据

独立表空间模式(5.6以后默认):
一个表一个ibd文件

共享表空间的设置

mysql[(none)]>select @@innodb_data_file_path;
+-------------------------+
| @@innodb_data_file_path |
+-------------------------+
| ibdata1:12M:autoextend  |
+-------------------------+
1 row in set (

一般是在初始化数据之前:
vim /etc/my.cnf 
innodb_data_file_path=ibdata1:512M;ibdata2:512M:autoextend

mysql[(none)]>show variables like '%extend%';
+-----------------------------+-------+
| Variable_name               | Value |
+-----------------------------+-------+
| innodb_autoextend_increment | 64    |
+-----------------------------+-------+
1 row in set (0.00 sec)

独立表空间的设置

mysql[(none)]>select @@innodb_file_per_table;
+-------------------------+
| @@innodb_file_per_table |
+-------------------------+
|                       1 |
+-------------------------+
1 row in set (0.00 sec)

独立表空间的迁移
alter table t1 discard tablespace;
alter table t1 import tablespace;

InnoDB 事务的ACID如何保证

概念:

名词 概念
redo log : 重做日志,记录数据页的变化(ib_logfile0)
redo log buffer: redo log的缓冲区(内存)
ibd : 表空间的数据文件,以段区页方式规划存储数据行和索引
buffer pool 数据页缓冲区
LSN 日志序列号,redo log\logbuffer\ibd\buffer pool
WAL 日志优先写磁盘
脏页 在内存 中被修改的数据页
CKPT checkpoint,将内存脏页回写到磁盘的动作
TXID 事务ID,伴随着事务的整个生命周期
undo log 回滚日志

redo 功能

简介

重做日志,ib_logfile0,ib_logfile1......
记录内容
记录数据页的变化
作用:
主要 保证ACID中的“D”,持久化功能,对于AC也有相应的保证
加快了commit命令的速度,提高了事务的并发
并且实现了,在MySQL Crash时,ACSR中前滚功能

undo 功能

简介

回滚日志,5.7存储在ibdata1,8.0 undo单独存储
undu记录的内容
记录逆操作
作用
主要保证了A的特性,CI也有相应的功能

隔离级别和锁机制

功能
主要提供了ACID中的“I”隔离性的功能,C的功能受到一定影响
隔离级别介绍

oldguo[(none)]>select @@tx_isolation;
oldguo[(none)]>select @@transaction_isolation;
oldguo[(none)]>show variables like '%tx%';

vim /etc/my.cnf 
transaction_isolation=REPEATABLE-READ
transaction_isolation=READ-COMMITTED
级别 表示 现象
RU: 读未提交 脏读,不可重复读,幻读
RC: 读已提交 不脏读,有不可重复读,有幻读
RR: 可重复读 防止脏读,防止不可重复读(快照),可能出现幻读(gap间隙锁,next-lock下一键锁)
SR: 可串行化 ---

InnoDB 的锁 (主要解决“I”)

record lock 记录所,行级锁
gap间隙锁
next-lock下一键锁

存储引擎参数

默认存储引擎

  select @@default_storage_engine;

Percona:XtraDB 默认
MariaDB:InnoDB 默认myrocks tokudb

innodb_buffer_pool_size(缓冲区大小)

+-------------------------------------+----------------+
| Variable_name                       | Value          |
+-------------------------------------+----------------+
| innodb_buffer_pool_chunk_size       | 134217728      |
| innodb_buffer_pool_dump_at_shutdown | ON             |
| innodb_buffer_pool_dump_now         | OFF            |
| innodb_buffer_pool_dump_pct         | 25             |
| innodb_buffer_pool_filename         | ib_buffer_pool |
| innodb_buffer_pool_instances        | 1              |
| innodb_buffer_pool_load_abort       | OFF            |
| innodb_buffer_pool_load_at_startup  | ON             |
| innodb_buffer_pool_load_now         | OFF            |
| innodb_buffer_pool_size             | 134217728      |
+-------------------------------------+----------------+
10 rows in set (0.00 sec)

vim /etc/my.cnf
innodb_buffer_pool_size=2G

官方建议:最多95%
生产建议:不超过80%,50-70%

1.业务够用的
2.公司硬件有预留
3.MySQL还有可能额外使用到其它的内存结构
4.我们公司做的多实例
mysql[(none)]>show engine innodb status \G
》》》》》找到缓冲区信息

----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 137428992
Dictionary memory allocated 149717
Buffer pool size   8191
Free buffers       7663
Database pages     528
Old database pages 213
Modified db pages  0
Pending reads      0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 489, created 39, written 96
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 528, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
1 read views open inside InnoDB
Process ID=7324, Main thread ID=140396550145792, state: sleeping
Number of rows inserted 11, updated 2, deleted 0, read 527
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s

计算:
Buffer pool size   8191        页
每页16kb

innodb_log_buffer_size(日志缓冲区)

mysql[(none)]>select  @@innodb_log_buffer_size;
+--------------------------+
| @@innodb_log_buffer_size |
+--------------------------+
|                 16777216 |
+--------------------------+
1 row in set (0.00 sec)

innodb_log_file_size(redo参数)

mysql[(none)]>show variables like "%log_buffer%";
+------------------------+----------+
| Variable_name          | Value    |
+------------------------+----------+
| innodb_log_buffer_size | 16777216 |
+------------------------+----------+
1 row in set (0.00 sec)

relog 的刷写策略("双1")

mysql[test]>select @@innodb_flush_log_at_trx_commit;
+----------------------------------+
| @@innodb_flush_log_at_trx_commit |
+----------------------------------+
|                                1 |
+----------------------------------+
1 row in set (0.00 sec)

默认:
“1”,在每个事务提交时,立即刷写redo buffer中日志到OS buffer,并立即FSYNC到磁盘。
“0”,每秒,刷写redo buffer 中的日志到OS buffer,每秒FSYNC到磁盘。
“2”,在每个 事务提交时,立即刷写redo buffer中入职到OS buffer ,每秒FSYNC到磁盘。

你可能感兴趣的:(MySQL第五章节 存储引擎)