Database tuning layered thinking 1. Tuning strategy
1) * Number processing (only the necessary fields are extracted, reducing the flow rate)
It is best to use, useful fields to reduce traffic.
Table structure will change, increase or decrease a column, if the * number of all queries will cause code logic error.
2) Large SQL (split, progressively narrow result set)
Large SQL is very time-consuming to execute, where a string clause is followed, or a read-table union query. or temporary tables temporarily store result sets
3) a reasonable index (the condition following the WHERE clause)
4) type conversion (' Use of symbols ')
In the query operation, take the "
5) Try not to use a range query, or narrow the scope of the search (Program logic Update)
<,>,like, so vague query, bad,
2. Which SQL statements need to be tuned
1) Looking for development to
2) View program log (interface--background service)
3) View slow query log
4) View statistics: Maximum calls, maximum traffic, slowest query (100ms). Monitoring System,
5) Show full processlist
3.SQL Configuration Level
3.1 Which configurations need attention
Query_cache:mysql features, query cache, saved result set, put in cache
Max_connection: Maximum number of links in the database. You can change the number of links, and the connection pool of the business program to do the corresponding relationship.
Wait_timeout: Data is detected every eight hours if there is no link to access it. It's closed. Avoid this problem by linking the database every eight hours to ensure that the program always accesses it
Lock: Process operation, to ensure that the other process does not change him, so a process to operate it, the first lock up, and so on and then open to another process to use.
If it takes a long time, the other program is waiting.
To solve the lock problem, an SQL statement cannot be executed too long, and large SQL is decomposed into small SQL.
4.SQL Architecture Level
1) Master-slave replication (read/write separation)
If both read and write to a table, the CPU soars. The pressure is big and so on.
A copy of the database can be copied to several databases, read when read from the library, write in the main library.
2) Sub-database sub-table
3) Load Balancing
Lv5,f5,nginx
Load Balancing is a more uniform distribution of pressure between databases
4) Multilevel Cache
Redis
5. Business-Level thinking
1) Table field splitting
Some columns are not commonly used, not even
2) text (for Key-value)
Split up, create a new detail table
Convenient operation and maintenance (DDL), easy to cache, improve query performance
Cold Data Backup
Export historical data once a year to narrow the scope of the search
Processing of program Logic
Example of Update full table
The idea of database tuning and layering