Home >Database >Mysql Tutorial >Examples to explain database optimization
Searching for database optimization on the Internet basically optimizes from the SQL level, and rarely mentions the instance optimization of the database itself. Even if there is, it is all based on instance optimization of a specific database. This article covers instance optimization of all mainstream databases currently on the market (Oralce, MySQL, POSTGRES, Dameng). According to the configuration of the article, you can use 80% or more of your database performance. above.
Database Optimization Methodology
This part is theoretical knowledge. Students who are not interested can jump directly to the parameter configuration part.
Database optimization goals
Recommended "mysql video tutorial"
According to different roles, Database optimization is divided into the following goals:
Business perspective (key users):
Reduce user page response time
Database perspective (development):
Reduce database SQL response time
Database server perspective (operation Dimension):
Make full use of the physical resources of the database server
Reduce the CPU usage of the database server
Reduce database server IO usage
Reduce database server memory usage
##Indicators
1. The average SQL response time becomes shortera. Before optimization: The average database response time is 500msb. Optimization target: The average database response time is 200ms2. Database server The CPU usage decreasesa. Before optimization: CPU usage is 70% during database peak periodb. Optimization target: CPU usage is 50% during database peak period3 . Database server IO usage becomes lowa. Before optimization: database IO WAIT is 30%b. Optimization target: database IO WAIT is less than 10%Database optimization misunderstandings
There may be the following misunderstandings when optimizing databases:1. Before optimizing, you must have a deep understanding of the internal principles of the databaseOptimization has "routines", and you can also complete database optimization well by following these "routines" 2. Continuously adjusting database parameters can ultimately achieve optimization Sometimes design No matter how unreasonable the parameters are adjusted, it will not work3. Continuously adjusting the operating system parameters can ultimately achieve optimization Same as above4. Database performance is determined by the application and database architecture, and It has little to do with application developmentOn the contrary, it has a lot to do with application development5. It is necessary to separate reading and writing, and it must be divided into databases and tablesThe amount of data Only when the level reaches a certain ratio is it necessary to separate reading and writing, and divide the tables into separate databases. Otherwise, it will only increase the complexity. Generally speaking, the single table size of Oracle can reach 100 million, and that of MySQL can reach 10 million~20 millionDatabase optimization process
The complete database optimization process is as follows: #First of all, you need to understand the optimization problem as much as possible, collect system information during the problem and archive it. Develop optimization goals based on current system problem performance and communicate with customers to reach agreement on goals; analyze system problems through a series of tools and formulate optimization plans. After the plan review is completed, each person in charge will implement it. If the optimization goal is achieved, an optimization report will be compiled; otherwise, the optimization plan will need to be re-formulated.Database instance optimization
Database instance optimization follows three mantras:The log cannot be small, the cache must be large enough, and the connection must be sufficient.
After the database transaction is submitted, the modifications to the data page by the transaction need to be flushed (fsync) to the disk to ensure the durability of the data. This disk flush is a random write with low performance. If the disk is flushed every time a transaction is submitted, it will greatly affect the performance of the database. The database will adopt the following two optimization techniques in the architecture design: a. First write the transaction to the log Examples to explain database optimization RedoLog (WAL), and optimize random writing into sequential writing b. Add one The layer cache structure Buffer optimizes each write into a sequential writeSo logs and caches are particularly important for database instances. If there are not enough connections, the database will directly throw an exception and the system will be inaccessible. Database parameter optimizationMainstream database architectures all have the following in common:Data cacheSQL parsing areaSort memoryREDO and UNDOLock, LATCH, MUTEXMonitoring and connectionFile reading and writing performanceNext we adjust the parameters according to different databases to achieve the best performance of the database.
ORACLE
Parameter classification | Parameter name | Parameter value | Remarks |
---|---|---|---|
SGA_TAGET, MEMORY_TARGET | Physical memory 70-80% | The bigger the better | |
DB_CACHE_SIZE | Physical memory 70-80% | The bigger the better | |
SHARED_POOL_SIZE | 4-16G | It is not recommended to set it too large | |
PROCESSES, SESSIONS, OPEN_CURSORS | Set according to business requirements | Generally 120% of the estimated number of business connections | |
SESSION_CACHED_CURSORS | is greater than 200 | Soft Analysis |
MYSQL(INNODB)
Parameter name | Parameter value | Remarks | |
---|---|---|---|
INNODB_BUFFER_POOL_SIZE | Physical memory 50-80% | Generally speaking, the larger the better the performance | |
Innodb_log_buffer_size | 16-32M | Adjust according to the operating conditions | |
sync_binlog | 1, 100, 0 | 1 Best security | |
max_connections | Adjust according to business conditions | can be pre-set Leave part of the value | |
innodb_flush_log_at_trx_commit | 2 | Security and performance compromise considerations | |
wait_timeout, interactive_timeout | 28800 | Avoid scheduled application connection interruptions |
POSTGRES
Parameter name | Parameter value | Remarks | |
---|---|---|---|
SHARED_BUFFERS | Physical memory 10-25% | ||
CACHE_BUFFER_SIZE | Physical memory 50-60% | ||
wal_buffer | 8-64M | It is not recommended to set it too large or too small | |
max_connections | Adjust according to business conditions | Generally 120% of the estimated number of business connections | |
maintenance_work_mem | 512M or larger | ||
work_mem | 8-16M | The original configuration 1M is too small | |
checkpoint_segments | 32 or larger | |
Parameter name | Parameter value | Remarks | |
---|---|---|---|
MEMROY_TARGET, MEMROY_POOL | Physical memory 90% | ||
BUFFER | Physical memory 60% | Data cache | |
MAX_BUFFER | Physical memory 70% | Maximum data cache | |
max_sessions | Set according to business requirements | Generally 120% of the estimated number of connections for the business |
There are too many ways to optimize the database, including replacing disk arrays and upgrading hardware, rewriting SQL scripts to add indexes, adjusting database parameters to optimize performance, and even adjusting the database. architecture. This article optimizes the parameters of the database itself. By adjusting the parameters in the above tables, you can basically achieve 80% of the best performance of the database.
This article comes from the php Chinese website,
mysql tutorialThe above is the detailed content of Examples to explain database optimization. For more information, please follow other related articles on the PHP Chinese website!