一|一 MYSQL简介

锁粒度

  • 表级锁:MYSQL中最基本的锁策略,拥有最小的开销,锁定整张表。当客户端对表进行写操作时,将获取写锁,同时阻塞其他的读与写操作;当客户端进行读操作时,将获取读锁,不影响其他的读获取锁。
  • 行级锁:提供最优秀的并发能力,同时花费最大的开销。InnoDB 和 XtraDB 存储引擎实现的是行级锁。
事务 ACID
  • Atomicity(原子性):事务必须作为一个单个的不可分割的工作单元,事务中的操作或者都成功,或者回滚。
  • Consistency(一致性):数据库必须总是从一个一致性状态到下一个一致性状态。
  • Isolation(隔离性):一个事务的结果通常是只有这个事务完成时才对其他事务可见。
  • Durability(持久性):一旦事务被提交,其做的改变必须持久化。
隔离等级
  • READ UNCOMMITTED(未提交读):事务可以看到其他未提交事务的结果,也成为脏读。性能并不比其他隔离级别好很多。
  • READ COMMITTED(提交读):也叫不可重复读。大多数数据库的默认隔离级别(MYSQL不是)。一个事务所做的改变只有提交完才对其他事务可见。
  • REPEATABLE READ(可重复读):保证了同一个事务中读取同一行时,读取两次结果是一致的。可能会出现幻读。
  • SERIALIZABLE(串行化):最高隔离级别。强制事务串行化执行,确保了事物间不互相影响。解决了幻读问题。
隔离等级优缺点
Isolation level Dirty reads possible Nonrepeatable reads possible Phantom reads possible Locking reads
READ UNCOMMITTED YES YES YES NO
READ COMMITTED NO YES YES NO
REPEATABLE READ NO NO YES NO
SERIALIZABLE NO NO NO YES
MVCC(multiversion concurrency control) 工作原理:保存数据在某个时间点的快照。
InnoDB的MVCC实现 在每一行存了两个额外的隐藏值:行的创建事务版本号和行的删除事务版本号。
【一|一 MYSQL简介】例:
  • SELECT:InnoDB must examine each row to ensure that it meets two criteria:
    • InnoDB must find a version of the row that is at least as old as the transaction
      (i.e., its version must be less than or equal to the transaction’s version). This
      ensures that either the row existed before the transaction began, or the transaction
      created or altered the row.
    • The row’s deletion version must be undefined or greater than the transaction’s
      version. This ensures that the row wasn’t deleted before the transaction began.
      Rows that pass both tests may be returned as the query’s result.
  • INSERT:
    InnoDB records the current system version number with the new row.
  • DELETE:
    InnoDB records the current system version number as the row’s deletion ID.
  • UPDATE:
    InnoDB writes a new copy of the row, using the system version number for the new
    row’s version. It also writes the system version number as the old row’s deletion
    version.
MYSQL存储引擎 InnoDB
  • 事务
  • 支持表级、行级锁
MYISAM
  • 非事务
  • 只支持表级锁
  • 可对BLOB、TEXT列的前五百个字符进行索引
  • 支持全文索引
Archive
  • 只支持INSERT与SELECT
  • 到MYSQL5.1才支持索引
XtraDB
  • 第三方引擎
  • InnoDB的修改版
改变表的存储引擎
  • ALTER TABLE mytable ENGINE = InnoDB;
    • 最简单的方式
    • 花费时间长:MYSQL会一行一行的拷数据到一个新表中
    • 可能会占满磁盘的I/O
    • 在转换过程中,原始表会被读锁定,阻塞对标数据的读取
  • Dump and import
    • 使用mysqldump先备份表
    • 编辑dump文件,修改表名字和引擎类型
    • mysqldump默认在CREATE TABLE前会写一个DROP TABLE语句,所以执行前要小心
  • CREATE and SELECT
    • 前两种方式的中和
    • 创建新表,并使用INSERT ... SELECT向新表中填充数据,适合数据不是很多时
    mysql> CREATE TABLE innodb_table LIKE myisam_table; mysql> ALTER TABLE innodb_table ENGINE=InnoDB; mysql> INSERT INTO innodb_table SELECT * FROM myisam_table;

    • 当数据较多时,使用增量插入更好一些
    mysql> START TRANSACTION; mysql> INSERT INTO innodb_table SELECT * FROM myisam_table WHERE id BETWEEN x AND y; mysql> COMMIT;

    推荐阅读