The 20 Mysql interview questions most frequently asked by countless companies

The following are the 20 Mysql interview questions that I have summed up from interviews with countless companies and I feel that I have been asked the most frequently and most usefully. I will share them for your reference. I hope you all go well in the interview and get a satisfactory offer!Insert picture description here

Can you tell me the difference between myisam and innodb?

The myisam engine is the default engine before version 5.1. It supports full-text retrieval, compression, spatial functions, etc., but does not support transactions and row-level locks, so it is generally used in scenarios where there are a large number of queries and a small amount of insertion, and myisam does not support foreign keys. And the index and data are stored separately.

Innodb is based on a clustered index. Contrary to myisam, it supports transactions, foreign keys, and supports high concurrency through MVCC. Indexes and data are stored together.

Do you know what is covering index and back to table?

Covering index refers to that in a query, if an index contains or covers the values ​​of all fields that need to be queried, we call it a covering index, and no need to go back to the table query.

To determine whether a query is a covering index, we only need to explain sql statement to see whether the result of Extra is "Using index".

Take the user table above as an example, let's add another name field, and then try some queries.

explain select * from user where age=1; //The query name cannot be obtained from the index data
explain select id, age from user where age=1; //Can be obtained directly from the index

What are the types of locks?

MySQL locks are divided into shared locks and exclusive locks, also called read locks and write locks.

The read lock is shared and can be realized by lock in share mode. At this time, it can only read but not write.

Write lock is exclusive, it will block other write locks and read locks. From the granularity to distinguish, can be divided into table locks and row locks.

Table locks will lock the entire table and block all read and write operations on the table by other users. For example, alter will lock the table when modifying the table structure.

Row locks can be divided into optimistic locks and pessimistic locks. Pessimistic locks can be implemented through for update, and optimistic locks are implemented through version numbers.

What guarantee does ACID rely on?

A atomicity is guaranteed by the undo log log, which records the log information that needs to be rolled back. When the transaction is rolled back, the successfully executed SQL is undone

C consistency is generally guaranteed by the code level

I isolation is guaranteed by MVCC

D durability is guaranteed by memory + redo log. MySQL modifies data and records this operation in memory and redo log at the same time. When the transaction is committed, it is flushed through redo log, and it can be restored from redo log when it is down.

Since there are too many knowledge points to be compiled, only a part of it is shown here. Friends who need the answers to the interview questions and more interview materials can click the link below to get it for free! Link: 1103806531 Password: CSDN

Insert picture description here

How can the uniqueness of the ID after the sub-table be guaranteed?

Because our primary keys are all auto-incremented by default, then the primary keys after sub-tables will definitely conflict in different tables. There are several ways to consider:

Set the step size, such as 1-1024 tables, we set the basic step size of 1-1024 respectively, so that the primary keys will not conflict when they fall into different tables.
Distributed ID, implement a set of distributed ID generation algorithm by yourself or use an open source such as snowflake algorithm.
After the table is divided, the primary key is not used as the query basis, but a new field in each table is used as the unique primary key, such as the order table. The order number is unique, no matter which table it ultimately falls on, it is based on the order number as the query basis, and the update is the same.

Is unique index faster than normal index and why

Unique index is not necessarily faster than normal index, it may be slower.

When querying, when limit 1 is not used, after a piece of data is matched, the unique index is returned, and the ordinary index will continue to match the next piece of data, and return after finding a mismatch. It seems that the unique index has one less match, but In fact, this consumption is minimal. During the
update, the situation is more complicated. The statement of the ordinary index puts the record in the change buffer is executed. For the unique index, it must be verified for uniqueness, so it must be The data page is read into the memory to ensure that there is no conflict, and then the operation can be continued. For the case of more writes and less reads, the ordinary index uses the change buffer to effectively reduce the number of disk accesses, so the performance of the ordinary index is higher than that of the unique index.

What are the components of MySQL and what are they used for

Server connector: management connection, permission verification. Analyzer: lexical analysis, grammatical analysis. Optimizer: execution plan generation, index selection. Actuator: operate storage engine, return execution results.
Storage engine: store data, provide read and write interface.

What are the disadvantages of MySQL query cache, under what circumstances should it be used, and what changes have been made to query cache in version 8.0.

The query cache may fail very frequently. For a table, as long as there is an update, all query caches of the table will be emptied. Therefore, for a frequently updated table, the query cache may not have a positive effect.
For reads far more than You can consider using query cache for written tables.
The query cache function of version 8.0 has been deleted ( ̄. ̄).

How does MySQL restore data from half a month ago

Restore through the entire database backup + binlog. The premise is to have regular entire database backups and save binlog logs.

What are the characteristics of the isolation level of MySQL transactions

Read uncommitted (RU): When a transaction is not committed, the changes it makes can be seen by other transactions.
Read commit (RC): After a transaction is committed, the changes it makes will be seen by other transactions.
Repeatable read (RR): The data seen during the execution of a transaction is always consistent with the data seen at the start of the transaction. Of course, under the repeatable read isolation level, uncommitted changes are not for other transactions Visible.
Serialization (S): For the same row of records, both reads and writes will be locked. When a read-write lock conflict occurs, the subsequent access transaction must wait for the completion of the previous transaction before it can continue.

What MySQL index related optimizations have been made

