Interview -> MySQL

  • A: Transaction

    • Concept: A transaction is a set of operations to meet the ACID properties, may submit a transaction by the commit, rollback may be used for rollback
      • Atomicity (Atomicity)
        • The transaction is considered the smallest indivisible unit, operation of its services either submit all succeed or all fail rollback
        • Rollback can be used to achieve rollback logs, log roll back office operations performed by the modified reverse to perform these modifications upon rollback
      • Consistency (Consistency)
        • Database before and after the transaction execution are consistent state in a consistent state, the result of a transaction reads all data is the same
      • Isolation (Isolation)
        • A firm make changes before final submission, other transactions are not visible
      • Persistent (Durability)
        • Once the transaction commits, then modify it done will always be saved to the database, even if the system goes down, the result of transaction execution can not be lost, redo logs to ensure persistence
    • ACID properties of the transaction concept is simple, but difficult to understand, mainly because of these features is not a grade level relations MySQL defaults to auto-commit mode ( AUTOCOMMIT), that is to say, if you do not show the use of  START TRANSACTION statement to start a transaction then each query will be treated as a transaction automatically submitted
      • 1, only to meet the consistency, the results of the transaction is correct
      • 2, in uncomplicated cases, serial execution of a transaction, isolation, be able to meet, as long as this time to meet the atomicity, consistency will be able to meet the
      • 3, in the case of concurrent, parallel execution of multiple transactions, the transaction must not only meet the atomicity, isolation also need to meet in order to meet consistency
      • 4, the transaction is to be able to meet the persistence database downtime to deal with the situation
  • II: Concurrent consistency problem

    • In a concurrent environment, transaction isolation is difficult to ensure, so there will be many concurrent issue of consistency
      • Missing data
        • Two transactions T1 and T2 while a data modification, to modify T1, T2 subsequently modified, T2 to T1 is modified cover the modifications
      •  Read dirty data
        • Modify a data T1, T2 then reads the data, if T1 withdrew this amendment, then the T2 data is read dirty data
      • Non-repeatable read
        • A read data T2, Tl modifications made to the data, if the data T2 is read, and the read results of the measurement result of the first reading is different
      • Magic Reading
        • Read data in a range T1, T2 insert new data within this range, read data T1 within this range again, this time reading the results and the results of the first reading different
    • The main reason is complicated by inconsistencies undermined the transaction isolation, the solution is through concurrency control to ensure isolation. Concurrency control may be achieved by blocking, but requires the user to control the operation block, it is very complex. Database management system provides transaction isolation level, allowing users to more convenient concurrency consistency.
  • Three: Lock

    • MySQL provides two lock granularity: row-level locking, table-level locking
    • You should try to lock only need to change the portion of the data, but not all of the resources, the smaller the amount of data locked, the less the chance of lock contention, the system naturally higher degree of concurrency
    • However, locking resources may be consumed, various operations of the lock (acquire the lock, the lock is released, and checking the state) will increase overhead, thus the smaller the lock granularity, the greater the system overhead, so the choice of locking particles when degrees, need to make a trade-off between the degree of concurrency and locking overhead
    • Lock Type
      • Exclusive lock (Exclusive), abbreviated as X lock, also known as a write lock, shared lock (Shared), abbreviated as S lock, also known as a read lock
        • There are two requirements below
          • A data object of a transaction plus X lock, can be read and update the A, other transactions can not lock during lock A plus any
          • A data object of a transaction plus S locks, read operations can be A, but can not update operations, other transactions can lock during lock on the add S A, but can not add X lock
        • Lock compatible relationship
          • as follows
      • Intent lock (Intention Locks)
        • Use intent locks can more easily support multi-granularity
        • In the presence of row-level locking and table-level locking, transaction T wants to lock table A plus X, you need to check to make sure other transactions or for any row of Table A Table A, locking that is required for each row of table a detection time, which is very time-consuming operation
        • Original intent lock on X / S locks introduced IX / IS ,, IX / IS table lock is used to represent a transaction you want to lock or S X plus lock on a data table row, there the following two provisions
          • A transaction before obtaining an S lock rows of data objects, you must first obtain a list of IS or stronger lock lock
          • A transaction before obtaining an X lock rows of data objects, you must first obtain a lock table IX
        • By introducing intent locks, transaction T wants to lock table A plus X, simply must first detect whether there are other matters on the table A plus X / IX / S / IS lock, if it means adding the other transaction is using this table or a table row lock, thus locking failure transaction T + X
        • Lock is compatible with a variety of relationships
          • as follows
        • Explanation:
          • IS is compatible between any / IX lock, because they just want to express to the table lock, rather than a true lock
          • S lock is only compatible with the IS lock, that transaction T wants to add to the data line S lock, other transactions can already acquire S lock on the table or rows in a table
    • Locking Protocol
      • A locking protocol
        • X lock must be added to modify the transaction data T A, T is not released until the end of the lock
        • Can solve the problem of lost modify, because you can not have two simultaneous transactions on a data modification, the modification of the transaction will not be covered
        • as follows
      • Two locking protocol
        • On the basis of a request to add must read data latch S A, S scanned and immediately release the lock
        • Read dirty data can solve the problem, because if a transaction to modify the data in a heap A, a lock according to the agreement, will add X lock, then it can not be combined with S-lock, that is not read into the data
        • as follows
      • Tertiary lock agreement
        • In the secondary, based on the requirements needed to be added S locks when reading data A, until the end of the transaction to release the S lock
        • You can not solve the problem repeatable read, because reading A, other transactions can not lock A plus X, thus avoiding a change in the data during reading
        • as follows
      • Two-phase locking protocol
        • Locking and unlocking is divided into two phases
        • Serializable scheduling means, by concurrency control, so that the result of the transaction with the transaction concurrently executing the same results with a serial execution. Follow two lock transaction protocol is sufficient conditions serializable scheduling, for example to satisfy the following two-phase locking protocol that serial scheduling
          • lock-x(A)...lock-s(B)...lock-s(C)...unlock(A)...unlock(C)...unlock(B)
        • But not required, for example, the operation does not satisfy the two-phase locking protocol, but it can also be serialized schedule
          • lock-x(A)...unlock(A)...lock-s(B)...unlock(B)...lock-s(C)...unlock(C)
    • MySQL implicit and display lock
      • MySQL's InnoDB storage engine uses two-phase locking protocol, it will automatically lock when needed according to the isolation level, and all locks are released at the same time, this is known as an implicit lock
      • InnoDB can also use a specific display lock statement
        • SELECT ... LOCK In SHARE MODE;
        • SELECT ... FOR UPDATE;
  • 四:隔离级别

    • 未提交读(READ UNCOMMITTED)
      • 事务中的修改,即使没有提交,对其它事务也是可见的。
    • 提交读(READ COMMITTED)
      • 一个事务只能读取已经提交的事务所做的修改。换句话说,一个事务所做的修改在提交之前对其它事务是不可见的
    • 可重复读(REPEATABLE READ)
      • 保证在同一个事务中多次读取同样数据的结果是一样的
    • 可串行化(SERIALIZABLE)
      • 强制事务串行执行
      • 需要加锁实现,而其它隔离级别通常不需要
    • 四者兼容如下
  • 五、多并发并发控制

    • 多版本并发控制,是MySQL的InnoDB存储引擎实现隔离级别的一种具体方式,用于实现提交读和可重复读这两种隔离级别,而未提交读隔离级别总是读取最新的数据行,无需MVCC,可串行化隔离级别需要对所有读取的行都加锁,单纯使用MVCC无法实现
    • 版本号
      • 系统版本号:是一个递增的数字,每开始一个新的事务,系统版本号就会自动递增
      • 事务版本号:事务开始时的系统版本号
    • 隐藏的列
      • MVCC 在每行记录后面都保存着两个隐藏的列,用来存储两个版本号
        • 创建版本号:指示创建一个数据行的快照时的系统版本号
        • 删除版本号:如果该快照的删除版本号大于当前事务版本号表示该快照有效,否则表示该快照已经被删除了
    • Undo 日志
      • MVCC 使用到的快照存储在 Undo 日志中,该日志通过回滚指针把一个数据行(Record)的所有快照连接起来
    • 实现过程
      • 以下实现过程针对可重复读隔离级别
      • 当开始一个事务时,该事务的版本号肯定大于当前所有数据行快照的创建版本号,理解这一点很关键。数据行快照的创建版本号是创建数据行快照时的系统版本号,系统版本号随着创建事务而递增,因此新创建一个事务时,这个事务的系统版本号比之前的系统版本号都大,也就是比所有数据行快照的创建版本号都大
  • 六:存储引擎

    • InnoDB
      • InnoDB是 MySQL 默认的事务型存储引擎,只有在需要它不支持的特性时,才考虑使用其它存储引擎
      • 实现了四个标准的隔离级别,默认级别是可重复读(REPEATABLE READ)。在可重复读隔离级别下,通过多版本并发控制(MVCC)+ 间隙锁(Next-Key Locking)防止幻影读
      • 主索引是聚簇索引,在索引中保存了数据,从而避免直接读取磁盘,因此对查询性能有很大的提升
      • 内部做了很多优化,包括从磁盘读取数据时采用的可预测性读、能够加快读操作并且自动创建的自适应哈希索引、能够加速插入操作的插入缓冲区等
      • 支持真正的在线热备份。其它存储引擎不支持在线热备份,要获取一致性视图需要停止对所有表的写入,而在读写混合场景中,停止写入可能也意味着停止读取
    • MyISAM
      • MyISAM设计简单,数据以紧密格式存储。对于只读数据,或者表比较小、可以容忍修复操作,则依然可以使用它
      • 提供了大量的特性,包括压缩表、空间数据索引等
      • 不支持事务
      • 不支持行级锁,只能对整张表加锁,读取时会对需要读到的所有表加共享锁,写入时则对表加排它锁。但在表有读取操作的同时,也可以往表中插入新的记录,这被称为并发插入(CONCURRENT INSERT)
      • 可以手工或者自动执行检查和修复操作,但是和事务恢复以及崩溃恢复不同,可能导致一些数据丢失,而且修复操作是非常慢的
      • 如果指定了 DELAY_KEY_WRITE 选项,在每次修改执行完成时,不会立即将修改的索引数据写入磁盘,而是会写到内存中的键缓冲区,只有在清理键缓冲区或者关闭表的时候才会将对应的索引块写入磁盘。这种方式可以极大的提升写入性能,但是在数据库或者主机崩溃时会造成索引损坏,需要执行修复操作
    • 比较
      • 事务:InnoDB 是事务型的,可以使用 Commit 和 Rollback 语句
      • 并发:MyISAM 只支持表级锁,而 InnoDB 还支持行级锁
      • 外键:InnoDB 支持外键
      • 备份:InnoDB 支持在线热备份
      • 崩溃恢复:MyISAM 崩溃后发生损坏的概率比 InnoDB 高很多,而且恢复的速度也更慢
      • 其它特性:MyISAM 支持压缩表和空间数据索引
  • 七:主从复制

    • 主要涉及三个线程:binlog 线程、I/O 线程和 SQL 线程
    • binlog 线程 :负责将主服务器上的数据更改写入二进制日志(Binary log)中
    • I/O 线程 :负责从主服务器上读取二进制日志,并写入从服务器的中继日志(Relay log)
    • SQL 线程 :负责读取中继日志,解析出主服务器已经执行的数据更改并在从服务器中重放(Replay)
  • 八:读写分离

    • 主服务器处理写操作以及实时性要求比较高的读操作,而从服务器处理读操作
    • 读写分离能提高性能的原因在于读写分离常用代理方式来实现,代理服务器接收应用层传来的读写请求,然后决定转发到哪个服务器
      • 主从服务器负责各自的读和写,极大程度缓解了锁的争用
      • 从服务器可以使用 MyISAM,提升查询性能以及节约系统开销
      • 增加冗余,提高可用性

Guess you like

Origin www.cnblogs.com/idoljames/p/11420908.html