MySQL transaction knowledge points finishing

1. Affairs

Transaction is the smallest unit of MySQL execution.
Four characteristics of transactions: atomicity (atomicity or indivisibility), consistency, isolation (also known as independence), and durability.

Question: If a table user(money) performs two operations at the same time, a money-100 and a money+100, and a transaction is started, how can the execution of the transaction ensure atomicity?
Answer: Introduce the temporary table undolog, each transaction has an undolog, the intermediate data is stored in the undolog, write the two operations -100 and +100 to this table, lock them together, and then write the result to the original table together in.


Second, the index

MySQL uses B+ trees for indexing.

Assuming that there are 10,000 pieces of data in the teacher table, how are these data stored in MySQL?
select * from teacher-MySQL data is stored through B+ tree. The values ​​of the B+ tree are stored on the leaf nodes. Add an index on the basis of the
Insert picture description here
table teacher(id, name)
name, then build a tree, use id as a key to build a B+ tree, and use name as a key to build a B+ tree.
When multiple keys are used: Binary tree, rbtree, process or thread also has multiple keys.


Three, row-level locks, table-level locks, page-level locks

When inserting a piece of data into the data, the data must be
placed on the disk: 1) When inserting the table, the table-level lock locks the entire table.
2) For a piece of data to change multiple attributes, row-level locks are used.
3) Page-level lock: The storage of data in the disk is page by page, and a page is 4k. When a table is relatively large (such as tens of millions of data), this time is to lock part of the table data. Only one operation is allowed on a page.


Four, binlog

What data will be stored in binlog?
Every time a SQL statement is executed, it will generally be recorded in the binlog. The specific needs to be distinguished according to the actual situation:
select statements are not stored in binlog , and statements such as insert, update, and delete are stored in binlog.

Binlog function Binlog
can be used to restore some historical operations of the database, such as rollback.
The rollback here is not directly related to the rollback of the transaction. The rollback of the transaction is achieved through undolog.

The database service is stopped when the rollback is performed, and the data may be down at this time.
Insert picture description here


Five, the difference between InnoDB and MyISAM

InnoDB supports transactions, but MyISAM does not support transactions?
MyISAM is a table-level lock, and InnoDB is a row-level lock.

Why does InnoDB not support full-text indexing, but MyISAM supports full-text indexing?
Blog system: We insert an article into the database. When we want to view this article, if the article has 10W+ articles, we can index the article.
InnoDB data is already on the node. If you want to index the full text, it is equivalent to copying a database. It is better to store articles in MyISAM. InnoDB's leaf nodes are used to store articles, which makes the leaves too large and not suitable for storing articles. The full-text index is a summary and MD5 value for query.
Insert picture description here

Guess you like

Origin blog.csdn.net/locahuang/article/details/110353004