MySQL- must know will be

1. Index

1.1 B + Tree index

A: InnoDB the default indexing . Ordered index, the neighboring data are present together, the order becomes random IO IO.

1.1.1 underlying database B + tree Why

A: Summary:

  • In the leaf node, the non-leaf node of a B-tree also stores the data stored in all of the B + tree. Such that the B + tree queries from the root to the leaf node.
  • B + tree internal node more than the B-tree small , the cost of a small read and write.

1.1.2 clustered index (main index) and the non-clustering index (secondary indexes)

Answer: B + Tree index divided into the main index and secondary index.

  • Clustered index (main index): leaf node stores the entire line . You can get directly to key data. A table can contain only one primary index.
  • Non-clustering index (secondary index): the leaf nodes storing the primary key values. You will need to be a primary key, primary key and then walk along the main index.

1.1.3 back to the table

A: The general index back to the table refers queries to search the general index tree to get the primary key value, then the primary key index tree search again.

1.2 Hash Index

A: In the O (1) time search, but you can not sort packets, only to find precision .
Adaptive Hash Index: When an index is frequently cited, and then create a hash index on B + tree index, providing fast lookup.

1.3 full-text index

A: MyISAM supports full-text index, used to find text keyword, match against lookup. InnoDB also supports the 5.6.4 version.

1.4 Index Tuning

1.4.1 index column order

A: The most selective index on the front . Unique selectivity index = index value / total number of records.

1.4.2 covering index

A: The index is covering only select data columns can be acquired from the index without reading the data line, that query column to be covered by the index built.

1.4.3 Prefix index

A: According selective, only the index of the string leftmost M characters .

1.4.4 index pushdown

A: The joint index , the index is traversed, before the rest of the other field judge, filter the records do not satisfy the conditions to reduce the frequency and amount of data back to the table.

1.5 index usage scenarios

answer:

  • Often need to query conditions are listed and there is discrimination, it can be indexed.
  • Small table, no indexes, full table scan can be. Medium to large tables, indexes recommended.
  • Large table, build and maintain the price index is too large, it is better with partitioning technology.

2. Storage Engine

A: MySQL is a relational database, the default port number is 3306.5.5 before use MyISAM, after 5.5 with InnoDB.

2.1 InnoDB

A: After the 5.5 version of the default engine. The default transaction isolation level, the main index repeatable read is a clustered index, do a lot of internal optimization (insert buffer, hash indexes, etc.), support hot backup.

2.2 MyISAM

A: The pre-5.5 versions of the default engine.

2.3 MyISAM and InnoDB difference

A: The differences are as follows:

  • Row-level locking. MyISAM supports only table-level locking, InnoDB support table-level locks and row-level locking.
  • Transaction support. MyISAM does not support transactions, InnoDB provides transaction support and crash recovery.
  • Foreign keys. MyISAM does not support foreign keys, InnoDB foreign keys support.
  • InnoDB supports multi-version concurrency control, can handle large amounts of data.
  • MyISAM supports full-text indexing.

To summarize: InnoDB supports row-level locking, transactions, foreign keys .

3. Transaction

A: The transaction is a logical set of operations, or are executed or not executed.

3.1 four properties ACID

A: ACID.

  • Atomicity (Atomicity): the transaction is the smallest unit of execution, either all executed or not executed all.
  • Consistency (Consistency): To ensure that the database state is consistent, multiple transactions on the same data read result is the same.
  • Isolation (Isolation): a plurality of concurrent transactions, each transaction is not subject to interference by other transactions.
  • Persistent (Durability): Once a transaction is committed, this modification is persisted in the database.

The problem of concurrent transactions 3.2

  • Dirty read (Dirty read): Transaction A transaction reads uncommitted data modification B, B rollback, inconsistent data A.
  • Lost modify (Lost to modify): Transaction A and B simultaneously read data, to submit the modified A, modified B coverage will then submit modifications A's.
  • Non-repeatable read (Unrepeatable read): A between the two read data transaction, the transaction has updated modifications B, resulting in inconsistent data is read twice.
  • Magic Reading (Phantom read): A transaction between the two read data, there are recorded transaction B increases, resulting in twice the number of records read data inconsistency.
    NOTE: Magic Reading finger changes on. Non-repeatable reading index changes occur.

