一、The InnoDB Engine

Each InnoDB table is represented on disk by an .frm format file in the database directory,as well as data and index storage in the InnoDB tablespace.The InnoDB tablespace is a logical single storage area that is made up of one or more files or partitions on disk.By default,InnoDB uses a single tablespace that is shared by all InnoDB tables.The tablespace is stored in machine-independent format.It is implemented such that table sizes can exceed the maximum file size allowed by the filesystem.It is also possible to configure InnoDB to create each table with its own tablespace.

InnoDB supports transactions,with commit and rollback.It provides full ACID(atomicity,consistency,isolation, durability) compliance.Multi-versioning is used to isolate transactions from one another.

InnoDB provides auto-recovery after a crash of the MySQL server or the host on which the server runs.     MySQL manages query contention for InnoDB tables using multi-versioning and row-level locking.Multi- versioning gives each transaction its own view of the database.This,combined with row-level locking,keeps contention to a minimum.The result is good query concurrency even if clients are performing a mix of reads and writes.However,it's possible for deadlock to occur.     InnoDB supports foreign keys and referential integrity,including cascaded deletes and updates.     The tablespace storage format is portable,so InnoDB files can be copied directly to another host and used by a server there.     InnoDB operates using two primary disk-based resources:a tablespace for storing table contents,and a set of log files for recording transaction activity.

Each InnoDB table has a format (.frm) file in the database directory of the database to which the table belogs. This is the same as tables managed by any other MySQL storage engine,such as MyISAM.However,InnoDB manages table contents (data rows and indexes) on disk differently than does the MyISAM engine.By default, InnoDB uses a shared "tablespace",which is one or more files that form a single logical storage area.All InnoDB tables are stored together within the tablespace.There are no table-specific data files or index files for InnoDB the way there are for MyISAM tables.The tablespace also contains a rollback segment.As transactions modify rows,undo log information is stored in the rollback segment.This information is used to roll back failed transactions.

二、The InnoDB Tablespace and Logs

Although InnoDB treats the shared tablespace as a single logical storage area,it can consist of one file or multiple files.Each file can ben a regular file or a raw partition.The final file in the shared tablespace can ben configured to be auto-extending,in which case InnoDB expands it automatically if the tablespace file up. Because the shared tablespace is used for InnoDB tables in all database (and thus is not database specific), tablespace files are stored by default in the server's data directory,not within a particular database directory.

If you do not want to use the shared tablespace for storing table contents,you can start the server with the --innodb_file_per_table option.In this case,for each new table that InnoDB creates,it sets up an .ibd file in the database directory to accompany the table's .frm file.The .ibd file acts as the table's own tablespace file and InnoDB stores table contents in it.(The shared tablespace still is needed because it contains the InnoDB data dictionary and the rollback segment.)

Use of the --innodb_file_per_table option does not affect accessibility of any InnoDB tables that may already have been created in the shared tablespace.Those tables remain accesibel.

In addition to its tablespace files,the InnoDB storage engine manages a set of InnoDB-specific log files that contain information about ongoing transactions.As a client performs a transaction,the changes that it makes are held in the InnoDB log.The more recent log contents are cached in memory.Normally, the cached log information is written and flushed to log files on disk at transaction commit time,though that may also occur earlier.

If a crash occurs while the tables are being modified,the log file are used for auto-recovery.When the MySQL server restarts,it reapplies the changes recorded in the logs,to ensure that the tables reflect all committed transactions.

It can consist of one file or multiple files.     Each component file of the tablespace can be a regular file or a raw partition (a device file).A given tablespace can including both types of files.

Tablespace files can be on different filesystems or physical disk drives.One reason to place the files on multiple physical drives is to distribute InnoDB-related disk activity among them.

The tablespace size can exceed the limits that the filessystem places on maximum file size.This is true for two reasons.First,the tablespace can consist of multiple files and thus can be large than any single file.Second, the tablespace can include raw partitions,which are not bound by filesystem limits on maximum file size.InnoDB can use the full extent of partitions,which makes it easy to configure a very large tablespace.

The last component of the tablespace can be auto-extending,with an optional limit on how large the file can grow.

Using Foreign Keys   Both tables must be InnoDB tables and they must be TEMPORARY tables.     In the referencing table,there must be an index where the foreign key columns are listed as the first columns in the same order.Such an index is created on the referencing table automatically if it does not exist.     In the referenced table,there must be an index where the referenced columns are listed as the first columns in the same order.

Index prefixes on foreign key columns are not supported.     If the CONSTRAINT symbol clause is given,the symbol value must be unique in the database.

三、Configuring InnoDB Buffers

