[Mysql] interview FAQ ~ Continued

Reprinted Source: probably the best MySQL important points of the whole network / face questions summary

 

table of Contents

1, the difference between relational databases and non-relational databases

2, transaction-related

2.1 four major characteristics of the transaction (ACID)

2.2 concurrent transactions bring what issues?

Magic Reading 2.3 degree difference is not repeated and

2.4 What are the transaction isolation level?

2.5 MySQL's default isolation level

3, index-related

3.1 What is the index can improve query speed

3.2 What is the most left-prefix principle?

3.3 Mysql how to add an index for the table field?

4, related storage engine

4.1 Some commonly used commands

4.2 MyISAM and InnoDB difference

5, lock-related

5.1 difference optimistic locking and pessimistic locking

5.2 other kinds of locks usage scenarios

5.3 optimistic locking two common implementations

Shortcoming 5.4 optimistic locking

5.5 InnoDB locking mechanism and lock algorithm

5.6 share lock and exclusive lock

 

1, the difference between relational databases and non-relational databases

 

2, transaction-related

2.1 four major characteristics of the transaction (ACID)

  • Atomic: A transaction is the smallest unit of execution does not allow division. Operation to ensure atomicity of transactions either completed or totally ineffective;
  • Consistency: Before and after the transaction, the data is consistent, the same results for a plurality of transaction data read is the same;
  • Isolation: concurrent access to the database, a user transaction not interfere with other firms, databases between concurrent transactions are independent;
  • Persistence: a transaction is committed after. It changed data in the database is persistent, even if the database fails nor should it have any impact.

 

2.2 concurrent transactions bring what issues?

  • Dirty read (Dirty read): Read Uncommitted
  • Lost modify (Lost to modify): refers to when a transaction reads a data, another transaction also access the data, then modify the data in this first transaction after the second transaction also modify this data. Such modifications result in a transaction first one is lost, the loss of so called modified. For example: Transaction 1 reads a data table A = 20, A = read transaction 2 also 20, a modified transaction A = A-1, A modify transaction 2 = A-1, A = the final result. 19, transaction Amendment 1 is lost.
  • Non-repeatable read (Unrepeatableread): means within a transaction reads the same data multiple times. When this transaction is not over, another transaction also access the data. So, between the two read data in the first transaction, due to the modification of the second transaction led to the first transaction data may be read twice not the same. This happened twice within a transaction read data is not the same situation, so called non-repeatable read.
  • Magic Reading (Phantom read): Magic Reading and unrepeatable reads the like. It occurs in a transaction (T1) is read several lines of data, followed by another concurrent transaction (T2) is inserted into some of the data. In the following query, the first transaction (T1) will find more than a few original records do not exist, as if the same happened hallucinations, so called phantom reads.

 

Magic Reading 2.3 degree difference is not repeated and

  • Non-repeatable read focus is modified, the new focus is phantom read or delete.

 

2.4 What are the transaction isolation level?

SQL standard defines four levels of isolation:

  • READ-UNCOMMITTED (read uncommitted): the lowest level of isolation, changes have not been allowed to read the data submitted, may cause dirty reads, non-repeatable reads or phantom reads.
  • READ-COMMITTED (read committed): allow concurrent transactions to read data already submitted, can prevent dirty reads, but phantom reads or non-repeatable read may still occur.
  • REPEATABLE-READ (repeatable read): multiple reads the results of the same field are the same, unless the data is modified their affairs themselves, you can prevent dirty reads and non-repeatable reads, but phantom reads still occur.
  • SERIALIZABLE (serialization): the highest level of isolation, full compliance ACID isolation levels. All transactions executed one by one in sequence, it is impossible to produce interference between such matters, that is to say, this level prevents dirty reads, non-repeatable reads and phantom reads.

   

 

2.5 MySQL's default isolation level

Supported by default isolation level MySQL InnoDB storage engine is REPEATABLE-READ (can be re-read). View isolation level command is as follows.

mysql> SELECT @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+

Note: the difference is that SQL standard InnoDB storage engine ** REPEATABLE-READ (can be re-read) the use of lower transaction isolation level is the Next-Key Lock lock algorithm, thus avoiding to generate magic of reading, which with other database systems ( such as SQL Server) are different. So the default isolation level support InnoDB storage engine is REPEATABLE-READ (can be re-read) has been completely isolated to ensure the requirements of the transaction.

Because the lower the isolation level, the less transaction requests a lock, so most of the database system isolation level is READ-COMMITTED (read submission) :, but you know that InnoDB storage engine is used by default ** REPEATABLE-READ (can be re-read) ** does not have any performance loss.

InnoDB storage engine in the case of a distributed transaction will generally be used ** SERIALIZABLE (serialization) ** isolation level.

 

3, index-related

3.1 What is the index can improve query speed

Reference Address: https://juejin.im/post/5b55b842f265da0f9e589e79   Author: Java3y

 

3.2 What is the most left-prefix principle?

MySQL in the index may reference multiple columns in a certain order, such an index called the joint index. Such as the User table name and city plus joint index is (name, city), and the most left-prefix principle means that when the query if the query exactly matches left index continuous one or several columns, this column can be used to. as follows:

