Introduction
Do you encounter the interview, the interviewer asked
What should be the answer to this question? In fact, the reason for writing this theme is that I have seen the public in the past few days of a database tuning knowledge (not on the link), I have slightly turned a few, above all the time to the database to be split horizontally, I would like to ask you readers, you have experienced horizontal split? Now many articles, practicality is too poor, Only pure theoretical analysis can be said.
This article first came to know a question, I on the basis of a perfect.
First stage optimized SQL and indexes
This is the first stage of tuning, why?
Because this step is the lowest cost Ah, do not need to add any middleware. You didn't go through the index optimization and SQL optimization, it is not deceptive to what level of split.
What was the step like? I said a ballpark figure.
(1) Use slow query log location to execute inefficient SQL statements
(2) Analysis of SQL execution plan with explain
(3) Determine the problem, take the corresponding optimization measures, build index AH, etc.
I will not give an example, because how to optimize the SQL article, a catch a lot, and then paste it over, the reader is tired to look at.
Second Stage build cache
You should consider building a cache if you are not able to solve the problem by optimizing SQL. After all, you use caching to cache complex, time-consuming, infrequently executed results and reduce the resource consumption of the database.
It is important to note that the complexity of the system increases after the cache is built. You need to consider a number of issues, such as:
Cache and database consistency issues? (for example, more cache, or delete cache), this can be seen in my article, "Database and cache double write consistency scheme resolution."
How to resolve cache breakdown, cache penetration, cache avalanche issues? Whether there is a need to do cache preheating. But I guess most of the small and medium companies should not think about it. This can be seen in my other article, "Distributed Redis Review explaining"
Third stage read and write separation
Cache also can not make the case, engage master-slave replication, on the read and write separation. At the application level, distinguish between read and write requests. Or use out-of-the-box middleware Mycat or Altas to do read and write separation.
Note that as long as you dare to say that you are using a master-slave architecture, there are three questions that you need to prepare:
(1) master-slave benefits?
Answer: Database backup, database load balancing, improve database availability
(2) Principal and subordinate principle?
Answer: (the picture is not self-drawn, lazy)
The main library has a log dump thread that passes the Binlog from the library
From the library there are two threads, one I/O thread, one SQL thread, the I/O thread reads the Binlog content from the main library and writes to the relay Log,sql thread reads the contents from the relay log, writes the database from the library.
(3) How to solve the master-slave consistency?
Answer: This question, I do not recommend to solve the problem at the database level. According to the cap theorem, the master-slave architecture is inherently a highly available architecture and cannot satisfy the consistency.
Even if you use synchronous replication mode or semi-synchronous replication mode, it is weak consistency, not strong consistency. Therefore, it is recommended to use caching to solve the problem.
The steps are as follows:
1, own through the test, calculate the master-slave delay time, the proposed MySQL version is 5.7, because MySQL since 5.7, the multi-threaded copy function is perfect, generally can guarantee the delay within 1s. But then again, MySQL is now out to 8.x, and others use the 5.x version.
2, the database write operation, writes the database first, then writes the cache, but the validity period is very short, is slightly longer than the master-slave delay time.
3, read the request, the first read the cache, the cache exists directly back. If the cache does not exist (at this point the master-slave synchronization is completed), read the database again.
Phase IV using partitioned tables
To say the truth, when you interview, you can actually skip this stage. Because many internet companies do not recommend the use of partitioned tables, I do not recommend using partitioned tables, using this partition table, too many pits.
Here's a quote from the other articles:
What is the MySQL partition table?
Answer: All data is still in one table, but physical storage is placed in different files according to certain rules. This is a feature that MySQL supports, the business code does not need to change, but the SQL statement needs to change, the SQL condition needs to bring the partition column.
Disadvantages
(1) partition key design is not very flexible, if you do not take the partition key, it is easy to appear full table lock
(2) Use ALTER TABLE in partition Table ... Order BY, you can only order by within each partition.
(3) partition Key of partition table is created index, then this index will be partitioned. The partition key does not have global index one said.
(4) own sub-database sub-table, own control of business scenarios and access mode, controllable. Partition table, research and Development wrote a SQL, are not sure which partition to check, not too controllable.
... Not listed, not recommended
Fifth stage vertical split
The above four stages have not been done, it is vertical split. The complexity of the vertical split is still smaller than the horizontal split. Split your table into different small tables by module. Everyone should have seen the "Big Website Architecture Evolution Road", this kind of article or book, basically have mentioned this stage.
If you are lucky enough to work in a company such as a carrier or a bank, you will find them a table and hundreds of fields are common. Therefore, it should be split, the principle of splitting is generally the following three points:
(1) Place infrequently used fields in a single table.
(2) Put the usual fields in a single table
(3) The columns of the frequently combined query are placed in a table (federated index).
Sixth stage horizontal split
OK, horizontal split is the most troublesome stage, after splitting there will be a lot of problems, I emphasize once again, horizontal split must be the most final choice. In a sense, I think it's better to split vertically. Because you split the vertical, divided into different modules, and found that the pressure of the single module is too large, you can completely optimize the module alone, such as improving the machine configuration of the module. If the horizontal split, split into two tables, the code needs to change, and then found two tables not yet, and then change the code, and then split into three tables? After horizontal splitting, the coupling between the modules is too strong, the cost is too large, cautious.
Several stages of database optimization