search
HomeDatabaseMysql Tutorial学会设置五大类MySQL参数

学会设置五大类MySQL参数

Jun 07, 2016 pm 04:09 PM
mysqlwebparameterlearnset upconnect

(一)连接 连接通常来自Web服务器,下面列出了一些与连接有关的参数,以及该如何设置它们。 1、max_connections 这是Web服务器允许的最大连接数,记住每个连接都要使用会话内存(关于会话内存,文章后面有涉及)。 2、max_packet_allowed 最大数据包大小,通常

(一)连接

连接通常来自Web服务器,下面列出了一些与连接有关的参数,以及该如何设置它们。

1、max_connections

这是Web服务器允许的最大连接数,记住每个连接都要使用会话内存(关于会话内存,文章后面有涉及)。

2、max_packet_allowed

最大数据包大小,通常等于你需要在一个大块中返回的最大数据集的大小,如果你在使用远程mysqldump,那它的值需要更大。

3、aborted_connects

检查系统状态的计数器,确定其没有增长,如果数量增长说明客户端连接时遇到了错误。

4、thread_cache_size

入站连接会在MySQL中创建一个新的线程,因为MySQL中打开和关闭连接都很廉价,速度也快,它就没有象其它数据库,如Oracle那么多持续连接了,但线程预先创建并不会节约时间,这就是为什么要MySQL线程缓存的原因了。

如果在增长请密切注意创建的线程,让你的线程缓存更大,对于2550或100的thread_cache_size,内存占用也不多。

(二)查询缓存

MySQL中的缓存查询包括两个解析查询计划,以及返回的数据集,如果基础表数据或结构有变化,将会使查询缓存中的项目无效。

1、query_cache_min_res_unit

MySQL参数中query_cache_min_res_unit查询缓存中的块是以这个大小进行分配的,使用下面的公式计算查询缓存的平均大小,根据计算结果设置这个变量,MySQL就会更有效地使用查询缓存,缓存更多的查询,减少内存的浪费。 

2、query_cache_size

这个参数设置查询缓存的总大小。

3、query_cache_limit

这个参数告诉MySQL丢掉大于这个大小的查询,一般大型查询还是比较少见的,如运行一个批处理执行一个大型报表的统计,因此那些大型结果集不应该填满查询缓存。

qcache hit ratio = qcache_hits / (qcache_hits + com_select)

使用

SQL> show status like 'qcache%';

SQL> show status like 'com_%';

找到这些变量。

average query size = (query_cache_size - qcache_free_memory)/qcache_queries_in_cache

使用

SQL> show variables like 'query%';

qcache_* status variables you can get with:

SQL> show status like 'qcache%';

获取query_cache_size的值。

(三)临时表

内存速度是相当快的,因此我们希望所有的排序操作都在内存中进行,我们可以通过调整查询让结果集更小以实现内存排序,或将变量设置得更大。

tmp_table_size

max_heap_table_size

无论何时在MySQL中创建临时表,它都会使用这两个变量的最小值作为临界值,除了在磁盘上构建临时表外,还会创建许多会话,这些会话会抢占有限制的资源,因此最好是调整查询而不是将这些参数设置得更高,同时,需要注意的是有BLOB或TEXT字段类型的表将直接写入磁盘。  深入浅出MySQL双向复制技术

(四)会话内存

MySQL中每个会话都有其自己的内存,这个内存就是分配给SQL查询的内存,因此你想让它变得尽可能大以满足需要。但你不得不平衡同一时间数据库内一致性会话的数量。这里显得有点黑色艺术的是MySQL是按需分配缓存的,因此,你不能只添加它们并乘以会话的数量,这样估算下来比MySQL典型的使用要大得多。最佳做法是启动MySQL,连接所有会话,然后继续关注顶级会话的VIRT列,mysqld行的数目通常保持相对稳定,这就是实际的内存总用量,减去所有的静态MySQL内存区域,就得到了实际的所有会话内存,然后除以会话的数量就得到平均值。

1、read_buffer_size

缓存连续扫描的块,这个缓存是跨存储引擎的,不只是MyISAM表。

2、sort_buffer_size

执行排序缓存区的大小,最好将其设置为1M-2M,然后在会话中设置,为一个特定的查询设置更高的值。

3、join_buffer_size

执行联合查询分配的缓存区大小,将其设置为1M-2M大小,然后在每个会话中再单独按需设置。

4、read_rnd_buffer_size

用于排序和order by操作,最好将其设置为1M,然后在会话中可以将其作为一个会话变量设置为更大的值。

(五)慢速查询日志

慢速查询日志是MySQL很有用的一个特性。

1、log_slow_queries

MySQL参数中log_slow_queries参数在my.cnf文件中设置它,将其设置为on,默认情况下,MySQL会将文件放到数据目录,文件以“主机名-slow.log”的形式命名,但你在设置这个选项的时候也可以为其指定一个名字。

2、long_query_time

默认值是10秒,你可以动态设置它,值从1到将其设置为on,如果数据库启动了,默认情况下,日志将关闭。截至5.1.21和安装了Google补丁的版本,这个选项可以以微秒设置,这是一个了不起的功能,因为一旦你消除了所有查询时间超过1秒的查询,说明调整非常成功,这样可以帮助你在问题变大之前消除问题SQL。

3、log_queries_not_using_indexes

开启这个选项是个不错的主意,它真实地记录了返回所有行的查询。

小结

我们介绍了MySQL参数的五大类设置,平时我们一般都很少碰它们,在进行数据库性能调优和故障诊断时这些参数还是非常有用的。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

MySQL's Role: Databases in Web ApplicationsMySQL's Role: Databases in Web ApplicationsApr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

MySQL: Building Your First DatabaseMySQL: Building Your First DatabaseApr 17, 2025 am 12:22 AM

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL: A Beginner-Friendly Approach to Data StorageMySQL: A Beginner-Friendly Approach to Data StorageApr 17, 2025 am 12:21 AM

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)