InnoDB uses a buffer pool to hold information read from InnoDB tables.The buffer pool serves to reduce disk I/O for information that is frequently accessed,and a larger buffer more effectively achieves this goal.To change the size of the buffer pool,set the innodb_buffer_pool_size option.Its default value is 8MB.If your machine has the memory available,you can the value much higher.

innodb_buffer_pool_size

The size in bytes of the momory buffer InnoDB uses to cache data and indexes of its tables.The larger you set this value,the less disk I/O is neednd to access data in tables.On a dedicated database server, you may set this to up to 80% of the machine physical memory size.However,do not set it too large because competition for physical memory might cause paging in the operating system.

innodb_additional_mem_pool_size

The size in bytes of a memory pool InnoDB uses to store data dictionary information and other internal data structures.The more tables you have in your application,the more memory you need to allocate here.If InnoDB runs out of memory in this pool,it starts to allocate memory from the operating system and writes warning messages to the MySQL error log.The default value is 1MB.

Innodb_max_dirty_pages_pct

This is an integer in the range from 0 to 100.The default is 90.The main thread in InnoDB tries to write pages from the buffer pool so that the percentage of dirty(not yet written) pages will not exceed this value.

四、Use foreign key

mysql> create table parent(p_id int,p_msg varchar(100),->  index idx_p_id (p_id))-> engine = innodb;
Query OK, 0 rows affected (0.05 sec)mysql> show index from parent \G;
*************************** 1. row ***************************Table: parentNon_unique: 1Key_name: idx_p_idSeq_in_index: 1Column_name: p_idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
1 row in set (0.01 sec)ERROR:
No query specifiedmysql> create table child(c_id int,c_msg varchar(200),->   foreign key (c_id) references parent(p_id)->  on delete cascade->  on update cascade)->  engine = innodb;
Query OK, 0 rows affected (0.04 sec)mysql> show index from child \G;
*************************** 1. row ***************************Table: childNon_unique: 1Key_name: c_idSeq_in_index: 1Column_name: c_idCollation: ACardinality: 0Sub_part: NULLPacked: NULLNull: YESIndex_type: BTREEComment:
Index_comment:
1 row in set (0.01 sec)ERROR:
No query specifiedmysql> insert into child values(1,'aaa');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`jack`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`c_id`) REFERENCES `parent` (`p_id`) ON DELETE CASCADE ON UPDATE CASCADE)  mysql> insert into parent values(1,'aaaaaa');
Query OK, 1 row affected (0.01 sec)mysql> insert into child values(1,'aaa');
Query OK, 1 row affected (0.01 sec)mysql> select * from child;
+------+-------+
| c_id | c_msg |
+------+-------+
|    1 | aaa   |
+------+-------+
1 row in set (0.01 sec)mysql> select * from parent;
+------+--------+
| p_id | p_msg  |
+------+--------+
|    1 | aaaaaa |
+------+--------+
1 row in set (0.00 sec)mysql> insert into child values(1,'bbbb');
Query OK, 1 row affected (0.01 sec)mysql> select * from child;
+------+-------+
| c_id | c_msg |
+------+-------+
|    1 | aaa   |
|    1 | bbbb  |
+------+-------+
2 rows in set (0.00 sec)mysql> update parent set p_id=2;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from child;
+------+-------+
| c_id | c_msg |
+------+-------+
|    2 | aaa   |
|    2 | bbbb  |
+------+-------+
2 rows in set (0.00 sec)mysql> select * from parent;
+------+--------+
| p_id | p_msg  |
+------+--------+
|    2 | aaaaaa |
+------+--------+
1 row in set (0.00 sec)mysql> delete from parent;
Query OK, 1 row affected (0.20 sec)mysql> select * from parent;
Empty set (0.00 sec)mysql> select * from child;
Empty set (0.00 sec)mysql> select constraint_name,update_rule,delete_rule,table_name,referenced_table_name from REFERENTIAL_CONSTRAINTS where table_name = 'child';
+-----------------+-------------+-------------+------------+-----------------------+
| constraint_name | update_rule | delete_rule | table_name | referenced_table_name |
+-----------------+-------------+-------------+------------+-----------------------+
| child_ibfk_1    | CASCADE     | CASCADE     | child      | parent                |
+-----------------+-------------+-------------+------------+-----------------------+
1 row in set (0.01 sec)  

转载于:https://www.cnblogs.com/Richardzhu/p/3312081.html

