Interview must ask [database]-mysql index, lock, database transaction, sql tuning, redis

Why is the page size 4K?

The cpu can handle the data width at one time, and the bus size is 4k

Index data structure, why use this structure

B+ tree1
, B+ tree is a multi-channel balanced search tree, the height of the tree is balanced, the efficiency of multiple searches is average, the search time is short2
, the multi-channel feature of the B+ tree allows the database storage engine to obtain one more page of data. Improve query efficiency and hit rate

Clustered index and non-clustered index

Myaism non-clustered index, data and index are stored in files, and the leaf nodes of the slow primary key index tree and auxiliary index tree both store pointers to data pages. You can find data by walking both trees

Innodb clustered index, the data and index are stored in a file, the data row is stored in the leaf node of the primary key index tree, and the leaf node of the auxiliary index stores the pointer of the primary key index. If you are walking the auxiliary index tree, you need to go back to the table to find the data

So myaism is suitable for frequently updated data

mysql's execution plan explain + SQL statement

type:system>const>eq_ref>ref>range>index>ALL

[1] system: There is only one row of records in the table (system table), which usually does not appear;
[2] const: means that it can be found once by index , const is used to compare primary and unique index
cows
[3] eq_ref: unique index scan , For each index key, there is only one record in the table that matches it. Often used for primary key or unique index scans. eg: CEO department;
[4] ref: non-unique index scan, return all rows matching a single value;
[5] rang: only retrieve rows in a given range, and use an index to select rows. Generally, queries such as between, <, >, in, etc. appear in the where statement. This range scan index is better than a full table scan, because you only need to start at one point of the index and end at another point, without scanning all indexes;
[6] index: Full Index Scan, the difference between index and ALL is that the index type only traverses the index tree , The index file is usually smaller than the data file. index is read from the index, and All is read from the hard disk;
[7] ALL: read from the disk

extra:

[7] Using index condition: whether to push down the index

Index condition pushdown

Pass part of the judgment conditions to the storage engine.
Instead, the engine filters the result pages according to the query conditions to the server of the database.
Reduce the number of table lookups, reduce data reading, and improve query efficiency

How to do slow SQL optimization?

1. Create an index: create a suitable index, we can query in the index, and after the query is found, we can directly find the corresponding amount record2
, sub-table: when a table has more data or the value of some fields of a table When there are many and rarely used, use horizontal or vertical table to optimize, such as spu table
3, read-write separation: when a server cannot meet the needs, use the method of separating read-write for clustering, one machine Write, read by other machines
4. Cache: use redis for caching

mysql index failure

Or, like, function conversion, the value is not according to the type, for example, the int type is quoted.
As for whether in will index failure, it may be a problem with the mysql version, or it may be due to a field type conversion problem

Paging function limit

MySQL uses limit for paging, oracle uses rownum for paging
table LIMIT 5, 10; // Retrieve record rows 6-15
LIMIT 95,-1; // Retrieve record rows 96-last
. // From 96 to the maximum LIMIT 5; // Retrieve The first 5 record rows The
first one returns the offset of the record row, the second parameter specifies the maximum number of the returned record row

The characteristics of the transaction

Atomicity, all operations on the data either fail or succeed.
Consistency: the transaction starts and completes, and the data remains consistent.
Isolation: transactions are independent of each other, and the intermediate state is not visible to the outside world.
Persistence: data modification is permanent

Database transaction isolation level

4 isolation levels
1. Read uncommitted
transaction B can read the uncommitted data of transaction A, which breaks the isolation. Once transaction A rolls back, transaction B reads dirty data.
2. Read commits
a transaction only to query Other transactions have committed data, but the transaction will read the committed data, which will cause the data to change when the transaction reads the data repeatedly. Non-repeatable reads destroy the consistency (update and delete)
A transaction updates or deletes data. After submission, B The data in the first query and the second query of the transaction are inconsistent, that is, non-repeatable read
3. Repeatable read
a transaction reads data that does not exist for itself multiple times, the first time does not exist, and the second time exists, it is consistent (Insert) For
example, transaction A query data is 7 items, transaction Binsert has 8 items, and it is submitted. Because the row lock is added, not the table lock, then transaction A updates all the values ​​of a field in the table and finds that 8 pieces of data are updated, transaction A phantom reads
4. Serialization

The realization of innoDB transaction isolation level

Locking mechanism:
ru: no lock
is added when the transaction is read rc: row-level shared lock is added when it is read, and it is released immediately after reading, not the end of the transaction
rr: row-level shared lock is added when entering, and after the transaction is locked Release
se: add table-level shared lock, release only after the end of the transaction

MVCC multi-version concurrency control

Generate a data snapshot is
actually CAS version control and separation of read and write, the main scope of rc and rr levels

Table lock granularity and execution efficiency

Table locks have the largest granularity and low execution efficiency

The difference between table shared lock and table exclusive lock

The lock added when the table is read allows other transactions to read but not writes. It is a shared
lock when the table is written, and it does not allow reading and writing.

Row lock

Row lock has low granularity, small conflicts, and is not easy to deadlock

Innodb default row lock, myasim does not have the concept of row lock, this is one of the differences between the two

The specific implementation and difference between innodb row lock and table lock

Innodb uses row locks when walking the index. At this time, it locks the index columns instead of all data.
Global queries use table locks, locks

InnoDB gap lock

When we use the range condition query instead of the equivalent condition query, InnoDB will lock the eligible range index, and the records that do not exist in the condition range are called "gap (GAP)"

Innodb and myasim deadlock

Innodb will deadlock
myasim will not deadlock, because the table lock is added, which is serialized.
But innodb concurrency efficiency is also high

Distributed self-growing primary key

1. With self-increasing sequence, you can specify the start, stop and step length.
2. uuid
3. Snowflake algorithm

Redis
Redis version used in the project

Redis usage scenarios in the project

How does Redis ensure high availability

Redis election process

The difference between Redis and Memcache

Redis cluster mode

Redis cluster needs to increase shards, how to ensure lossless slot migration

Implementation of Redis distributed lock

Redis's strategy for deleting expired keys

Redis's memory elimination strategy

Redis's Hash object underlying structure

The expansion process of Hash objects in Redis

Will there be any problems with the expansion process of Redis's Hash object when the amount of data is large?

What are the persistence mechanisms of Redis

The realization principle, advantages and disadvantages of RDB and AOF

AOF rewriting process

Principles of Sentinel Mode

When using the cache, whether to operate the database first or operate the cache first

Why invalidate the cache instead of updating the cache

Cache penetration, cache breakdown, cache avalanche

Several design patterns for updating the cache

Guess you like

Origin blog.csdn.net/u010010600/article/details/109203044