"Interview Heart Sutra" MySQL Foundation

One leaf is blind, but I don’t see Offer

Preface

MySQL is an essential part of today's world of back-end technology. Almost all back-end technical interviewers will cross-examine the interviewees with no dead ends around the principles and usage of MySQL. The cross-examination from MySQL may be late, but it will never be absent. Old Na couldn't bear to see the self-confident eyes dimmed by the interviewer. On a dark and windy night for a month, I finally made up my mind to collect and sort out Redis interview materials, and write the "Interview Heart Sutra" series, hoping to universalize all living beings.

Note: This question and answer is based on MySQL 8.0

Interview begins

A sloppy bald middle-aged man walks towards you holding a scratched Mac. Looking at your sparse hair, thinking I fuck, this is at least a senior architect. But those of us who have watched "Interview Heart Sutra" can still be calm and stable

Insert picture description here

Hello, boy. It is written on your resume that MySQL is used in the project. Please summarize what MySQL is in one sentence?

At this time, you couldn't help cursing inwardly, your head was bald, and the questions asked were so ordinary, just this? But you cannot show it.

Seriously answer: MySQL is a relational database management system developed and maintained by Oracle Corporation

The interviewer did not look up, and then asked:

What is a MySQL index? What is the use?

Index (Index) is a data structure that can help MySQL obtain data efficiently

This is easy to understand. If MySQL is a book, an article in it is like a piece of data, and the index is the book's table of contents. If you want to read an article in the book, you can open the table of contents and find The page number of the article, and then directly turn to the corresponding page number to read. However, if there is no table of contents, you have to start from the first page and search back page by page until you find the article you want to stop searching and read. (If the article you want to read happens to be on the last page of the book, then you have to search the entire book, the efficiency can be imagined)

Boy, do you know what kinds of MySQL indexes are there?

Coherent replied: There are four common index, namely: PRIMARY KEY (primary key index), UNIQUE (unique index), INDEX (general index) and FULLTEXT (full-text index), which is the use of several B-tree implementation

It is best to briefly introduce the difference between these indexes

Note: B+ tree is a kind of B tree

If you want to highlight your own, you can also say: In addition, the use of R-tree implementation of spatial data types of indexes , MEMORY tables also support hash indexes

If you want to make people feel that your bones are wonderful, you can say: In addition, MySQL also supports multi-column indexes , Invisible Indexes (invisible indexes) , Descending Indexes (descending indexes)

The interviewer slowly raised his head, saw his eyes narrowed slightly, and continued to ask:

You mentioned the B-tree just now, so what is the difference between the B-tree balanced binary tree? Why not implement the index with a balanced binary tree?

Answer: A balanced binary tree is a binary tree with a left and right tree height ≤ 1, and a B-tree is a polytree. Since each node of the B tree can store multiple elements, the tree height of the B tree is often smaller than the tree height of the binary tree under the same amount of data, and the data retrieval speed is faster

Note: Data retrieval always starts from the root node, the height of the tree = the number of disk I/Os

What should I pay attention to when using and creating a multi-column index (also known as a compound index)?

Wind Qingyun said: the most left principle

Leftmost principle: For example, if you have a three-column index (col1, col2, col3), when the search condition is (col1), (col1, col2) or (col1, col2, col3), both Can take index query

Do you understand MySQL transactions? Talk about its features

Note: InnoDB supports transactions, MyISAM does not

Without thinking, say: ACID , which are Atomicity, Consistency, Isolation and Durability

The interviewer continued to ask: How many isolation levels are there for MySQL transactions?

There are four levels, from low to high: Read uncommitted, read committed, repeatable read, and serializable

NOTE: MySQL's default isolation level is repeatable read (repeatable read)

Explain dirty reading, non-repeatable reading, and phantom reading separately

At this time you must secretly cheer: "Small, I am not afraid of you asking, I am afraid you will not ask. The old man is already ready"

Dirty read means after a transaction has not been read transaction to another modification COMMIT data; non-repeatable reads refers to a transaction reads the same data multiple times, between the multiple readings of this transaction have additional data modify and COMMIT , leading to inconsistent results of reading the same data multiple times; phantom reads and non-repeatable read similar, except that the non-repeatable read focus is modified, the new focus is phantom read or delete

What are the storage engines of MySQL?

Answer: InnoDB and MyISAM are commonly used. There are also MEMORY, CSV, ARCHIVE, etc.

Then you talk about the difference between InnoDB and MyISAM

