Interview Summary: 10 Questions About MySQL Affairs Common Interview Questions and Answers (FQA)

Learning the relational database MySQL is a good starting point. Most people are used to CRUD in their work. Can you still answer the soul of the interviewer's soul? We need to understand some deeper database fundamentals.

The article is continuously updated every week, and everyone's "three consecutive" is the biggest affirmation to me. You can search the WeChat public account "back-end technology school" for the first time (usually updated one or two earlier than the blog)

After finishing the interview, 10 FAQs (Frequently asked questions) about MySQL transaction and storage engine, you want to know here.

What is a transaction?

A transaction is "a set of atomic SQL queries", or an independent unit of work. If the database engine can successfully apply all the statements of the set of queries to the database, then execute the set of queries. If any of the statements cannot be executed due to a crash or other reasons, all statements will not be executed. That is to say, all the statements in the transaction are executed successfully or failed.

Do you know the transaction control syntax?

BEGIN 或 START TRANSACTION 显式地开启一个事务;
COMMIT / COMMIT WORK二者是等价的。提交事务,并使已对数据库进行的所有修改成为永久性的;
ROLLBACK / ROLLBACK WORK。回滚会结束用户的事务,并撤销正在进行的所有未提交的修改;
SAVEPOINT identifier 在事务中创建一个保存点,一个事务中可以有多个 SAVEPOINT;
RELEASE SAVEPOINT identifier 删除一个事务的保存点;
ROLLBACK TO identifier 把事务回滚到标记点;
SET TRANSACTION 用来设置事务的隔离级别。InnoDB 存储引擎提供事务的隔离级别有READ UNCOMMITTED、READ COMMITTED、REPEATABLE READ 和 SERIALIZABLE

Use common language to talk about the things you understand

To use the banking business as a chestnut, the user lemon has two bank cards, one is the CMBC salary card of the China Merchants Bank, and the other is the ICBC savings card. To the CCB savings card account. Remember that the bank abbreviation here is followed by the name of the corresponding data table. If you ca n’t remember it, let me take care of it for you.

招商银行(CMBC):“存么?白痴!”
中国工商银行(ICBC): “爱存不存!”
中国建设银行(CCB): “存?存不?”
中国银行(BC): “不存!”
中国农业银行(ABC): “啊,不存!”
民生银行(CMSB):“存么?SB!"
兴业银行(CIB):“存一百。”
国家开发银行(CDB):“存点吧!”
汇丰银行(HSBC):“还是不存!”

image

This transfer operation can be simplified into a transaction, including the following steps:

  1. Check if the balance of the CMBC account is greater than 1 million
  2. Subtract 1 million from the CMBC account balance
  3. Add 1 million to ICBC account balance

The following statement corresponds to the creation of a transfer transaction:

START TRANSACTION;
SELECT balance FROM CMBC WHERE username='lemon';
UPDATE CMBC SET balance = balance - 1000000.00 WHERE username = 'lemon';
UPDATE ICBC SET balance = balance + 1000000.00 WHERE username = 'lemon';
COMMIT;

What are the ACID characteristics of transactions?

ACID is actually an English acronym for transaction characteristics, the specific meaning is this:

  • Atomicity (atomicity)
    A transaction must be regarded as an indivisible minimum unit of work. All operations in the entire transaction must either be successfully submitted or failed and rolled back. For a transaction, it is impossible to perform only some of the operations .

  • Consistency (consistency) database is always converted from one consistent state to another consistent state. In the previous example, the consistency ensures that even if the system crashes between the third and fourth statements, the CMBC account will not lose 1 million, otherwise lemon will cry because the transaction is not finally committed, so all the transactions The changes will not be saved in the database.
  • Isolation (isolation)
    Generally speaking, the changes made by a transaction are not visible to other transactions until the final commit. In the previous example, when the third statement is executed and the fourth statement has not yet started, if someone else is also preparing to deposit money to the lemon CMBC account, then he still sees 100 in the CMBC account Million.
  • Durability (durability)
    Once the transaction is committed, the changes made by it will be permanently saved in the database. At this time, even if the system crashes, the modified data will not be lost. Durability is a somewhat vague concept, because there are actually many different levels of durability. Some persistence strategies can provide very strong security, while others are not necessarily. And "there can't be a strategy that can guarantee 100% durability" otherwise what else do you need to backup.

