Stand-alone storage engine to the mysql REFLECTIONS

Last article we introduce mysql storage engine and thus lead to the physical structure of an index of
thinking
but concurrent transactions and locking mechanism is like, let's explore

How a sql statement is executed

•连接器: Authentication and permissions related (MySQL login time).
•查询缓存: Time to execute a query, it will first check the cache.
•分析器: There is no cache hit, then, it will go through the SQL statement parser, analyzer it means to look at your SQL statement to be doing, and then check your SQL statement syntax is correct.
•优化器: According to MySQL believe that the best solution to do it.
•执行器: Execute a statement, then returns the data from the storage engine.

Transaction concurrency problem?

MySQL enabled by default auto-commit every execute a sql statement automatically commits the transaction. In MySQL transaction must first turn off the automatic submission, use manual submission of atomic execute multiple sql statements. When more transactions, concurrency problems will emerge.

Lock and MVCC

Lock divided into exclusive locks and shared locks, specifically, write locks and read locks (lock in share mode, shared read locks, not writable)
older versions of MySQL MyISAM engine only supports the use of table lock, no matter that a thread operations data that ever this table lock, other threads may only operate without any influence on the line can not be performed at the same time, only waiting to acquire the lock. The new version of the engine is reduced InnoDB lock granularity, the lock support lines, data lines which use the lock give the corresponding row, as long as the operation independent of other threads can operate simultaneously with the locking row, concurrent increased efficiency .
There InnoDB意向锁,索引行锁,间隙锁

lock Explanation
Intent locks When a lock to advance declaration of intent, and the intent to acquire a table-level lock, if the acquisition is successful, it will be later or is (are allowed), locked up some of the rows of the table.
Index row lock Examples: execute an SQL statement SELECT sid FROM table_student WHERE sid = 123; if sid index exists (the InnoDB automatically creates a clustered index based on the master key), then the index of the corresponding lock line prevents the retrieval of other threads in the row to modify. If the field is not indexed, it will lock the entire table.
Lock gap The default is to use MySQL under REPETABLE READ default isolation level of things, the isolation level will occur as a phantom read problem. Thus in order to prevent phantom read InnoDB problem with the locking gap (Gap locks). To the range of the clearance gap field locking latch popular terms that a range of query data, preventing insertion phantom lines.

next-key is a combination lock and the gap index row locks, mainly to prevent phantom read
1), if the update condition is not taking the index, this time will be a full table scan, when the scan table, to prevent any of the other update operation, it rose to a table lock.
2), if the update conditions index field, but not the only index (including primary key index), then the time will be updated using the Next-Key Lock. The reason for using Next-Key Lock of:
A), we must first ensure plus an exclusive lock on the records meet the criteria, will lock the value of the current non-unique index and the corresponding primary key index;
b), but also to ensure the lock interval can not be inserted the new data.
3) If the conditions for a unique index update, use the Record Lock (recording lock).
We note that the above lock on the ordinary select.....is invalid, belong to a snapshot reading this, I will explain later. RR isolation level it select...from...lock in share modecan increase the gap to avoid phantom reads.
Magic Reading performance: a transaction (with a read view) before and after the two queries in the same range when, after a query to see the previous query does not see the line.
For phantom read two caveats:
1, the repeatable read isolation level, the common query is a snapshot reading, will not see another transaction inserts data, Magic reading only appear in the current reading.
2, phantom read refers specifically to the newly inserted row, read the results of the original update not exist line. Because the current reading is the role can read all the latest recorded values have been submitted.

Of course there is a magic solution to read, is MVCC (MVCC multi-version concurrency control), under the sub-read transaction isolation level RR will save a Read View (snapshots) , first read form Read View, the second will follow this Read View. And every time a new form of RC Read View under conditions of isolation.

Read the above next-key lock everyone has a taste of flair, but will have to wait for a lock. MVCC added to each of two implicit list, wherein a modified memory line is "time", to delete another column stores "time"
the RR isolation level
Select
modified version a) is less than or equal to the row number transaction.
delete the version number b) line either not defined, or greater than the version number of the transaction.
Insert、Update、Delete
Are used to update the version number, not go into here.

We think about the line of the revision number is less than or equal to the transaction number and delete the version number of the line are either not defined, or greater than the version number of the transaction is not already well avoid the phantom reads.

Troubleshooting

Then our mysql how to roll back the transaction, and how to restore data when it is down, as well as master-slave replication is how to achieve, we will say that through the log, which log each specific that it corresponds.

Released seven original articles · won praise 45 · views 8511

Guess you like

Origin blog.csdn.net/xk4848123/article/details/105195921