mysql read buffer_mysql 参数:[read_buffer_size] [sort_buffer_size] [read_rnd_buffer_size] [tmp_table_s...

http://imysql.cn/2008_09_27_deep_into_mysql_sort_buffer

http://my.oschina.net/realfighter/blog/364420

https://www.percona.com/blog/2007/07/24/what-exactly-is-read_rnd_buffer_size/

ee3a89d4a3acc54c6ca2b93addcc2253.png

skip-external-locking:跳过外部锁定。要明白这个参数,必须先了解external-locking(外部锁定,作用是为MYISAM数据表在多进程【多个服务公用同一个数据库目录】访问下锁定),大多数情况下,我们的mysql服务都是单进程服务的,从mysql官网上看,skip-external-locking参数默认情况下是ON的,

mysql> show variables like '%skip%';+------------------------+-------+

| Variable_name | Value |

+------------------------+-------+

| skip_external_locking | ON |

在配置文件[mysqld]下开启这个参数OK。

key_buffer_size = 256M:为MYISAM数据表开启供线程共享的索引缓存。我们的项目中数据表基本上用的是INNODB引擎,所以这个参数暂时不进行调整,有需要的可以参考http://database.51cto.com/art/201010/229939.htm

max_allowed_packet = 16M:服务端最大允许接收的数据包大小。在没有调整该配置项的时候,服务端默认是4M。当然这个参数和mysql(默认16M)和mysqldump(默认为24M,我已经调整为16M)中的数据包大小有关系,一般情况下1M就可以,官方建议如果使用了blog或者更大的字符串时进行该参数的调整,一般情况下,数据库会被初始化为net_buffer_length(最小1024byte,最大是1M,默认是16KB)的大小。

table_open_cache = 512:所有线程打开表的数目(默认设置大小为1000)。如果opened_tables很大并且不经常使用flush tables,官方建议我们增加该参数的大小。这个值并不是越大越好,需要根据实际情况下open_tables和opened_tables的综合进行调整,详细可见http://www.cnblogs.com/suredandan/p/4010931.html

sort_buffer_size = 512K:需要排序 会话 的缓存大小,是针对每一个connection的,这个值也不会越大越好,默认大小是256kb,过大的配置会消耗更多的内存。我个人还没有测试,可以查看http://bbs.chinaunix.net/thread-1805254-1-1.html

read_buffer_size = 512K:为需要全表扫描的MYISAM数据表线程指定缓存,也是针对每个connection的,这个参数暂时我也不需要太关注。

Each thread that does a sequential scan for a MyISAM table allocates a buffer of this size (inbytes)for each table it scans. If you do many sequential scans, you might want to increase thisvalue, which

defaults to131072. The value of this variable should be a multiple of 4KB. If it is set to a value that isnot

a multiple of 4KB, its value will be rounded down to the nearest multiple of 4KB.

This optionis also used in the following context forall search engines:

• For caching the indexesin a temporary file (not a temporary table), when sorting rows forORDER BY.

• For bulk insert into partitions.

• For caching results of nested queries.

andin one other storage engine-specific way: to determine the memory block size forMEMORY tables.

The maximum permissible settingfor read_buffer_size is2GB.

For more information about memory use during different operations, see Section8.11.4.1, “How MySQL

Uses Memory”.

read_rnd_buffer_size = 1M:首先,该变量可以被任何存储引擎使用,当从一个已经排序的键值表中读取行时,会先从该缓冲区中获取而不再从磁盘上获取。默认为256K。

This variable is used for reads from MyISAM tables, and, for any storage engine, for Multi-Range Read

optimization.

When reading rowsfrom a MyISAM table in sorted order following a key-sorting operation, the rows are

read throughthis buffer to avoid disk seeks. See Section 8.2.1.15, “ORDER BY Optimization”. Setting

the variable to a large value can improve ORDER BY performance by a lot. However,this isa buffer

Server System Variables627allocatedfor each client, so you should not set the globalvariable to a large value. Instead, change the

session variable onlyfromwithin those clients that need to run large queries.

The maximum permissible settingfor read_rnd_buffer_size is 2GB.

另外可参见http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3642777&highlight=

thread_cache_size = 18:有多少线程供服务缓存使用。

How many threads the server should cache for reuse. When a client disconnects, the client's threads

are put in the cache if there are fewer than thread_cache_size threads there. Requests forthreads

are satisfied by reusing threads takenfrom the cache if possible, and only when the cache is empty isanew thread created. This variable can be increased to improve performance if you have a lot of newconnections. Normally,this does not provide a notable performance improvement ifyou have a good

thread implementation. However,ifyour server sees hundreds of connections per second you should

normallyset thread_cache_size high enough so that most newconnections use cached threads. By

examining the difference between the Connections and Threads_created status variables, you can

see how efficient the thread cacheis. For details, see Section 5.1.6, “Server Status Variables”.

Thedefault value is based on the following formula, capped to a limit of 100:8 + (max_connections / 100)

This variable has no effectfor the embedded server (libmysqld) and as of MySQL 5.7.2 isno longer

visible within the embedded server.

query_cache_size= 8M:分配给查询缓存的内存大小。要配合query_cache_type使用,默认是不开启的。只从该参数的表面介绍来看,似乎值设置的越大,带来的效果会更好,但是请注意,查询缓存的工作原理,一个select语句过来后,数据库将查询结果缓存到cache中,等同样的select查询过来后,如果这段时间内该查询结果没有发生变化时,数据库将cache中将缓存结果返回,那么假如查询的相关数据表增删改特别多的话,数据表变更的这段时间内,要将cache失效,然后再更新数据,对于增删改来说,花费的时间就很多了,所以要有所权衡,这个参数我会在将来进行相关测试数据整理。

By default, the query cache isdisabled. Thisis achieved using a default value of 1M, with a default forquery_cache_type of0. (To reduce overhead significantly if you set the size to 0, you should also start the server with

query_cache_type=0.

The permissible values are multiples of1024; other values are rounded down to the nearest multiple.

Note that query_cache_size bytes of memory are allocated evenif query_cache_type is set to 0.

See Section8.9.3.3, “Query Cache Configuration”, formore information.

The query cache needs a minimum size of about 40KB to allocate its structures. (The exact size

depends on system architecture.) If yousetthe value of query_cache_size too small, a warning will

occur,as described in Section 8.9.3.3, “Query Cache Configuration”.

query_cache_type = 1:1表示缓存所有查询,2表示缓存select sql_cache的查询,看如下内容

0 or OFF Do not cache results in or retrieve results fromthe query cache. Note thatthis does not deallocate the query cache buffer. To do that, you should setquery_cache_size to0.1 or ON Cache all cacheable query results except forthose that begin with SELECT

SQL_NO_CACHE.2 or DEMAND Cache results only for cacheable queries that begin with SELECT SQL_CACHE.

set global max_connections = 500:注意这个是通过命令行设置最大连接数,不是配置在配置文件的,因为我在配置文件里面尝试修改,重启mysql服务后并没有起效,通过该参数设置以后,重启服务后,依然没有起效,如果有朋友知道这个原因的话,请告知。如果说你的项目使用的是spring的连接池的时候,我认为spring个connection就对应的这个连接。根据你项目的需求而定。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ee3a89d4a3acc54c6ca2b93addcc2253.png

然后对以上参数进行一下简单的介绍:

1、max_connections这个参数,这个参数指MySql的最大连接数,如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,服务器根据自己的实际情况进行增加,如果连接数越多,因MySql会为每个连接提供连接缓冲区,就会开销越多的内存,所以要适当调整该值,不能盲目提高设值。可以过mysql -e "SHOW VARIABLES LIKE 'max_connections';"查看当前状态的连接数量来设定该值大小。当你常看到Too many connections错误,可以增加此值了,默认是100。

2、back_log这个参数主要是基于max_connections进行的一个额外连接,也就是说当mysql连接大于max_connections设置的值的话,而又在(max_connections+back_log)之间,则mysql会把新连接放到堆栈中,等待之前连接的process释放,如果当前最大请求超过了(max_connections+back_log),就不会授权连接,当然该值也受约于系统的TCP/IP连接的侦听队列(系统的tcp_max_syn_backlog值的大小),可以通过cat /proc/sys/net/ipv4/tcp_max_syn_backlog查看,当然可以修改该值

3sysctl -w net.ipv4.tcp_max_syn_backlog=N或在/etc/sysctl.conf中添加tcp_max_syn_backlog=N即可。

由于mysql被称为内存式数据库,当然很内存是密不可分了,而他和内存的关系主要是通过缓冲区大小的几个参数吧。

缓冲包括:全局缓冲和局部缓冲。

全局缓冲参数大致有如下:

key_buffer_size, innodb_buffer_pool_size, innodb_additional_mem_pool_size,innodb_log_buffer_size, query_cache_size

而局部缓冲(我自己习惯这么叫,虽然不专业,呵呵)一般mysql还会为每个连接分配连接缓冲。

全局缓冲在上篇博客中有介绍,这地方就省略了。

局部缓存:

每个连接到MySQL服务器的线程都需要有自己的缓冲。大概需要立刻分配256K,甚至在线程空闲时,它们使用默认的线程堆栈,网络缓存等。事务开始之后,则需要增加更多的空间。运行较小的查询可能仅给指定的线程增加少量的内存消耗,然而如果对数据表做复杂的操作例如扫描、排序或者需要临时表,则需分配大约read_buffer_size,sort_buffer_size,read_rnd_buffer_size,tmp_table_size大小的内存空间。不过它们只是在需要的时候才分配,并且在那些操作做完之后就释放了。有的是立刻分配成单独的组块。tmp_table_size可能高达MySQL所能分配给这个操作的最大内存空间了。注意,这里需要考虑的不只有一点——可能会分配多个同一种类型的缓存,例如用来处理子查询。一些特殊的查询的内存使用量可能更大——如果在MyISAM表上做成批的插入时需要分配bulk_insert_buffer_size 大小的内存;执行 ALTER TABLE, OPTIMIZE TABLE, REPAIR TABLE命令时需要分配myisam_sort_buffer_size大小的内存。

read_buffer_size:是MySql读入缓冲区大小。对表进行顺序扫描的请求将分配一个读入缓冲区,MySql会为它分配一段内存缓冲区。read_buffer_size变量控制这一缓冲区的大小。如果对表的顺序扫描请求非常频繁,并且你认为频繁扫描进行得太慢,可以通过增加该变量值以及内存缓冲区大小提高其性能。

sort_buffer_size:是MySql执行排序使用的缓冲大小。如果想要增加ORDER BY的速度,首先看是否可以让MySQL使用索引而不是额外的排序阶段。如果不能,可以尝试增加sort_buffer_size变量的大小。

read_rnd_buffer_size:是MySql的随机读缓冲区大小。当按任意顺序读取行时(例如,按照排序顺序),将分配一个随机读缓存区。进行排序查询时,MySql会首先扫描一遍该缓冲,以避免磁盘搜索,提高查询速度,如果需要排序大量数据,可适当调高该值。但MySql会为每个客户连接发放该缓冲空间,所以应尽量适当设置该值,以避免内存开销过大。

tmp_table_size:是MySql的heap(堆积)表缓冲大小。所有联合在一个DML指令内完成,并且大多数联合甚至可以不用临时表即可以完成。大多数临时表是基于内存的(HEAP)表。具有大的记录长度的临时表(所有列的长度的和)或包含BLOB列的表存储在硬盘上。如果某个内部heap(堆积)表大小超过tmp_table_size,MySQL可以根据需要自动将内存中的heap表改为基于硬盘的MyISAM表。还可以通过设置tmp_table_size选项来增加临时表的大小。也就是说,如果调高该值,MySql同时将增加heap表的大小,可达到提高联接查询速度的效果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值