ACID

What is dirty reading, non-repeatable reading, phantom reading?

Dirty reading

Before the data is submitted after transaction A modifies the data, another transaction B reads the data at this time. If there is no control, transaction B reads A to modify the data, and then A changes the data before submitting, then B reads The received data is dirty data. This process is called dirty read.

Dirty reading

Non-repeatable

At a certain time after reading certain data in a transaction, the data read before was read again, but it was found that the data read out had changed or some records had been deleted.

image

Phantom reading

When transaction A reads a range of records according to the query condition, transaction B inserts a new record that satisfies the condition in the range. When transaction A queries the record again according to the condition, a new record that meets the condition will be generated ( Phantom Row)
Phantom reading

What is the difference between non-repeatable reading and magic reading?

  • The point of non-repeatable reading is modification: in the same transaction, under the same conditions, the data read for the first time is different from the data read for the second time. (Because there are other transactions submitted for modification in the middle)
  • The point of phantom reading is to add or delete: In the same transaction, under the same conditions, the "number of records" read out for the first and second time is different. (Because there are other transactions in the middle that have been inserted / deleted)

Do you know the four isolation levels of SQL? What is the specific problem solved?

SQL implements four standard isolation levels, each of which specifies the modifications made in a transaction, which are visible within and between transactions, and which are not. Low-level isolation levels generally support higher concurrent processing and have lower system overhead.
Isolation level

Various isolation levels can solve dirty reads, non-repeatable reads, and phantom reads to varying degrees. Isolation levels have their own strengths and there is no perfect solution. It is a rogue to talk about specific implementations out of business scenarios.

Comparison of isolation levels

Which storage engines in MySQL support transactions?

InnoDB and NDB Cluster storage engines in MySQL provide transaction processing capabilities, as well as other third engines that support transactions.

What is automatic submission?

MySQL defaults to the automatic submission AUTOCOMMITmode. In other words, if a transaction is not explicitly started, each query is treated as a transaction to perform a commit operation.

For transactional tables such as MyISAM or memory tables, the modification AUTOCOMMITwill have no effect. For this type of table, there is no concept of COMMITor ROLLBACK, it can be said that it is equivalent to always in the AUTOCOMMITenabled mode.

Can storage engines be mixed in transactions?

Try not to use multiple storage engines in the same transaction. The MySQL server layer does not manage transactions. The transaction is implemented by the underlying storage engine.

If you mix transactional and non-transactional tables (such as InnoDB and MyISAM tables) in a transaction, there will be no problem under normal commit.

However, if the transaction needs to be rolled back, the changes on the non-transactional table cannot be undone, which will cause the database to be in an inconsistent state. This situation is difficult to repair and the final result of the transaction cannot be determined. Therefore, it is very important to choose the right storage engine for each table.

What are the types of MySQL storage engines?

The most commonly used storage engines are InnoDB engine and MyISAM storage engine. InnoDB is MySQL's default transaction engine.

View the currently supported engines for database tables:

show table status from 'your_db_name' where name='your_table_name'; 
查询结果表中的`Engine`字段指示存储引擎类型。

Features and application scenarios of InnoDB storage engine?

InnoDB is MySQL's default "transaction engine", which is set to handle a large number of short-lived (short-lived) transactions. Most of the short-term transactions are normally submitted and rarely rolled back.

For more information about InnoDB transaction model, refer to the official MySQL manual, and post a link here: https://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html

history

InnoDB in the modern MySQL version is historically called InnoDB plugin. This MySQL plug-in was developed in 2008. It was not until 2010 when Oracle acquired Sun that the release of MySQL5.5 officially replaced the old version of InnoDB with the InnoDB plugin. So far, the "spare tire" has successfully turned into a royal engine for MySQL instead of a plug-in. You see that a plug-in works so hard.

image

Features

Multi-version concurrency control (MVCC) is used to support high concurrency. And achieve four standard isolation levels, through the gap lock next-key lockingstrategy to prevent the emergence of phantom reading.

