(Java Daily Talk: Day 16 - Employment Interview Knowledge Clearance Strategy) MySQL database

        What are the three paradigms of database?

First normal form: each column cannot be split again (atomicity)

Second normal form: On the basis of the first normal form, non-primary key columns are completely dependent on the primary key (directly dependent)

Third normal form: On the basis of second normal form, non-primary key columns only depend on the primary key, not on other primary keys (completely dependent)

        What is the difference between MyISAM index and InnDB index?

The InnDB index is a clustered index, and the MyISAM index is a non-clustered index

The leaf nodes of InnDB's primary key index store row data, so the primary key index is very efficient

The leaf node of the MyISAM index stores the row data address, which needs to be addressed again to get the data

The leaf nodes of the InnDB non-primary key index store the primary key and other indexed column data, so covering the index will be very efficient when querying

        Four characteristics of the InnoDB engine?

insert buffer

secondary write

adaptive hash index

read ahead

        What are database transactions?

A transaction is an indivisible sequence of database operations and the basic unit of database concurrency control. The result of its execution must change the database from one consistent state to another. A food is a logical set of operations that either do all or none of them.

        What are the four major characteristics (ACID) of transactions?

Atomicity: A transaction is the smallest unit of execution and cannot be split. The atomicity of transactions ensures that actions are either fully completed, or safe to have no effect.

Consistency: Before and after the execution of the transaction, the data remains consistent, and the results of multiple transactions reading the same data are the same

Isolation: When accessing the database concurrently, the transactions of a user will not be interfered by other transactions, and the databases between concurrent transactions are independent.

Durability: After a transaction is committed. Its changes to the data in the database are persistent. Even if the database goes down it shouldn't have any effect on it.

        What is a dirty read? Phantom reading? Non-repeatable read?

Dirty read: A transaction has updated a piece of data, and another transaction has read the same piece of data at this time. For some reason, the previous RollBack operation, the data read by the latter transaction will be incorrect .

Non-repeatable read: The data is inconsistent between the two queries of a transaction, which may be the original data updated by a transaction inserted in the middle of the two queries

Phantom read: the number of data items in the two queries of a transaction is inconsistent

        What is the transaction isolation level? What is the default isolation level of MYSQL?

READ-UNCOMMITTED (read uncommitted): The lowest isolation level that allows reading uncommitted data changes, which may cause dirty reads, phantom reads, and non-repeatable reads. 

READ-COMMITTED (read committed): Allows to read data committed by concurrent transactions, which can prevent dirty reads, but phantom reads and non-repeatable reads may still occur.

PEPEATABLE-READ (repeatable read): The result of multiple reads on the same field is consistent, unless the data is modified by the transaction itself, which can prevent dirty reads and non-repeatable reads, but phantom reads may still occur.

SERIALIZABLE (serializable): The highest isolation level, fully compliant with the ACID isolation level. All transactions are executed one by one, so that there is no possibility of interference between transactions, that is, this level can prevent dirty reads, non-repeatable reads, and phantom reads.

        The relationship between isolation level and lock?

At the READ-UNCOMMITTED level, reading data does not require a shared lock, which conflicts with an exclusive lock on data that is modified so that it will not be changed.

At the READ-COMMITTED level, the read operation requires a shared lock, but the lock is released after the statement is executed

At the PEPEATABLE-READ level, the read operation needs to add a shared lock, but the shared lock is not released before the transaction is committed, that is, the shared lock must be released after the transaction is executed.

SERIALIZABLE is the most restrictive isolation level because it locks an entire range of keys and holds the lock until the transaction completes.

        What is deadlock? How to deal with it?

A deadlock is a phenomenon in which two or more transactions occupy each other on the same resource and request to lock each other's resources, resulting in a vicious circle.

Common solutions to deadlocks:

1. If different programs will access multiple tables concurrently, try to agree to access the tables in the same order, which can greatly reduce the chance of deadlock

2. In the same transaction, try to lock all the resources needed at once to reduce the probability of deadlock

3. For business parts that are very prone to deadlocks, you can try to use upgraded locking granularity to reduce the probability of deadlocks through table-level locking.

        What are pessimistic and optimistic locking in database? How did it happen?

The task of concurrency control in the database management system is to ensure that the isolation and unity of the transaction and the unity of the database are not destroyed when multiple transactions access the same data in the data at the same time. Optimistic lock concurrency control (optimistic lock) and pessimistic lock concurrency control (pessimistic lock) are the main technical means used in concurrency control.

