Table horizontally divided
If a number of records in the table too much, such as the 10 million, and the need for frequent retrieval, then we have a need to split up. If I split into 100 tables, each table only 100,000 records. Of course, this need can be logically divided into data. A good division basis, in favor of a simple implementation of the program, you can also take advantage of the horizontal component of the table. Such as providing the only system interface function queries monthly, monthly then split into the table 12, a table for each query only query is enough. If I have to be divided according to the region, even if the demolition and then a small table, a query or to all joint table to check, it might as well not demolished. So a good split basis is the most important. Keywords: UNION
Example:
- Orders table according to the order of time to produce sub-table (a year)
- Student Fact Sheet
- Inquiries fee, nearly three months of data into a table, another table to put in a year
Vertical division of the table
Some table records the number is not much, may also 2,3 million, the field is very long, however, a large table space, you need to perform a large number of I / O while retrieving table, seriously degrade the performance. This time need to split a large field to another table, and the table with the original table is a one to one relationship. (JOIN )
When the content of [questions], [information] answer two tables, originally added as a field to a few questions [information] in, you can see the content of the questions and answers of these two fields is very long, there are 30,000 records in the table table has accounted for 1G of space, in the column list of questions is very slow. After analysis, we found that the system is often based on [book], [unit], type, type, degree of difficulty query, Pagination questions details. And each time retrieving all these tables do join, each table to be scanned again. 1G. We can split the content and answers to another table, only display the details of this when it read a large table, which gave rise to questions [content], [information] answer two tables.
Select the appropriate field type, in particular primary key
General principles of selection field is small insurance does not protect the large, can take up small-byte field would not have a large field. For example, the primary key, we recommend using the self-energizing type so save space, space is efficiency! By 4 bytes and 32 bytes by positioning a record, who fast who slow too obvious. When it comes to several tables do join, the effect is even more apparent.
Files, images and other large files are stored with the file system
Database only stores the path. Pictures and documents stored in the file system, even on a single server (Figure bed / video server) alone.
Database configuration parameters
The most important parameter is the great memory, we mainly use the innodb engine, so the following two parameters tone
innodb_additional_mem_pool_size = 64M innodb_buffer_pool_size =1G
For myisam, need to be adjusted key_buffer_size, of course, adjust the parameters will depend on the state, you can see the current status with the show status statement to determine which parameters to adjust to change
Modify the port in 3306 my.ini, default storage engine and the maximum number of connections
In the my.ini. Port = 3306 [modified in two places] default = INNODB-Storage-Engine max_connections = 100
Reasonable hardware and operating system resources
If your machine memory more than 4G, it should be no doubt that the use of 64-bit operating system and 64-bit mysql 5.5.19 or mysql5.6
Separate read and write
If the database is a lot of pressure, a machine can not support, you can copy multiple machines using mysql to achieve synchronization, the pressure on the database dispersed.
Master
Slave1
Slave2
Slave3
Main library master for writing, slave1-slave3 are used for select, each database shared pressure a lot smaller.
To achieve this, we need a program specifically designed to write all operations master, read all operating slave, to the program developed an additional burden. Of course, there are already middleware agent to achieve this, the program to read and write database which is transparent. There is the official mysql-proxy, but still alpha version. Sina have amobe for mysql, you can achieve this purpose, the following structure
Time to complete the backup of the database
The actual needs of the project, complete the scheduled backup a database, or operating some scheduled backups of the database table
Every 1 hour windows, back up data newsdb
The windows every night at 2:00 a backup newsdb a table
cmd> mysqldump -u root -p password database name> into the database to a directory
All tables cases, the backup database mydb
Enter the directory where mysqldump
cmd> mysqldump -u root -phsp shop> d: /shop.log [shop all database tables derived All]
cmd> mysqldump -u root -phsp shop temusers emp> d: /shop2.log [temusers shop database export and emp]
How to recover data table
Enter the mysql user interface
The full path mysql> source backup files
:( The scheduled backup command to write to ask the my.bat)
windows How regularly back up (2:00 a.m.)
Using windows built-in scheduled task, the timing to execute the batch command.
Incremental Backup and Restore
Definitions: mysql database will be in binary form, the automatic operation of the user mysql database records to a file, when users want to restore, you can use the backup files to restore.
Incremental backups record dml statements, table creation statements and are not recorded select. Record things include: sql statement itself, hours of operation, location
Step incremental backup and recovery
Note: The previous version of mysql5.0 and does not support incremental backups
1, the configuration file my.ini or my.conf, enable binary backup.
Open the my.ini file, find the log-bin, configure: log-bin = G: \ Database \ mysqlbinlog \ mylog
In G: \ Database directory new directory mysqlbinlog
2, restart the mysql service
This time will see the following two files in the following directory mysqlbinlog:
mylog.000001: log backup files. To view this information inside the log files, we can use the program to view mysqlbinlog, mysqlbinlog program stored in the mysql bin directory ( "C: \ Program Files \ MySQL \ MySQL Server 5.6 \ bin").
Implementation of sql statement
UPDATE emp set ename='zouqj' where empno=100003;
Start - Run --cmd, mysqlbinlog backup file path
C:\Program Files\MySQL\MySQL Server 5.6\bin>mysqlbinlog G:\Database\mysqlbinlog\mylog.000001
mylog.index: log index file, which records it in the log file. (G: \ Database \ mysqlbinlog \ mylog.000001)
3, suppose now the question is, I have this update is a mistake, how to recover
In the mysql log records the time and location of every operation, so we both can be restored based on time, it can be restored based on location.
Well, right now we can see from the chart, produced by the time this statement is "2016-04-17 12:01:36", the location is 614
Time to recover
We can choose to produce in time before the second statement
Execute cmd command: mysqlbinlog --stop-datetime = "2016-04-17 12:01:35" G: \ Database \ mysqlbinlog \ mylog.000001 | mysql -uroot -p
This time I will execute SQL statements View
SELECT * from emp where empno=100003;
It turned into a
To recover by location
Execute cmd command: mysqlbinlog --stop-position = "614" G: \ Database \ mysqlbinlog \ mylog.000001 | mysql -uroot -p
This time to execute SQL to see the results, then change back.