1. The advantages and disadvantages of using indexes?
The index can locate the page of the book very quickly like the book directory.
Advantages: improve query efficiency. If there is no index, query the database table will perform a full table scan.
Disadvantages: slow insertion, occupying hard disk space
2. What is the difference between Myisam and Innodb B + tree?
Myisam | Innodb | |
---|---|---|
Leaf node | Value stores the data address, query data by address | Value stores data |
advantage | It is faster when creating an index | Query data is faster |
Disadvantages | Query data is slow | Index creation is slow |
3. What is the difference between leaf nodes and non-leaf nodes in the B + tree?
Leaf node | Non-leaf node | |
---|---|---|
the difference | There are Key and Value | Only Key |
4. Comparison of the four methods of indexing in MySQL
Hash | Balanced binary tree AVL | B tree | B + tree | |
---|---|---|---|---|
advantage | The query speed is very fast according to the hash algorithm | High query efficiency | Nodes can store multiple data, reducing the height of the tree and the number of IO, the query efficiency is better | Inheriting the characteristics of B-tree, there are more leaf nodes and non-leaf nodes, the order is stored between leaf nodes, the query efficiency is fast and the range query is also fast |
Disadvantages | Data storage is out of order, and range query cannot be performed | Can range query, but need to maneuver, the efficiency is very low | Support range query, it will also maneuver, and the efficiency is not high | Because there is redundant node data, it will occupy the size of the hard disk, but the advantages are far greater than the disadvantages |
Five, MySQL master-slave replication principle
Six, MySQL sharded cluster principle
7. How does the MySQL sharded cluster guarantee unique IDs?
1. UUID: not self-increasing
2. Snowflake algorithm: not self-increasing
3. Set step size: the starting value and step size must be defined at the beginning, the starting value is 1, 2, 3 ... step size is divided Number of tablets
8. Introduction to MyCat
Alibaba ’s open source framework, a
reverse proxy real database written in java language.
Read-write separation: implement routingDatasource interface, configure dynamic data source, use aop to distinguish read-write operations. Sharding
cluster: split database tables horizontally
Mycat has three configuration files:
1. server.xml: Mycat configuration file, configuration account password, etc.
2. schema.xml: Myvat corresponding physical database and database table configuration
3. rule.xml: Mycat sharding Table) Rules
Nine, the difference between horizontal split and vertical split
Vertical split: For example, the member service corresponds to the member database and the WeChat service corresponds to the WeChat database. Disadvantages: need to communicate through the interface, can not cross the library check
horizontal split: a table stored in multiple databases, such as 6 , 3 databases, each database stores 2 data, using mycat middleware to find the data of the three libraries at the same time, take the mold to store the data
10. What is the difference between multiple data sources and dynamic data sources?
Multi-data source: subcontracting form
Dynamic data source: dynamic data is constantly switched in jvm
11. SpringBoot dynamic data source switching principle
Process:
1. Use AOP to determine the prefix of the business logic layer method. If the prefix is such as select, get, find, etc., findUser
2. If it is a write operation, a key will be passed to RoutingDataSource
Configuration:
1. Create a data source for reading and writing
2. Register the data source to RoutingDataSource
3. Only use AOP technology to intercept the business logic layer method and determine whether the method prefix needs to be read or written
Note:
1. The data source is placed in the RoutingDataSource in the form of Map
2. Use the Order (0) annotation to make aop execute before the transaction
12. Mycat achieves 9 strategies for horizontal sharding
1. Region: judging the region by the parameters passed by the client, enumerating algorithms, enumerating files
2. Modulus: according to the number of id% database clusters
13. How does Mycat query all the data after sub-table and sub-database?
Convert select * from user
to
select * from db1_user
select * from db2_user
select * from db3_user
three queries
If there is a case with where id = 1, only one query statement needs to be sent. Because 1% 3 = 1, check directly from the second database
Paging query
select * from user limit 0, 2
Randomly paging query from a library, sending three statements, summing up the query data, and randomly returning a set of data.
select * from user order by id desc limit 0, 2
take the two largest
14. ShardingJdbc
Dangdang's open source database middleware
To achieve the principle of read-write separation:
1. It is necessary to configure the read-write separation jdbc connection in the configuration file to all ShardingJdbc. The master database and the slave database use binary files to achieve data synchronization.
2. ShardingJdbc will automatically determine the SQL statement type (DML or DQL). If it is a DML sentence, it will send a request to the jdbc connection configuration of the lake district master. If it is a DQL sentence, get the slave jdbc connection to send the request
DML create/delete/update/inster
Fifteen, the difference between ShardingJdbc and MyCat
MyCat intercepts all Jdbc requests from the client (dao) and uniformly forwards them to the real database
All parse SQL, rewrite SQL statements through routing rules
- ShardinJdbc is a Jar form, rewriting the native method of Jdbc in the local application layer to implement the database sharding form
- Mycat belongs to server-side database middleware
- ShardingJdbc is a local database middleware, there is
a difference between the type of ribbon and nginx to achieve load balancing
16. Why is the index invalid? What matters to note?
17. How does Mysql locate slow queries
Eighteen, SQL statement optimization
19. What is the difference between half-line synchronization and parallel synchronization?
twenty,
twenty one,
twenty two,
twenty-three,
twenty four,
twenty five,
Twenty-six,
Twenty-seven,
Twenty-eight,
Twenty-nine,
thirty,
thirty-one,
Thirty-two,
Thirty-three,
Thirty-four,
1. Configuration file format
2. Implement the specified interface.
Slow query needs to be enabled. The
default statement does not respond within 10 seconds is slow query
. Read from the specified log file
Sharding = sub-table sub-library
Master-slave, sub-table sub-library
When will the index perform IO operations
The difference between mechanical hard drives and solid state drives?
What is the difference between data structure Hash, balanced binary tree, B tree, and B + tree?
Hash: The length of the array is N, and the Hash value is Hash (name) = X, then the storage location is the remainder of X% N
Balanced binary tree (invented by three AVL scientists): it
will take the middle value, such as inserting 1, 2, 3, after balance, 2 will be the root node, 1 is the left subtree, and 3 right subtrees
For example, the primary key index, the key is the primary key.
There are both leaf nodes and non-leaf nodes, at the bottom of the tree
Because the index is persistent to the hard disk, so to query the data, the index should be read from the hard disk for comparison (IO). It needs to consume cpu resources.
Neither shardingjdbc nor mycat can achieve master-slave replication. The
nature of the disk IO
disk (I / O)-> memory (read to memory, compare in memory)-> CPU
IO: read the hard disk to memory
, files will be generated when the index is built, and there are many files with multiple indexes and multiple data
How many nodes are stored in the B + tree?
Index failure
How often is MySQL master-slave replication data synchronization performed? Or configure it yourself?
Monitor the binlog file, every time dmlsql is synchronized?
mycat achieves read-write separation (through permissions) and sub-table sub-database
Two ways to achieve read and write separation:
1. mycat, intercept all jdbc requests, pan selection, update distinction, route analysis, the client accesses mycat through the public network ip, there is a virtual database on mycat
2, dynamic data source aop, intercept The service method
is distinguished by the method header set by yourself, such as find
What is the difference between dynamic data sources and multiple data sources?
Dynamic data sources include multiple data sources
SpringBoot connects to the virtual database in Mycat and accesses other databases through Mycat.
Cloud database
Alibaba Cloud, Tencent Cloud, etc., do a lot of configuration automatically