3.3 Isolation Level

A: MySQL defines four isolation levels.

  • Uncommitted Read (Read-Uncommitted): the lowest level allowed to read data has not been submitted. Happens dirty reads, phantom reads, non-repeatable read.
  • Read Committed (Read-Committed): allowed to read the data already submitted. Prevent dirty reads.
  • Repeatable read (Repeatable-Read): read write transaction prohibited transactions, block all write transactions. Prevent dirty reads and non-repeatable read. InnoDB the default isolation level .
  • Serialization (Serialization): the highest level. All transactions executed in turn, will not interfere with each other. Stop three affairs issues.

4. The lock mechanism

4.1 points according to size

A: divided into table-level locks and row-level locking.

  • Table-level lock : The maximum size of the lock. Lock on an entire table , resource consumption, fast locking, deadlock free, but the high probability of lock conflicts.
  • Row-level locks : locks smallest granularity. Of the current operating line lock , lock slow, big spending, deadlock, but greatly reduce the conflict.

4.1.1 common row-level locking

A: There are three.

  • Record Lock: one-way lock, the lock qualifying line.
  • Gap Lock: gap lock, a lock range, excluding the recording itself.
  • Next-key Lock: Record + Gap , a lock range, containing records themselves .

4.1.2 table lock usage scenarios

A: When the transaction is more complex, easier to use row-level locking deadlock rollback. Most of the data update large table, table-level locking efficiency better.

4.1.3 When row-level locking will lock the entire table

A: The updated column is not indexed, it will directly lock the entire table.

Is writable by 4.2 points

A: You can be further divided into shared locks and exclusive locks.

  • Shared lock (Shared Lock): read lock. Locked resources by other users to read, but can not be modified until the S lock on all resources are released.
  • Exclusive lock (Exclusive Lock): write lock. A transaction T plus X lock for the data, read only allowed to modify T, X until the lock is released. In the update operations, such as insert, update, or delete, always apply an exclusive lock.

4.3 Deadlock

A: MySQL is deadlock in multiple transactions using row-level locking for lock caused by a row of data .

4.3.1 Solution

A: divided into two areas.
The operational level:

  • Lock access to resources specified order. (Dining philosophers problem in the operating system)
  • The same transaction once the lock as many resources.
  • Transaction split into small transactions.

Database settings:

  • Set timeout. InnoDB default is 50s.
  • Open deadlock detection. When a deadlock occurs, the return on a transaction deadlock chain, so that other matters continue.

4.4 pessimistic locking and optimistic locking

A: divided into:

  • Pessimistic lock : the use of database lock mechanism of realization, then the entire data processing are locked, guarantee exclusivity .
  • Optimistic locking : CAS achieve . Whether the data in the data tables and data in accordance with the current transaction unanimously, to decide whether to perform the operation.

ABA problem 4.4.1 optimistic locking

A: Add version of the data recording mechanism or using timestamps.
ABA problem: when the transaction reads data X A, Y affairs modified to B, back and modify A, the data actually happened changed, concurrency problems, but the transaction occurred X data are not available changes.

The large table optimization

A: The single table records the number is too large, database performance significantly decreased, the need for a series of optimization.

5.1 defined data range

The simplest means to limit the scope of the condition data to query the data.

5.2 separate read and write

Master processes write or read operation of high real-time, from the server to process the read operation .

  • Implementation: additional proxy server, decides to forward the application to which the request coming from the server.
  • The reason: greatly reduced lock contention; redundant exchange availability; conservation of resources available from the server MyISAM.
    Here Insert Picture Description

5.3 Split Horizontal

A horizontal split the Table A Table a1 split into a plurality of identical structure, a2, a3 storage, only a portion of each sub-table data.
Subdivided into sub-table library and sub-library sub-table.

  • Library sub-table: the child is still a database table instance, still competing for the same physical machine resources.
  • Sub-library sub-table: child tables scattered in different databases.

Advantages: Business transformation is simple; single table to solve the problem of data overload.
Disadvantages: difficult to ensure consistency across the library; poor join the associated performance; expansion and maintenance too difficult.

