Some issues related to MySQL


1. What are the three paradigms of databases?

The first paradigm: emphasizes the atomicity of columns, that is, each column of the database table is an indivisible atomic data item.
The second paradigm: requires the attributes of the entity to be completely dependent on the primary key. The so-called complete dependence means that there cannot be attributes that only depend on part of the primary key.
The third paradigm: any non-primary attribute does not depend on other non-primary attributes.

2. There are a total of 7 pieces of data in an auto-increment table, the last 2 pieces of data are deleted, the MySQL database is restarted, and another piece of data is inserted. What is the id at this time?

If the mysql engine is innodb, the id is 6 at this time;
if the mysql engine is MyISAM, the id at this time is 8;
InnoDB tables will only record the maximum id of the auto-incrementing primary key in memory, so the maximum id will be lost after restarting . The self-incrementing primary key starts from 1.

3. How to get the current database version?

Use sql: select version()

Fourth, what is ACID?

A: Atomicity.
All operations in a transaction are completed or not completed at all, there will be no interruption in the middle. If the transaction operation is abnormal, it will be rolled back before the uncommitted transaction.
C: Consistency
Before the start of the transaction and after the end of the transaction, the integrity of the database has not been destroyed. This means that the written data must fully comply with all preset constraints, triggers, cascading rollbacks, etc.
I: Isolation The
database has the ability to allow multiple concurrent transactions to read, write and modify its data at the same time. Isolation can prevent data inconsistencies caused by cross execution when multiple transactions are executed concurrently.
D: Persistence
After the transaction is completed, the modification of the data is permanent, and it will not be lost even if the system fails.

5. What is the difference between char and varchar?

char(n): fixed length type, such as subscription char(10), when you enter the three characters "abc", they still occupy 10 bytes, and the other 7 are null bytes.
Char Advantages: High efficiency; Disadvantages: Space occupation; Applicable scenarios: The md5 value of the password is stored, and the use of char is very suitable.
varchar(n): variable length, the stored value is the length of the byte occupied by each value plus a byte used to record its length.
Therefore, it is more appropriate to consider varcahr in terms of space; it is more appropriate to consider char in terms of efficiency, and the use of the two needs to be weighed.

6. What is the difference between float and double?

Float can store up to 8 decimal digits and occupies 4 bytes in the memory.
A double can store up to 16-digit decimal numbers and occupy 8 bytes in the memory.

7. What is the difference between MySQL's inner connection, left connection, and right connection?

Inner join keywords: inner join; left join: left join; right join: right join.
The inner connection is to display the matched related data; the left connection is to display all the tables on the left, and the table on the right shows the data that meets the conditions; the right connection is just the opposite.

8. How is the MySQL index implemented?

An index is a data structure that satisfies a certain search algorithm, and these data structures point to the data in a certain way, so as to achieve efficient search for data.
Specifically, the indexes in MySQL are implemented differently by different data engines, but the indexes of the current mainstream database engines are all implemented by B+ trees. The search efficiency of B+ trees can reach the performance of the dichotomy. The complete data structure has been found, so the performance of the index is better.

9. How to verify whether the MySQL index meets the requirements?

1. Verify by viewing the execution plan by explain
2. Verify by comparing the time-consuming difference between the indexed search and the non-indexed search

Ten, talk about the transaction isolation of the database?

read-uncommited: read-uncommited:
other transactions can operate on the database before the transaction is committed, which will lead to dirty reads, non-repeatable reads and phantom reads.
Read-commited:
after the read transaction is committed, other transactions can operate on the database. Dirty reads can be prevented, but non-repeatable reads and phantom reads cannot be prevented;
Repeatable-read: The
default level of repeatable reads , to ensure that when the same data is read multiple times, its value is consistent with the content at the beginning of the transaction. Reading is prohibited Uncommitted data to other transactions (will cause phantom reads).
Serializable: Serialization
This mode is the isolation level that consumes the highest and most secure (the most costly and most reliable). Can prevent dirty reads, non-repeatable reads, and phantom reads.

