MySQL database optimization, see this is enough

Foreword

Database optimization on the one hand is to identify system bottlenecks and improve the overall performance of the MySQL database, on the other hand require reasonable structure design and parameters adjusted to improve the appropriate speed of the user, but also to conserve system resources as possible in order to allow the system provides a larger load.

1, FIG optimization glance

MySQL database optimization, see this is enough

2, optimization

I will optimize divided into two categories, soft and hard optimization optimization, software optimization can generally operate the database, but it is hard to optimize server hardware and operating parameters.

2.1 optimization software

2.1.1 query optimization

1. First, we can use EXPLAIN or DESCRIBE (abbreviation: DESC) command to analyze a query execution information.

2. Example:

DESC SELECT * FROM `user`

display:

MySQL database optimization, see this is enough

Which shows the index and query data read information such as the number of data pieces.

2.1.2 sub-query optimization

In MySQL, try to use JOIN instead of subqueries Because subqueries need nested queries, will create a temporary table when nested queries, build and delete temporary table, there will be a large overhead, but will not join query create a temporary table, the efficiency is higher than the nested sub-queries.

2.1.3 Index

The index is a database query to improve the speed of one of the most important ways you can participate on a high index author <MySQL database index> article describes in more detail, here recorded three considerations using the index:

1, LIKE keyword matching strings that begin with '%', does not use the index.

2, two fields are OR keyword must use the index, the query will use the index.

3, using a multi-column index must meet the leftmost match.

2.1.4 decomposition table

For many fields of the table, if some lower frequency fields, should this case, be separated out to form a new table,

2.1.5 middle of the table

For a large number of tables join query can create an intermediate table, thereby reducing connection time-consuming due at the time of the query.

2.1.6 increase redundancy field

Similar to creating middle of the table, but also to reduce the increase redundancy join query.

2.1.7 Analysis Table, checklists, optimize table

The main analysis table is distributed analysis table keywords, checklists primarily Check for errors in the table, the table is mainly optimized to eliminate deleted or updated table space caused by waste.

1, the analysis table: Use ANALYZE keyword, such as ANALYZE TABLE user;

MySQL database optimization, see this is enough

  1. Op: indicates the operation to perform.

  2. Msg_type: type of information, there is status, info, note, warning, error.

  3. Msg_text:显示信息.
2、检查表: 使用 CHECK关键字,如CHECK TABLE user [option]

option 只对MyISAM有效,共五个参数值:

  1. QUICK:不扫描行,不检查错误的连接.

  2. FAST:只检查没有正确关闭的表.

  3. CHANGED:只检查上次检查后被更改的表和没被正确关闭的表.

  4. MEDIUM:扫描行,以验证被删除的连接是有效的,也可以计算各行关键字校验和.

  5. EXTENDED:最全面的的检查,对每行关键字全面查找.
3、优化表:使用OPTIMIZE关键字,如OPTIMIZE [LOCAL|NO_WRITE_TO_BINLOG] TABLE user;

LOCAL|NO_WRITE_TO_BINLOG都是表示不写入日志.,优化表只对VARCHAR,BLOB和TEXT有效,通过OPTIMIZE TABLE语句可以消除文件碎片,在执行过程中会加上只读锁.

2.2 硬优化

2.2.1 硬件三件套

1、配置多核心和频率高的cpu,多核心可以执行多个线程.

2、配置大内存,提高内存,即可提高缓存区容量,因此能减少磁盘I/O时间,从而提高响应速度.

3、配置高速磁盘或合理分布磁盘:高速磁盘提高I/O,分布磁盘能提高并行操作的能力.

2.2.2 优化数据库参数

优化数据库参数可以提高资源利用率,从而提高MySQL服务器性能.MySQL服务的配置参数都在my.cnf或my.ini,下面列出性能影响较大的几个参数.

  • key_buffer_size:索引缓冲区大小

  • table_cache:能同时打开表的个数

  • query_cache_size和query_cache_type:前者是查询缓冲区大小,后者是前面参数的开关,0表示不使用缓冲区,1表示使用缓冲区,但可以在查询中使用SQL_NO_CACHE表示不要使用缓冲区,2表示在查询中明确指出使用缓冲区才用缓冲区,即SQL_CACHE.

  • sort_buffer_size:排序缓冲区

传送门:更多参数

https://www.mysql.com/cn/why-mysql/performance/index.html

2.2.3 library sub-table

Because the database too much pressure, the first problem is the peak system performance may be reduced, because of the impact on the performance of the database load is too high there. Another, had a lot of pressure on your database to hang out how to do? So at this point you have to make to the system + sub-library separate read and write sub-table, which is a library to split into multiple libraries, deployed on a plurality of database services, then the write request as the main repository bearer. Each primary library is then mounted at least one from the library, from the library to be requested by the read bearer.

MySQL database optimization, see this is enough

2.2.4 cache clusters

If the number of users growing, then you can keep adding machine, such as system level constantly adding machine, it can carry higher concurrent requests. Then write concurrent database level if more and more, plus a database server on expansion through sub-library sub-table is to support the expansion of the machine, if the database level concurrent read more and more, the expansion would add more from the library. But there is a big problem: The database itself is not actually used to carry high concurrent requests, so generally speaking, concurrent database single carrier per second in the order of thousands, and the database used by the machine are relatively high-profile , more expensive machines, the cost is high. If you're simply stop adding machines, in fact, it is wrong. So often have high concurrency architecture in this part of the design cache, the cache system is to carry high concurrency born. Therefore, the amount of concurrent single carrier per second are tens of thousands, even hundreds of thousands per second, concurrent high load carrying capacity than the database system to be higher than one to two orders of magnitude. So you can be based on business characteristics of the system, of the kind of writing less reading and more requests, the introduction of cache cluster. Specifically, it is at the same time to write a database of data when writing to the cache cluster, and then use the cache cluster to host most of the read request. In this case, by the cache cluster, the machine can use fewer resources carry higher concurrency.

MySQL database optimization, see this is enough

Epilogue

A complete and complex high concurrency system architecture, will include: a variety of complex infrastructure systems since the inquiry. A variety of exquisite architectural design. So small article at the most have the effect of better ideas, but the database optimization ideas almost on these.

Guess you like

Origin blog.51cto.com/14230003/2433341