5.4 Vertical Split

Vertical subdivided into base and vertical sub-table.

  • Vertical library: a database based on business segments, so that each business has its own database.
  • Vertical Table: column-based segmentation data table, a column of the Table 1-7 Table 1-4 is split into row and column of 15-7.

Advantages: Business decoupling; improved performance under high concurrency.
Disadvantages: increased complexity of the service; primary key redundancy ; large amount of data is not resolved, with the required level of resolution.

5.5 problems after split

  1. Transactional consistency
    two-phase commit poor performance. Usually pursue eventual consistency , a problem transaction compensation.
  2. Paging and sorting
    the sort field non-fragmented field, need to sub-sort in the table, then the summary sorted, and finally returned to the user.
  3. Globally unique primary key
    auto-increment primary key sub-libraries can not meet the demand, so the use of distributed globally unique ID as the primary key .
    Note: it is recommended for mature middleware, such as sharding-jdbc, Atlas, Cobar

6. MySQL architecture and implementation process

6.1 Infrastructure

A: MySQL Server is divided into layers and the storage engine layer.

  • Server layer : cross-Storage Engine functionality in this implementation, such as views, triggers, functions, etc., there is a binlog log.
  • Storage Engine : responsible for data storage and retrieval. Commonly used InnoDB, redolog carrying module.

6.2 Basic Components

A: Server layer has five main components.

  • Connectors: authentication and permissions settings.
  • The query cache: before executing the query, first check there is not cached result set. (Abandoned after 8.0)
  • Analyzer: no cache hit, then enter the analyzer lexical and grammatical analysis.
  • Optimizer: According to MySQL considered optimal program implementation.
  • Actuator: The user has permission, the call storage engines, the statement is executed.

6.3 execution process

6.3.1 query

Permissions check ----> query caching ----> analyzer ----> Optimizer ----> Permissions check ----> Actuators ----> Engine

6.3.2 update statement

Analyzer ----> ---- check permissions> effector ----> Engine ----> redo log prepare ----> binlog ----> redo log commit
to modify Bob's Age, for example:

  • Joe Smith to the first query this data, if there is a cache with the cache.
  • Get the query statement, the age was changed to 19.
  • Engine API interface calls, write this line data, InnoDB engine data stored in memory, also recorded redo log, redo log at this time prepare to enter the state.
  • Notification actuators, perform complete, you can submit.
  • Actuator after receiving the notification record binlog, engine interface calls, submit redo log for the submission of state.
  • update completed.

7. logging module and keywords distinction

7.1 log module

A: MySQL main redolog and binlog.

  • redolog (redo logs) : InnoDB-specific, physical log. Which records the data page has been modified .
  • binlog (archive log) : Server layer comes, logical logs, records of this modification of the SQL statement . Row binary format commonly used to ensure the accuracy of the data records.

7.1.1 redolog and binlog two-phase commit

A: submitted in two stages in order to ensure data consistency. redolog prepare engine data retention -> binlog actuator signal is received -> redolog commit actuators call
for submission of such abnormal downtime, MySQL redolog first determine whether a full, complete the direct submission. redolog just prepare status, view binlog is complete, would continue to submit complete redolog, or roll back the transaction.

7.1.2 MySQL Why did he suddenly look slow

A: When updating the database, to write the log, and other appropriate time to update the disk. When redolog full, need flush dirty pages , write data to disk, which is slower it will perform.

7.2 where, group by and having keywords

A: The three keys are summarized as follows:

  • where clause from the line used to screen generated;
  • group by packet output to the where clause;
  • for having the packet group by the results from the screening line.

7.2.1 where and having differences

A: where the packet before use for the line. After having used the packet, for the group, can max, avg other aggregate functions.

7.2.2 execution order

where to find qualified data ----> group by grouping ----> ---- aggregate function is calculated for each group> group HAVING remove undesired

8. Optimization

Reference https://www.cnblogs.com/huchong/p/10219318.html Author: hear the wind.

Published 117 original articles · won praise 8 · views 3702

Guess you like

Origin blog.csdn.net/qq_34761012/article/details/104534637