MYSQL performance optimization sharing (sub-database sub-table)

1. The sub-database and sub-table
are obviously, the unrestricted growth of a main table (that is, a very important table, such as a user table) is bound to seriously affect the performance, sub-database and sub-table are a very good solution, that is, performance optimization Approach, the current case is that we have a user table members with more than 10 million records, which is very slow to query. My colleagues' approach is to hash it into 100 tables, from members0 to members99, and then distribute records according to mid In these tables, the awesome code looks like this:
<?php
for($i=0;$i< 100; $i++ ){
//echo "CREATE TABLE db2.members{$i} LIKE db1.members <br>";
echo "INSERT INTO members{$i} SELECT * FROM members WHERE mid%100={$i}<br>";
}
?>


2. Modifying the mysql table structure without stopping is
also the members table. The table structure designed in the early stage is not reasonable. With the continuous operation of the database, its redundant data is also growing hugely. My colleagues used the following method to deal with it:

first create a temporary table :
/*Create a temporary table*/
CREATE TABLE members_tmp LIKE members

Then modify the table structure of members_tmp to a new structure, and then use the above for loop to export data, because it is wrong to export 10 million data at one time, mid is the primary key, a The guide of an interval is basically to export 50,000 items at a time, which is omitted here, and
then rename and replace the new table:

/*This is quite a classic statement*/
RENAME TABLE members TO members_bak,members_tmp TO members;

That's it, basically no loss can be achieved, and there is no need to stop the table structure to update, but in fact, the table is locked during RENAME, so it is a trick to choose to operate when there are few online. After this operation, the original table with more than 8G has suddenly become more than 2G.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989820&siteId=291194637