11. What are the commonly used engines for MySQL?

innodb: The
InnoDB engine provides support for database acid transactions, and also provides row-level locks and foreign key constraints. Its design goal is to handle large data capacity database systems. When MySQL is running, InnoDB will build a buffer pool in memory for buffering data and indexes. However, the engine does not support full-text search, and it is relatively slow to start. It does not save the number of rows in the table, so when the select count( ) from table instruction is executed, the entire table needs to be scanned. Because the granularity of the lock is small, the write operation will not lock the entire table, so using it in scenarios with high concurrency will improve efficiency.
MyISAM:
MySQL's default engine, but does not provide transaction support, nor does it support row-level locks and foreign keys. Therefore, when inserting and updating statements are executed, that is, the table needs to be locked when performing write operations, so the efficiency will be reduced. However, unlike InnoDB, the MyISAM engine saves the number of rows in the table, so when the select count(
) from table statement is executed, the saved value can be directly read without scanning the entire table. Therefore, if the table has far more read operations than write operations and does not require transaction support, you can use MyISAM as the first choice for the database engine.

12. What about row locks and table locks in MySQL?

MyISAM only supports table locks, InnoDB supports table locks and row locks, and the default is row locks.
Table-level locks: low overhead, fast locking, and no deadlocks. The lock granularity is large, the probability of lock conflicts is the highest, and the amount of concurrency is the lowest.
Row-level locks: high overhead, slow locking, and deadlocks. The lock granularity is small, the probability of lock conflicts is small, and the concurrency is the highest.

Thirteen, talk about optimistic locking and pessimistic locking?

Optimistic lock: Every time you get the data, you think that others will not modify it, so it will not be locked, but when submitting the update, it will be judged whether others have updated the data during this period.
Pessimistic lock: Every time you get the data, you think that someone else will modify it, so you will lock it every time you get the data, so that other people want to get the data will be blocked until the lock is released.
The optimistic lock of the database needs to be implemented by yourself. Add a version field in the table, and add 1 to the successful value of each modification, so that you can compare the version you own with the current version of the database when you modify it. If it is inconsistent, do not Modify, so that optimistic locking is realized.

14. What are the methods for troubleshooting MySQL?

1. Use the show processlist command to view all current connection information.
2. Use the explain command to query the SQL statement execution plan.
3. Open the slow query log and view the SQL of the slow query.

15. How to optimize MySQL performance?

1. Create an index for the search field.
2. Avoid using select * and list the fields that need to be queried.
3. Split the table vertically.
4. Choose the correct storage engine.

16. Mysql index structure, the difference between B+tree and BTree?

1. In B+Tree, all data record nodes are stored on the leaf nodes of the same layer in the order of key value size, instead of storing only key value information on the leaf nodes, this can greatly increase the key stored by each node Value, reduce the height of B+Tree.
2. B+Tree adds a chain pointer to all leaf nodes.

17. Why use the structure of B/B+tree to implement indexing?

MySQL is a disk-based database. The index exists on the disk in the form of an index file. The search process of the index involves disk IO consumption. The disk IO consumption is several orders of magnitude higher than the memory IO consumption. The organizational structure of the index should be designed to minimize the number of disk IOs when searching for keywords, and only the B/B+ tree data structure can meet the needs.

18. Why does MySQL index use B+ tree instead of B tree?

1. B+ tree is more suitable for external storage (usually refers to disk storage). Since internal nodes (non-leaf nodes) do not store data, a node can store more internal nodes, and each node can index a larger and more accurate range. That is to say, the amount of information in a single disk IO using the B+ tree is larger than that of the B tree, and the IO efficiency is higher.
2. MySQL is a relational database. It often accesses a certain index column according to the interval. The leaf nodes of the B+ tree establish chain pointers in order to strengthen the accessibility of the interval. Therefore, the B+ tree queries the interval range on the index column very well. friendly. However, the key and data of each node of the B-tree are together, and interval search cannot be performed.

Guess you like

Origin blog.csdn.net/qq_42697271/article/details/114012692