MySQL存储引擎之InnoDB
一、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: parent
Non_unique: 1
Key_name: idx_p_id
Seq_in_index: 1
Column_name: p_id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.01 sec) ERROR:
No query specified mysql> 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: child
Non_unique: 1
Key_name: c_id
Seq_in_index: 1
Column_name: c_id
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.01 sec) ERROR:
No query specified mysql> 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: 0 mysql> 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)
MySQL存储引擎之InnoDB的更多相关文章
- MySQL存储引擎【InnoDB、MyISAM、Memory】
数据库,MySQL这样存在多存储引擎的数据库软件,清楚常见的存储引擎的区别,使用合适的存储引擎,使得项目跑的更顺畅,有时候对于一个项目,甚至比项目本身都重要.这篇文章,旨在浅谈常见的三种存储引擎的区别 ...
- mysql 存储引擎 myisam innodb 区别
虽然MySQL里的存储引擎不只是MyISAM与InnoDB这两个,但常用的就是它俩了.可能有站长并未注意过MySQL的存储引擎,其实存储引擎也是数据库设计里的一大重要点,那么博客系统应该使用哪种存储引 ...
- MySQL存储引擎:InnoDB和MyISAM的差别/优劣评价/评测/性能测试
InnoDB和MyISAM简介 MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的 顺序访问方法) 的缩写 ...
- 二、mysql存储引擎之InnoDB
一.存储引擎简介 mysql采用业务逻辑和数据存储分离的架构,底层的存储引擎为上层的SQL层提供了支持:mysql采用的是插件的方式将存储引擎直接加载到正在运行的MySQL中,这是mysql的一个重要 ...
- 浅谈MySQL存储引擎选择 InnoDB还是MyISAM
如果是一些小型的应用或项目,那么MyISAM 也许会更适合.当然,在大型的环境下使用MyISAM 也会有很大成功的时候,但却不总是这样的.如果你正在计划使用一个超大数据量的项目,那么你应该直接使用In ...
- MySql存储引擎:innodb myisan memory
一.MySQL存在的常用存储引擎 存储引擎就是指表的类型,数据库的存储引擎决定了表在计算机中的存储方式. 使用show engines; (show engines\G;)可查看数据库支持的存储引擎 ...
- Mysql 存储引擎中InnoDB与MyISAM差别(网络整理)
1. 事务处理 innodb 支持事务功能,myisam 不支持. Myisam 的运行速度更快,性能更好. 2,select ,update ,insert ,delete 操作 MyISAM:假设 ...
- Mysql 存储引擎中InnoDB与Myisam的主要区别
一直以为我spring事物没有配置好,结果发现是mysql的表本身设置成了Myisam 引擎.改成innodb就支持事物了. 1, 事务处理 innodb 支持事务功能,myisam 不支持. Myi ...
- 【转】Mysql 存储引擎中InnoDB与Myisam的主要区别
1, 事务处理 innodb 支持事务功能,myisam 不支持. Myisam 的执行速度更快,性能更好. 2,select ,update ,insert ,delete 操作 MyISA ...
- mysql存储引擎之innodb学习
innodb引擎特点1.支持事务:支持4个事务隔离级别,支持多版本读. 2.行级锁定(更新时一般是锁定当前行):通过索引实现,全表扫描仍然会是表锁,注意间隙 锁的影响 3.读写阻塞与事务隔离级别有关 ...
随机推荐
- Speed-BI数据分析案例:2016年7月汽车销量排行榜
据中国汽车工业协会统计分析,2016年7月,汽车产销比上月均呈下降,同比呈较快增长.1-7月,汽车产销保持稳定增长,增幅比上半年继续提升. 7月,汽车生产195.96万辆,环比下降4.38%,同比增长 ...
- CentOS 6.6 yum 搭建LAMP环境
CentOS 查看操作系统版本 [root@oa ~]# cat /etc/redhat-releaseCentOS release 6.6 (Final) 参考linux centos yum安装L ...
- python_如何建立包
步骤: (1)包的名称为drawing (2)drawing中建立模块color和shape 视图: 备注: (1) E:/python_script/已经加入到系统变量path中 (2) 建立包时, ...
- python_条件、循环语句
1. python中语句块如何定义: 在Python中,冒号(:)用来标识语句块的开始,块中的每一个语句都是缩进的.当回退到和已经闭合的块一样的缩进量时,就表示当前块已经结束. 默认推荐缩进 ...
- int[] List<int> 排序
; List<,,,,,,}; ,,,,}; List<int> result = allingInts.ToList(); result.Sort(); allingInts = ...
- IIS+WebMatrix 做 PHP 遇到的问题总结
1. URL Binding Failure Webmatrix 解决办法: 用管理员权限运行 WebMatrix 并且 改 Port. 我反正是随便输入了一个 Port 就成功了, 默认的是 610 ...
- RequireJS初探
什么是RequireJS? /* --- RequireJS 是一个JavaScript模块加载器.它非常适合在浏览器中使用, 它非常适合在浏览器中使用,但它也可以用在其他脚本环境, 就像 Rhino ...
- Oracle 11g RAC 第二节点root.sh执行失败后再次执行root.sh
Oracle 11g RAC 第二节点root.sh执行失败后再次执行root.sh前,要先清除之前的crs配置信息 # /u01/app/11.2.0/grid/crs/install/rootcr ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- Codeforce Round #217 Div2
e,妈蛋,第二题被hack了 没理解清题意,- -居然也把pretest过了,- -# A: 呵呵! B:包含任意一个子集的输出NO!,其他输出YES! C:贪心额,类似上次的Topcoder的500 ...