Pessimistic lock: Assuming that concurrency conflicts will occur, all operations that may violate data integrity are blocked. When the data is queried, the transaction is locked until the transaction is committed. Implementation method: use the lock mechanism in the database. Optimistic locking: Assuming that no concurrency conflicts will occur, only checks for data integrity violations when committing operations. When modifying data, lock the transaction and lock it by version. Implementation method: Optimistic locking is generally implemented using a version number mechanism or a CAS algorithm.

Scenarios for using the two types of locks From the above introduction to the two types of locks, we know that the two types of locks have their own advantages and disadvantages, and we cannot think that one is better than the other. For example, optimistic locks are used when there are fewer writes (more read scenarios) ), that is, when there are very few conflicts, the overhead of locking can be saved and the throughput of the system can be increased.

However, if there is a lot of writing, conflicts will often occur, which will cause the upper-layer application to continue to retry, which will actually reduce the performance, so it is more appropriate to use pessimism in the scenario of a lot of writing.

        The difference between varchar and char?

char features:

char means a fixed-length string, and the length is fixed; if the length of the inserted data is less than the fixed length of char, it will be filled with spaces; because the length is fixed, the access speed is much faster than varchar, but because of its fixed length, Therefore, it will occupy extra space, which is the practice of exchanging space for time;

For char, the maximum number of characters that can be stored is 255, regardless of encoding

The characteristics of varchar

Varchar represents a variable-length string, and the length is variable; the inserted data is stored according to the length; varchar is the opposite of char in terms of access, it is slow because the length is not fixed, but because In this way, not occupying extra space is the practice of exchanging time for space;

        SQL life cycle?

1. The application server establishes a link with the database server

2. The database process gets the request sql

3. Analyze and generate an execution plan, execute

4. Read data to memory and perform logical processing

5. Through the link in step 1, send the result to the client

6. Close the connection and release resources

        How to optimize large table data query?

1. Optimize schema, sql statement + index

2. The second cache, memcached, redis

3. Master-slave replication, read-write separation

4. Vertical splitting, according to the coupling degree of your modules, split a large system into small systems, that is, distributed systems

5. Horizontal split

        MySQL replication principle and process

Master-slave replication: The DDL and DML operations in the database are transferred to the database through the binary log (BINLOG), and then these logs are re-executed; so that the data in the slave database is consistent with the master database.

The role of master-slave replication

        1. If there is a problem with the master database, you can switch to the slave database

        2. Can perform read-write separation at the database level

        3. Daily backup can be performed on the slave database

Problems solved by MySQL master-slave replication:

        Data distribution: Start or stop replication at will, and distribute data backup load balancing in different geographical locations; reduce the pressure on a single server, high availability and failover; help group applications avoid single point of failure, upgrade testing, you can use a higher version of MySQL as a slave

        Working principle of MySQL master-slave replication

        Record the data higher in the binary log on the main library, copy the log of the main library to its own relay log from the library

        Read the events of the relay log from the library and replay them into the slave library data.

        MySQL locks: pessimistic locks, optimistic locks, exclusive locks, shared locks, table-level locks, row-level locks

optimistic lock

Implemented with a data version recording mechanism, which is the most commonly used implementation of optimistic locking. What is a data version? That is to add a version identifier to the data, generally by adding a numeric type "version" to the database table. When reading data, read the value of the version field together, and add 1 to the version value every time the data is updated. When we submit an update, it is judged that the current version information of the corresponding record in the database table is compared with the version value extracted for the first time. If the current version number of the database table is equal to the version value extracted for the first time, it is updated. Otherwise, it is considered as expired data.

pessimistic lock

Each operation must acquire a lock to operate on the same data, which is very similar to synchronized in Java. Shared locks and exclusive locks are different implementations of pessimistic locks.

shared lock

Shared locks are also called read locks. All transactions can only read and write to them. After adding shared locks, other transactions can only add shared locks before the end of the transaction. Except for any other types of locks, they cannot Plus.

exclusive lock

If a certain food adds an exclusive lock to a row, only this transaction can read and write it. Before the end of this transaction, other transactions cannot add any locks to it. Other processes can read but cannot write. Need to wait for its release.

table lock

InnoDB's row lock is in the case of an index, and the table without an index locks the entire table

row level lock

Row locks are divided into shared locks and exclusive locks. Literally, it means to lock a certain row, that is, to add a lock to each record. Note: Row-level locks are based on indexes. If a SQL statement does not use indexes, row-level locks will not be used, and table-level locks will be used.

Guess you like

Origin blog.csdn.net/weixin_50249953/article/details/124368318