select * from user where name=xx and city=xx ; //可以命中索引
select * from user where name=xx ; // 可以命中索引
select * from user where city=xx ; // 无法命中索引            


It should be noted that, when queried if two conditions are used, but in a different order, such as city = xx and name = xx, so now query engine automatically optimizes order to match the joint index, so is the ability to hit index.

Since the most left-prefix principle, when you create a joint index, the order of index fields need to consider the number of field values ​​after de-emphasis, put more front. ORDER BY clause can also follow this rule.

 

3.3 Mysql how to add an index for the table field?

 

4, related storage engine

4.1 Some commonly used commands

 

4.2 MyISAM and InnoDB difference

MyISAM is the default MySQL database engine (before version 5.5). Although the excellent performance, but also provides a number of features, including full-text indexing, compression, spatial functions, etc., but MyISAM does not support transactions and row-level locking, and after the collapse of the biggest flaw is not safe recovery. However, after version 5.5, MySQL introduced InnoDB (transactional database engine), the default version of MySQL 5.5 storage engine is InnoDB.

Both of comparison:

  • Supports row-level locking: MyISAM only table-level locking (table-level locking), while InnoDB supports row-level locking (row-level locking) and table-level locking, defaults to row-level locking.
  • Whether to support the transaction and to restore security after a crash: MyISAM stressed that the performance of each query be atomic, which performs several times faster than InnoDB type, but does not provide transaction support. But InnoDB provides transaction support advanced database features affairs, external keys. With a transaction (commit), transaction-safe rollback (rollback) and crash repair capacity (crash recovery capabilities) of (transaction-safe (ACID compliant)type table.
  • Whether to support MVCC: Only InnoDB support. Cope with high concurrent transactions, MVCC is more efficient than simply locking; MVCC only work in READ COMMITTED and REPEATABLE READ isolation level two; MVCC can use optimistic (optimistic) and pessimistic locking (pessimistic) lock is achieved; each database MVCC implementation is not uniform. Recommended reading: MySQL-InnoDB-MVCC Multi Version Concurrency Control
  • Whether to support full-text indexing: MyISAM support, InnoDB does not support.
  • Whether to support foreign keys: MyISAM does not support, and InnoDB support.

 

5, lock-related

 

5.1 difference optimistic locking and pessimistic locking

  • Pessimistic lock: is assuming the worst case, get a time when data modification think others will, so every time she took the data will be locked, so people want to take this data will be blocked until it got the lock ( a time to a shared resource threads, other threads are blocked, and then run out the transfer of resources to other threads). Traditional relational database inside to use a lot of this locking mechanism, such as row locks, table locks, etc., read lock, write lock, are locked before doing the first operation. Implementation of the Java synchronized and in ReentrantLock and other exclusive lock is pessimistic locking thought.
  • Optimistic locking : always assume the best case, every time the data are to pick up other people think is not modified, it will not be locked, but when the update will determine what others during this time did not go to update the data, You can use the version number CAS mechanisms and algorithms. Optimistic locking is suitable for the types of applications to read, which can improve throughput, like similar write_condition mechanisms provided by the database, in fact, optimistic locking is provided. Java.util.concurrent.atomic package in Java following atomic variable classes is the use of one implementation of the optimistic locking CAS achieved.

 

5.2 other kinds of locks usage scenarios

  • Pessimistic locking: apply to write the next scene. Because the situation will often write more general conflict, which leads to the upper application will continue to be retry, such but rather reduces the performance
  • Optimistic lock: for multi-read scenario (written less), that is, when the conflict is really rare, so the lock can save the cost, increase the overall throughput of the system.

5.3 optimistic locking two common implementations

  • The version number of mechanisms

Typically plus a data version number is the version field in the data table represents the number of data is modified, and when data is modified, plus a version value. When a thread A to update the data values ​​will be read version value while reading data, when submitting the update, if just to read the version when the update is version equal value in the current database, or retry update until the update is successful.

Take a simple example:

Account information database table has a version field, a current value of 1; field and the current account balance (Balance) is $ 100.

At this time, the operator A to read out (version = 1), and deducting the $ 50 ($ 100- $ 50) from the account balance.
During operation of the operator A, the operator B also reads the user information (version = 1), and deducted from the account balance is $ 20 ($ 100- $ 20).
The operator A complete revision of the data version number plus one (version = 2), together with the account deductions balance (balance = $ 50), committed to the database update, this time due to submit data version is greater than the database records the current version of the data is updated database record is updated to version 2.
The operator B to complete the operation, but also the version number plus one (version = 2) try to submit data (balance = $ 80) to the database, but this time than the discovery of the database record version, data version number of the operator B, filed 2 , the database also records the current version 2, does not meet the "submit version must be greater than the current version of the record in order to perform the update," the optimistic locking strategy, therefore, the operator B submission was rejected.
Thus, to avoid the operator B with the cover of the operation result of operator A may be based on the old version = 1 modified data results.

  • CAS algorithm

I.e., compare and swap (compare and swap) is a well-known lock-free algorithms. Lock-free programming, that is, when not in use lock variable synchronization between multiple threads, which is synchronized variable in the absence of the thread is blocked, it is also called non-blocking synchronization (Non-blocking Synchronization). CAS algorithm involves three operands

  • The need to read and write memory value V
  • Comparing value A
  • B intends to write the new value

If and only if the value is equal to V A, CAS B with a new value by updating the value of V atomic manner, it will not perform any operations (compare and substitutions are an atomic operation). Normally a spin operation, i.e. continuously retries.

About spin locks, you can look at this article, very good: "interview must-depth understanding of spin lock"

 

Shortcoming 5.4 optimistic locking

  • ABA problem

If a variable V when the initial read is the value of A, and checks to the assignment at the time of preparation it is still value A, then we will be able to explain its value has not been revised yet another thread? Obviously not, because during that time its value may be changed to other values, and then back to A, that CAS operation will mistakenly believe that it has never been modified. This problem is known as CAS operations "ABA" problem.

JDK 1.5 after AtomicStampedReference class provides such a capability, which is compareAndSet method first checks whether the current reference is equal to the expected reference and the current mark is equal to the expected flag, if all equal Atomically the flag and the reference value to the given updated value.

  • Large overhead long cycle time

Spin CAS (that is, the cycle has been unsuccessful execution until it succeeds) if not successful for a long time, will bring a very large execution overhead CPU. If the JVM can support pause instructions provided by the processor so there will be some efficiency improvement, pause command serves two purposes, first it may delay pipelined execution of instructions (de-pipeline), the CPU does not consume too many resources for implementation, delay time implementation dependent, the delay time on the number of processors is zero. It can avoid a second exit loop when the order due to memory conflicts (memory order violation) caused by CPU pipeline is cleared (CPU pipeline flush), in order to improve the efficiency of the CPU.

  • Atomic operation can only guarantee a shared variable

CAS valid only for a single shared variable, when the operation involves a plurality of shared variables across CAS invalid. But from the beginning of JDK 1.5, provides AtomicReference class to ensure atomicity references between objects, you can put multiple variables on an object in the CAS to operate, so we can use the lock or use multiple shared variables like AtomicReference combined into a shared variable to operate.

 

5.5 InnoDB locking mechanism and lock algorithm

MyISAM and InnoDB storage engine to use locks:

  • MyISAM uses table-level lock (table-level locking).
  • InnoDB supports row-level locking (row-level locking) and table-level locking, defaults to row-level locking

Table level lock, and row-level locks comparison:

  • Table-level lock: Mysql locked in a maximum particle size of the lock, the current operation of the entire table lock, simple, resource consumption is relatively small, fast locking, deadlock will not occur. Its maximum size lock trigger lock conflict highest probability minimum degree of concurrency, MyISAM and InnoDB engines support table-level locking.
  • Row-level locks: Mysql smallest granularity locking in a lock for locking operation only for the current row. Row-level locking conflict can greatly reduce database operations. Locking its minimum size, high concurrency, but also the largest locking overhead, locking slow, there will be a deadlock. Details can refer to: Mysql lock mechanism is simple look

Lock algorithm InnoDB storage engine, there are three:

  • Record lock: locks on individual rows
  • Gap lock: gap lock, a lock range, the records themselves are not included
  • Key-Lock the Next: Record a lock range + GAP, comprising the records themselves

Knowledge Point:

  • For queries using innodb line of next-key lock
  • Next-locking keying order to solve the problem of phantom read Phantom Problem
  • When the index query contains a unique attribute, the next-key lock downgraded to record key
  • Gap lock design purpose is to prevent multiple transactions to insert records into the same range, and this will lead to phantom read problems
  • There are two ways to close the gap explicit lock :( addition unique and foreign key constraints checks, using only the remaining record lock) A. The transaction isolation level is set to RC B. The parameter is set to 1 innodb_locks_unsafe_for_binlog

 

5.6 share lock and exclusive lock

  • Shared lock (S lock): also known as a read lock, if the transaction data object A plus T S lock, can only be read transaction T A; other transactions can only lock again A plus S, plus X can not lock, S T until the lock is released on a. This ensures that other transactions can be read A, but not make any changes before the A S T release lock on A.
  • Exclusive lock (Exclusive lock, abbreviated as X lock): also known as a write lock, if the transaction data object T to A plus X lock, only allows T to read and modify A, no other transaction can no longer on the A add any type of lock, release lock on until T a. It prevents any other transaction to acquire the lock on the resource until the end of the transaction's original lock on the resources released. During update operations (INSERT, UPDATE or DELETE) always applies an exclusive lock.

The difference between the two:

  • Share locks (S locks): If the transaction data T to A plus shared lock, other transactions can only share plus A lock can not add exclusive lock. Acquire shared locks affairs can only read data, the data can not be modified.
  • Exclusive lock (X lock): If the transaction data T to A plus exclusive lock, other transactions can not be applied to any of the A block of any type. Transaction data acquired exclusive lock can not only read, but also to modify the data

 

Published 44 original articles · won praise 16 · views 10000 +

Guess you like

Origin blog.csdn.net/YYIverson/article/details/100937821