InnoDB supports transactions, MyISAM does not support
InnoDB supports foreign keys, MyISAM does not support
InnoDB is a clustered index, MyISAM is a non-clustered index. (Clustered index ≠ primary key)
MyISAM saves the total number of rows in the table, InnoDB does not save
InnoDB's smallest lock granularity is row locks, MyISAM's smallest lock granularity is table locks

The interviewer nodded slightly and continued:

You just mentioned the clustered index, how does it work?

Each InnoDB table has a special index, called a clustered index , which is used to store row data. Typically, a clustered index is the primary key index; if not defined for a table primary key will select a NOT NULL a unique index (UNIQUE) as a clustered index; if qualified UNIQUE not, generating a hidden up inside InnoDB Index GEN_CLUST_INDEX

Note: Usually we will create or select a InnoDB table for each successive increment of the column as the primary key (such as increment id), but strictly speaking, the primary key is not necessary **

What is the difference between VARCHAR(10) and CHAR(10)

A: VARCHAR (10) and CHAR (10) represents field can store up to 10 characters (Note: this is not a byte), but they are stored and retrieved in different ways. Their maximum length and whether to keep trailing spaces are also different.
Insert picture description here

The length of a CHAR column can be any value between 0 and 255. When CHAR stores the value, MySQL will right-fill with spaces to the specified length. When retrieving the value of a CHAR column, the trailing spaces will be discarded (if your data itself has trailing spaces, it will also be discarded). VARCHAR column stores variable-length character strings, up to 65535 bytes (different character sets, the bytes occupied by a character may be different), if the stored data does not reach the specified length, it will not be filled with spaces. In addition, when the VARCHAR column stores data, it will spend an extra 1 or 2 bytes to save the data length

What if I set the value'abcde' on the CHAR(2) column?

Can pretend to think, answer: There are two cases, if the SQL mode is set to strict, the value will not be stored and an error will be thrown. If the SQL mode is not set to strict, the value will be truncated and stored, and a warning will be thrown, for example,'ab' will be stored here

Do you understand MySQL master and slave? Briefly explain the master-slave synchronization process

Master-slave synchronization principle is actually based replication binary file , into homologous copy (a master multi-slave or a master-slave) and multi-source copy (multi-master multi-slave) , commonly used here to a master multi-slave , for example

Slightly organize the language and talk about it: Master writes all operation commands (add, delete, modify) to the data into the binlog file, and Slave reads the master's binlog file and executes it.

If necessary, you can also elaborate a little bit. The specific steps of master-slave replication are as follows:

Front-end: Master and each Slave have been configured with a unique server ID (using the server_id system variable), and the Master opens binlog

  1. Master: binlog thread-write all operation commands (add, delete, modify) to the data into the binlog file
  2. Slave: io thread-after using the start slave, it is responsible for pulling the binlog content from the master and putting it into its own relay log ; (Each slave will record the location of the last read, so it can be read next time. This It also means that multiple Slaves can be connected to the Master and execute different parts of the same binary log)
  3. Slave: SQL execution thread-execute statements in the relay log (Slave can be configured to only handle events that operate on a specific database or table)

The interviewer’s eyes light up, ah, this is a treasure

End of interview

The boy is pretty good, it's almost time for dinner, or let's go to the restaurant downstairs and have a chat while eating. By the way, what do you like to eat?

Smiled and said: It's an honor

This is a rare good opportunity. If you are lucky enough to encounter this situation, there is a high probability that the interviewer recognizes you very much. You can learn about the company's situation while eating, and you can also talk about the company's technology stack and hobbies. , To narrow the distance between each other. No matter how bad, you can fill your stomach...

Write at the end

I don’t know if you have such a feeling that many things seem very simple and you think you can do it too, but you just don’t want to do it; a lot of knowledge seems to be known to yourself, but when the interviewer asks, you can’t The logical answer was clear and complete, and afterwards I felt: I can do it all by myself, but I didn’t perform well...

I did the same before. But now I understand that I am not lazy, I just don't want to admit that I am a trash.

Stay hungry, stay foolish
strives to improve the visibility and breadth of your own skills. It is a necessary condition to pass the interview.
Usually, think more and summarize more, so that you can talk during the interview.
Besides, staying confident and calm will make you look more reliable. Improve interview pass rate

This article is based on the usual interview experience, summarized and perfected. Due to space reasons, some of the answers may not be perfect. I will analyze the common test sites during interviews in more detail in a later article.

In the simplest language, pretend the most high-end B

Finally, I wish you all go ashore soon. Our slogan is: We must win! ! !

Your point of praise and attention is very useful to me

Guess you like

Origin blog.csdn.net/weixin_39815001/article/details/108735294