The engine's table is based on a clustered index, which has high performance for primary key queries. However, its secondary index secondary indexmust include the primary key column in the non-primary key index, so if the primary key column is large, all other indexes will be large. Therefore, if there are many indexes on the table, the primary key should be as small as possible. In addition, InnoDB's storage format is platform independent.

InnoDB has done a lot of optimizations, such as: predictable read-ahead in the way of reading data from disk, adaptive hash index (adaptive hash index) that automatically creates hash indexes in memory to speed up read operations, and those that can accelerate insert operations Insert buffer (insert buffer), etc.

InnoDB supports true hot backup through some mechanisms and tools. Other storage engines of MySQL do not support hot backup. To obtain a consistent view, you need to stop writing to all tables. In a mixed read and write scenario, stopping writing may also mean Stop reading.

What are the characteristics and application scenarios of MyISAM storage engine?

MyISAM is the default storage engine for MySQL 5.1 and earlier. MyISAM provides a large number of features, including full-text indexing, compression, spatial functions (GIS), etc., but MyISAM does not "support transactions and row-level locks", for read-only data, or the table is relatively small, can tolerate repair operations, can still be used it.

characteristic

MyISAM "does not support row-level locking but locks the entire table". A shared lock is added to all tables that need to be read when reading, and an exclusive lock is added to the table when writing. But while the table has read operations, you can also insert new records into the table, which is called concurrent insertion.

MyISAM table can perform inspection and repair operations manually or automatically. However, unlike transaction recovery and crash recovery, it may cause some "data loss", and the repair operation is very slow.

For MyISAM tables, even BLOBand TEXTequal length fields, it can also be based on the first 500 characters to create the index, MyISAM also supports "full-text indexing," which is based on an index word created to support complex queries.

If an DELAY_KEY_WRITEoption is specified , the modified index data will not be written to disk immediately after each modification execution is completed, but will be written to the key buffer in memory, only when the key buffer is cleared or the table is closed Write the corresponding index block to disk. This method can greatly improve the write performance, but when the database or host crashes, it will cause "index damage" and need to perform repair operations.

InnoDB vs. MyISAM

Having said so much, I do n’t remember at a glance. I ’ll give you a table that briefly lists the main differences between the two engines, as shown below.
Engine comparison

Other storage engines

MySQL also supports some other storage engines, such as memory engine, NDB cluster engine, and CSV engine. Since these engines are not commonly used by the aforementioned InnoDB and MyISAM, they are not introduced here. If you are interested, you can go to the MySQL documentation to understand. Here is also an official link: https://dev.mysql.com/doc/refman/5.7/en/storage-engines.html
Engine list

Say a few more words

This article is the basics of MySQL. I try to sort out this piece of knowledge in a form that is easy to understand and combined with charts. The more basic and underlying knowledge, the easier it is to investigate and master the degree. The investigation point, I believe that after reading the MySQL transaction and storage engine should have a relatively complete understanding.

Finally, thank you for reading. The purpose of the article is to share the understanding of knowledge. If there are obvious deficiencies in the text, you are welcome to point out that we will learn together in the discussion.

Originality is not easy. Seeing here move your fingers, everyone's "three links" is the biggest support for my continued creation.

You can search the WeChat public account "back-end technology school" and reply "data" with various programming learning materials I prepared for you. The article is continuously updated every week, see you next time!

References

https://book.douban.com/subject/23008813/

https://juejin.im/post/5c519bb8f265da617831cfff#comment

https://tech.meituan.com/2014/08/20/innodb-lock.html

https://blog.csdn.net/shellching/article/details/8106156

https://coolshell.cn/articles/6790.html

https://zhuanlan.zhihu.com/p/29166694

https://dev.mysql.com/doc/refman/5.7/en/storage-engines.html

https://www.zhihu.com/question/27876575

https://www.runoob.com/mysql/mysql-transaction.html

https://blog.csdn.net/qq_35642036/article/details/82820178?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

https://github.com/CyC2018/CS-Notes/blob/master/notes/MySQL.md#b-tree-principle

Guess you like

Origin www.cnblogs.com/NanoDragon/p/12650049.html