MySQL存储引擎之InnoDB相关推荐

  1. MySQL存储引擎(InnoDB引擎)

    本篇章主要介绍什么是 MySQL 存储引擎?常用的 MySQL 存储引擎有哪些?以及详细介绍一下目前应用最广泛的 InnoDB 存储引擎,包括其:逻辑存储结构.架构.事务原理.MVCC等. 一.初识  ...

  2. mysql存储引擎中INNODB和MyISAM的区别

    切记:存储引擎是基于表的,而不是数据库. 存储引擎概念: MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不 ...

  3. MySQL存储引擎及InnoDB并发控制介绍

    MySQL存储引擎采用了可插拔的结构,即用户可以根据自己的需要来选择不同的存储引擎. 下表是MySQL不同的存储引擎的不同的特性: Feature<?xml:namespace prefix = ...

  4. MySQL 存储引擎(InnoDB、MyISAM、MEMORY)

    一.MySQL的体系结构 1.连接层:最上层是一些客户端和链接服务,主要完成一些类似于连接处理.授权认证.及相关的安全方案.服务器也会为安全接入的每个客户端验证它所具有的操作权限. 2.服务层:第二成 ...

  5. 浅谈MySQL存储引擎选择 InnoDB还是MyISAM

    如果是一些小型的应用或项目,那么MyISAM 也许会更适合.当然,在大型的环境下使用MyISAM 也会有很大成功的时候,但却不总是这样的.如果你正在计划使用一个超大数据量的项目,那么你应该直接使用In ...

  6. Mysql 存储引擎中InnoDB与Myisam的主要区别

    一直以为我spring事物没有配置好,结果发现是mysql的表本身设置成了Myisam 引擎.改成innodb就支持事物了. 1, 事务处理 innodb 支持事务功能,myisam 不支持. Myi ...

  7. Mysql存储引擎中InnoDB与Myisam的区别

    为什么80%的码农都做不了架构师?>>>    1. 事务处理 innodb 支持事务功能,myisam 不支持. Myisam 的执行速度更快,性能更好. 2. select ,u ...

  8. MySQL存储引擎之Myisam和Innodb总结性梳理

    Mysql有两种常用的存储引擎:InnoDB与Myisam,下表是两种引擎的简单对比   MyISAM InnoDB 构成上的区别: 每个MyISAM在磁盘上存储成三个文件.第一个 文件的名字以表的名 ...

  9. MySQL数据库MyISAM存储引擎转为Innodb

    之前公司的数据库存储引擎全部为MyISAM,数据量和访问量都不是很大,所以一直都没什么问题.但是最近出现了MySQL数据表经常被锁的情况,直接导致了用户连接网站时超时而返回502,于是决定把存储引擎转 ...

最新文章

  1. asp.net performance
  2. SQLServer还原 指定的转换无效解决方法
  3. IDEA JDK1.8 ProGuard 混淆Maven项目代码
  4. 如何断开mongodb数据库连接_mongodb关闭数据库实例
  5. 【转】SQL语句删除和添加外键、主键
  6. python︱写markdown一样写网页,代码快速生成web工具:streamlit lay-out布局(四)
  7. mysql单表多次内联接查询学科名称,一级学科名称二级学科名称三级学科名称
  8. OSChina 愚人节乱弹 ——我们组建个程序员国度吧
  9. 微生物组-扩增子16S分析和可视化(线上/线下,本周开课,2021.10)
  10. 路由器的信号无法连接到服务器,无线路由器有信号却连不上怎么办
  11. SpringBoot 文件上传 基于MD5 文件内容校验工具类
  12. Python:Excel自动化实践入门篇 甲【留言点赞领图书门票】
  13. iOS “此证书由未知颁发机构签名“
  14. 生信软件(1)bioawk
  15. gitlab 生成ssh密匙
  16. 最简单的梯度下降法求最优值
  17. 【基础知识】现在很火的app上的deeplink技术,到底是什么?
  18. 激战2电信服务器哪个最多,《激战2》电信一区服务器玩家实力排行榜
  19. 我是如何管理我的团队的?
  20. 破解android 九宫格锁

热门文章

  1. C语言再学习 -- NUL和NULL的区别
  2. zcmu2014(公式推导+二分)
  3. [以太坊源代码分析] II. 数据的呈现和组织,缓存和更新
  4. Satori变种正在通过替换钱包地址盗取ETH数字代币
  5. 研究Xposed相关二:如何root android模拟器(android4.3.1)
  6. JZOJ 5925. 【NOIP2018模拟10.25】naive 的瓶子
  7. 大学计算机二级考试 vb,大学计算机二级考试常用vb代码.docx
  8. mysql linux 客户端_MySQL—Linux查看客户端连接信息(连接数、进程等)
  9. BZOJ-1009-GT考试-HNOI2008
  10. 您没有足够的全新为该计算机所有用户安装,很抱歉,无法安装Office(64位),因为您的计算机上已经安装了这些32位Office程序解决办法...