MySql (16)

What are the three paradigms of the database?

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.

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 table type is MyISAM, the id is 8.
If the table type is InnoDB, the id is 6.

InnoDB tables will only record the maximum id of the auto-incrementing primary key in memory, so the maximum id will be lost after restart.

How to get the current database version?

Use select version() to get the current MySQL database version.

What is ACID?

Atomicity (atomicity): All operations in a transaction (transaction), either all completed or not completed at all, will not end in an intermediate link. If an error occurs during the execution of the transaction, it will be restored (Rollback) to the state before the transaction started, as if the transaction had never been executed. That is, transactions are indivisible and irreducible.
Consistency (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.
Isolation: The ability of the database 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. Transaction isolation is divided into different levels, including read uncommitted, read committed, repeatable read, and serializable.
Durability: After the transaction is completed, the modification of the data is permanent, and it will not be lost even if the system fails.

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.

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 a 16-bit decimal number at most, and it occupies 8 bytes in the memory.

What is the difference between inner connection, left connection, and right connection in mysql?

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.

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 is found, and the performance of all indexes is better.

How to verify whether the mysql index meets the requirements?

Use explain to view how SQL executes query statements, so as to analyze whether your index meets the requirements.

explain 语法:explain select * from table where type=1。

Talk about the transaction isolation of the database?

MySQL transaction isolation is added in the MySQL.ini configuration file, add at the end of the file: transaction-isolation = REPEATABLE-READ

Available configuration values: READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.

READ-UNCOMMITTED: uncommitted read, the lowest isolation level, before the transaction is committed, it can be read by other transactions (phantom reads, dirty reads, and non-repeatable reads will occur).
READ-COMMITTED: Commit read, a transaction can only be read by other transactions after it is committed (it will cause phantom reads and non-repeatable reads).
REPEATABLE-READ: Repeatable read, the default level, to ensure that when the same data is read multiple times, its value is consistent with the content at the beginning of the transaction. It is forbidden to read the uncommitted data of other transactions (will cause phantom reads) .
SERIALIZABLE: Serialization, the most costly and most reliable isolation level, this isolation level can prevent dirty reads, non-repeatable reads, and phantom reads.

Dirty read: indicates that a transaction can read data that has not yet been committed in another transaction. For example, a transaction tries to insert record A, and the transaction has not yet committed, and then another transaction tries to read record A.

Non-repeatable reading: refers to reading the same data multiple times in a transaction.

Phantom reading: Refers to the different result sets returned by multiple queries within the same transaction. For example, the same transaction A has n records in the first query, but there are n+1 records in the second query under the same conditions, which seems to have an illusion. The reason for the phantom read is that another transaction adds or deletes or modifies the data in the first transaction result set. If the data content of the same record is modified, the records of all data rows become more or less.

Tell me about the engines commonly used in mysql?

InnoDB engine: 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 command 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 engine: 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.

Talk about the row lock and table lock of 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 locking 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 strength is small, the probability of lock conflicts is small, and the concurrency is the highest.

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.

What are the methods for mysql troubleshooting?

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

How to optimize the performance of mysql?

Create an index for the search field.
Avoid using select * and list the fields that need to be queried.
Split the sub-table vertically.
Choose the right storage engine.

Guess you like

Origin blog.csdn.net/xghchina/article/details/115038434