mysql5.7开始默认存储引擎是Innodb,除非指定其他默认存储引擎。
    Innodb的优势
    1/支持事务
    2/行级锁
    3/在磁盘上按照主键排列以优化查询
    4/提供数据的完整性,支持外键

    Innodb最佳实践
    1/每张表都指定一个主键,主键可以是业务键,也可以是自增的的id列。
    2/关闭自动提交(autocommit),每秒提交几百次会影响性能。
    3/不要使用LOCK TABLE,Innodb不会牺牲可靠性与高性能去处理一张表上的并发的读写。
    4/打开innodb_file_per_table变量,或者使用一般表空间来存储数据跟索引,而不是将他们存在系统表空间中。这个变量默认是打开的。
    5/可以压缩Innodb的表而不牺牲读写性能。

    查看默认存储引擎的两种方法:

    1. mysql> show engines;
    2. +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    3. | Engine | Support | Comment | Transactions | XA | Savepoints |
    4. +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    5. | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
    6. | CSV | YES | CSV storage engine | NO | NO | NO |
    7. | MyISAM | YES | MyISAM storage engine | NO | NO | NO |
    8. | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
    9. | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
    10. | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
    11. | ARCHIVE | YES | Archive storage engine | NO | NO | NO |
    12. | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
    13. | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
    14. +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    15. 9 rows in set (0.01 sec)
    1. mysql> select * from information_schema.engines;
    2. +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    3. | ENGINE | SUPPORT | COMMENT | TRANSACTIONS | XA | SAVEPOINTS |
    4. +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    5. | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
    6. | CSV | YES | CSV storage engine | NO | NO | NO |
    7. | MyISAM | YES | MyISAM storage engine | NO | NO | NO |
    8. | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
    9. | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
    10. | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
    11. | ARCHIVE | YES | Archive storage engine | NO | NO | NO |
    12. | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
    13. | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
    14. +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
    15. 9 rows in set (0.01 sec)

    设置默认存储引擎
    mysql5.7默认的存储引擎是Innodb
    1/可以在配置文件中的[mysqld]组中添加—default-storage-engine=InnoDB
    2/在启动时,添加—default-storage-engine=InnoDB
    即使设置了默认的存储引擎,也可以创建其他引擎的表,只需在CREATE TABLE 时添加ENGINE=other_engine_name

    修改表的存储引擎

     ALTER TABLE table_name ENGINE=InnoDB;
    

    Innodb 多版本(multi-version)
    Innodb是一个多版本的存储引擎。他会保留老版本行的改变信息来保证事务的特征,像并发,回退等。这些信息保留在系统表空间或者undo表空间,在数据结构上来说,叫回退段。

    Innodb每行增加三个区域
    1/一个6byte的DB_TRX_ID,表明了上一个事务在这一行上的id。删除语句在innodb内部被看作update,行中特殊的位置被设置为已删除。
    2/一个7byte的DB_ROLL_PTR,这是一个指针,指向了undo段中undo记录。
    3/一个6byte的DB_ROW_ID。当新行插入时,它会无变化的增加。如果innodb自动生成了一个聚簇索引(创建表时没有指定主键,Innodb会自动创建一个主键索引),索引就是这个ROW_ID的值。除此之外,row_id不会出现在其他索引上。

    回退段中的undo 日志会被分成两部分,一部分是INSERT,另一部分是UPDATE。INSERT的undo 日志只会在事务回退时用到,当事务提交时,回退段中的INSERT undo部分就会被丢弃。UPDATE undo日志用来确保一致性读。update undo 日志只有在不存在分配快照之后才会被删除。在一致性读中,事务可能需要更新undo段中的数据来达到早期的版本数据。

    一般的delete语句,Innodb多版本中不会直接删除磁盘上的数据,只有等到undo段中这一行被标记成delete时,才会删除相应的数据以及索引。