Try to use the primary key query: all data is stored on the clustered index, which reduces the consumption of back to the table compared to ordinary index queries.
After MySQL 5.6, the index push down optimization is introduced, and through the appropriate use of joint indexes, it reduces the judgment of back to the table Consumption.
If you frequently query a column of data, you can consider using a covering index to avoid returning to the table. The
joint index puts the high-frequency field on the far left.

Briefly talk about the database paradigm

First normal form: Attributes cannot be divided.
Second normal form: On the basis of a normal form, each instance or row in a database table must be uniquely distinguishable. It is usually necessary to add a column to the table to store each instance This unique attribute column is called the primary key or primary key. The
third paradigm: On the basis of the second paradigm, a database table is required to not contain non-primary key information that has been contained in other tables. So The third normal form has the following characteristics: 1). Each column has only one value. 2). Each row can be distinguished. 3). Each table does not contain non-primary key information that other tables already contain.

A table with 10 million data, how to query by page

When the amount of data is too large, the limit offset paging will be slower to query later due to too much scan data. You can query with the last ID of the current page, SELECT * FROM T WHERE id> #{ID} LIMIT #{LIMIT }. Of course, ID must be ordered in this case, which is also one of the benefits of ordered ID.

How to deal with the slow query due to the increasing amount of data in the order table

Sub-database sub-table. Since the historical order usage rate is not high, the high frequency may only be recent orders. Therefore, the order table is split according to time, and the monthly or annual sub-table is considered according to the amount of data. Orders The ID preferably contains the time (such as generated according to the snowflake algorithm).At this time, the order record can be obtained directly according to the order ID, or it can be queried according to time.

Insert picture description here

Why use B+ tree as index instead of hash table?

1. The hash table maps the index field to the corresponding hash code and then stores it in the corresponding location. In this case, if we want to perform fuzzy search, obviously the structure of the hash table is not supported and can only be traversed This table. The B+ tree can quickly find the corresponding data through the leftmost prefix principle.

2. If we want to perform a range search, such as looking for a person with an ID of 100 ~ 400, the hash table is also not supported, and we can only traverse the entire table.

3. The index field is mapped into a hash code by hash. If many fields happen to be mapped to the hash code of the same value, the index structure formed will be a very long linked list. In this case, the search time will be greatly increase.

What is the difference between CHAR and VARCHAR?

1. CHAR and VARCHAR types are different in storage and retrieval

2. The length of the CHAR column is fixed to the length declared when the table is created, and the length value range is 1 to 255. When CHAR values ​​are stored, they are filled with spaces to a specific length. Trailing spaces need to be deleted when retrieving CHAR values.

What is the difference between primary key and candidate key?

Each row of the table is uniquely identified by the primary key, and a table has only one primary key.

The primary key is also a candidate key. By convention, candidate keys can be designated as primary keys and can be used for any foreign key references.

What is myisamchk used for?

It is used to compress MyISAM tables, which reduces disk or memory usage.

What is the difference between MyISAM Static and MyISAM Dynamic?

All fields on MyISAM Static have a fixed width. The dynamic MyISAM table will have fields like TEXT, BLOB, etc. to accommodate data types of different lengths.

MyISAM Static is easier to recover in case of damage.

What are transactions in the database?

A transaction is an ordered set of database operations as a unit. If all operations in the group are successful, the transaction is considered successful, even if only one operation fails, the transaction is not successful. If all operations are completed, the transaction is committed and its modifications will be applied to all other database processes. If an operation fails, the transaction will be rolled back and the impact of all operations of the transaction will be cancelled.

Transaction characteristics:

1. Atomicity: That is, indivisibility, all transactions are executed, or all transactions are not executed.

2. Consistency or stringency. The execution of the transaction makes the database from one correct state to another correct state

3. Isolation. Before the transaction is committed correctly, it is not allowed to provide any data changes made by the transaction to any other transaction.

4. Persistence. After the transaction is submitted correctly, the result will be permanently saved in the database. Even if there are other failures after the transaction is submitted, the processing result of the transaction will be saved. Or understand it this way: A transaction is a group of SQL statements that are bound together as a logical unit of work. If any statement operation fails, the entire operation will fail, and the operation will roll back to the pre-operation state, or there is a node. To ensure that it is either executed or not executed, you can use transactions. To consider grouped statements as transactions, it is necessary to pass the ACID test, namely atomicity, consistency, isolation and durability.

What causes SQL injection vulnerabilities? How to prevent it?

Reasons for SQL injection: In the process of program development, care is not taken to write sql statements and filter special characters. As a result, the client can submit some sql statements through global variables POST and GET for normal execution. Ways to prevent SQL injection:

Enable the magic_quotes_gpc and magic_quotes_runtime settings in the configuration file

When executing sql statement, use addslashes to convert sql statement. Sql statement writing try not to omit double quotation marks and single quotation marks.

Filter out some keywords in the sql statement: update, insert, delete, select, *.

Improve the naming skills of database tables and fields, name some important fields according to the characteristics of the program, and choose the ones that are not easy to guess.

Insert picture description here

At last

I have also compiled a full set of video tutorials for architects and systematic materials on java, including core knowledge points of java, interview topics, and the latest 20 years of Internet real questions, e-books, etc. I hope to help you!

Friends in need can click the link below to get it for free!

Link: 1103806531 Password: CSDN
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48655626/article/details/108492209