MySQL 可调节设置可以应用于整个 mysqld进程,也可以应用于单个客户机会话。
服务器端的设置
每个表都可以表示为磁盘上的一个文件,必须先打开,后读取。为了加快从文件中读取数据的过程,mysqld对这些打开文件进行了缓存,其最大数目由 /etc/mysqld.conf 中的table_cache 指定。清单 4给出了显示与打开表有关的活动的方式。
清单 4. 显示打开表的活动
?
1 2 3 4 5 6 7 8 | mysql> SHOW STATUS
LIKE
'open%tables'
;
+
---------------+-------+ | Variable_name | Value | +
---------------+-------+ | Open_tables| 5000 | | Opened_tables | 195| +
---------------+-------+ 2
rows
in
set
(0.00 sec) |
与表的缓存类似,对于线程来说也有一个缓存。 mysqld在接收连接时会根据需要生成线程。在一个连接变化很快的繁忙服务器上,对线程进行缓存便于以后使用可以加快最初的连接。
清单 5 显示如何确定是否缓存了足够的线程。
清单 5. 显示线程使用统计信息
?
1 2 3 4 5 6 7 8 9 10 11 | mysql> SHOW STATUS
LIKE
'threads%'
;
+
-------------------+--------+ | Variable_name| Value | +
-------------------+--------+ | Threads_cached| 27| | Threads_connected | 15| | Threads_created| 838610 | | Threads_running| 3| +
-------------------+--------+ 4
rows
in
set
(0.00 sec) |
关键字缓冲区保存了 MyISAM 表的索引块。理想情况下,对于这些块的请求应该来自于内存,而不是来自于磁盘。清单 6显示了如何确定有多少块是从磁盘中读取的,以及有多少块是从内存中读取的。
清单 6. 确定关键字效率
?
1 2 3 4 5 6 7 8 | mysql> show status
like
'%key_read%'
;
+
-------------------+-----------+ | Variable_name| Value| +
-------------------+-----------+ | Key_read_requests | 163554268 | | Key_reads| 98247| +
-------------------+-----------+ 2
rows
in
set
(0.00 sec) |
临时表可以在更高级的查询中使用,其中数据在进一步进行处理(例如 GROUPBY字句)之前,都必须先保存到临时表中;理想情况下,在内存中创建临时表。但是如果临时表变得太大,就需要写入磁盘中。清单 7给出了与临时表创建有关的统计信息。
清单 7. 确定临时表的使用
?
1 2 3 4 5 6 7 8 9 10 | mysql> SHOW STATUS
LIKE
'created_tmp%'
;
+
-------------------------+-------+ | Variable_name| Value | +
-------------------------+-------+ | Created_tmp_disk_tables | 30660 | | Created_tmp_files| 2| | Created_tmp_tables| 32912 | +
-------------------------+-------+ 3
rows
in
set
(0.00 sec) |
每个会话 的设置
下面这些设置针对于每个会话。在设置这些数字时要十分谨慎,因为它们在乘以可能存在的连接数时候,这些选项表示大量的内存!您可以通过代码修改会话中的这些数字,或者在 my.cnf 中为所有会话修改这些设置。
当 MySQL必须要进行排序时,就会在从磁盘上读取数据时分配一个排序缓冲区来存放这些数据行。如果要排序的数据太大,那么数据就必须保存到磁盘上的临时文件中,并再次进行排序。如果 sort_merge_passes状态变量很大,这就指示了磁盘的活动情况。清单 8 给出了一些与排序相关的状态计数器信息。
清单 8. 显示排序统计信息
?
1 2 3 4 5 6 7 8 9 10 | mysql> SHOW STATUS
LIKE
"sort%"
;
+
-------------------+---------+ | Variable_name| Value| +
-------------------+---------+ | Sort_merge_passes | 1| | Sort_range| 79192| | Sort_rows| 2066532 | | Sort_scan| 44006| +
-------------------+---------+ 4
rows
in
set
(0.00 sec) |
MySQL也会分配一些内存来读取表。理想情况下,索引提供了足够多的信息,可以只读入所需要的行,但是有时候查询(设计不佳或数据本性使然)需要读取表中大量数据。要理解这种行为,需要知道运行了多少个 SELECT语句,以及需要读取表中的下一行数据的次数(而不是通过索引直接访问)。实现这种功能的命令如清单 9 所示。
清单 9. 确定表扫描比率
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | mysql> SHOW STATUS
LIKE
"com_select"
;
+
---------------+--------+ | Variable_name | Value | +
---------------+--------+ | Com_select| 318243 | +
---------------+--------+ 1 row
in
set
(0.00 sec) mysql> SHOW STATUS
LIKE
"handler_read_rnd_next"
;
+
-----------------------+-----------+ | Variable_name| Value| +
-----------------------+-----------+ | Handler_read_rnd_next | 165959471 | +
-----------------------+-----------+ 1 row
in
set
(0.00 sec) |
查看数据库缓存配置情况
?
1 2 3 4 5 6 7 8 9 10 11 | mysql> SHOW VARIABLES
LIKE
‘%query_cache%';
+——————————+———+ | Variable_name | Value | +——————————+———+ | have_query_cache | YES | –查询缓存是否可用 | query_cache_limit | 1048576 | –可缓存具体查询结果的最大值 | query_cache_min_res_unit | 4096 | | query_cache_size | 599040 | –查询缓存的大小 | query_cache_type |
ON
| –阻止或是支持查询缓存 | query_cache_wlock_invalidate |
OFF
| +——————————+———+ |
在MYSQL的配置文件my.ini或my.cnf中找到如下内容:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # Query cache is used to cache SELECT results and later returnthem # without actual executing the same query once again. Having thequery # cache enabled may result in significant speed improvements, ifyour # have a lot of identical queries and rarely changing tables.See the # "Qcache_lowmem_prunes" status variable to check if the currentvalue # is high enough for your load. # Note: In case your tables change very often or if your queriesare # textually different every time, the query cache may result ina # slowdown instead of a performance improvement. query_cache_size=0 |
具体配置方法:
1. 将
query_cache_size
设置为具体的大小,具体大小是多少取决于查询的实际情况,但最好设置为1024的倍数,参考值32M。2. 增加一行:
query_cache_type=1
query_cache_type参数用于控制缓存的类型,注意这个值不能随便设置,必须设置为数字,可选项目以及说明如下:
如果设置为0,那么可以说,你的缓存根本就没有用,相当于禁用了。但是这种情况下query_cache_size设置的大小系统是否要为其分配呢,这个问题有待于测试?
如果设置为1,将会缓存所有的结果,除非你的select语句使用SQL_NO_CACHE禁用了查询缓存。
如果设置为2,则只缓存在select语句中通过SQL_CACHE指定需要缓存的查询。
OK,配置完后的部分文件如下:
?
1 2 3 | query_cache_size=128M query_cache_type=1 |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | mysql> show variables
like
'%query_cache%'
;
+——————————+———–+ | Variable_name|Value| +——————————+———–+ | have_query_cache|YES| | query_cache_limit|1048576| | query_cache_min_res_unit|4096| | query_cache_size| 134217728| | query_cache_type|
ON
| | query_cache_wlock_invalidate |
OFF
| +——————————+———–+ 6
rows
in
set
(0.00 sec) |
这里query_cache_size的值是134217728,我们设置的是128M,实际是一样的,只是单位不同,可以自己换算下:134217728 = 128*1024*1024。
query_cache_type设置为1,显示为ON,这个前面已经说过了。
【mysql缓冲和缓存设置详解】总之,看到上边的显示表示设置正确,但是在实际的查询中是否能够缓存查询,还需要手动测试下,我们可以通过show statuslike '%Qcache%'; 语句来测试,现在我们开启了查询缓存功能,在执行查询前,我们先看看相关参数的值:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 | mysql> show status
like
'%Qcache%'
;
+————————-+———–+ | Variable_name|Value| +————————-+———–+ | Qcache_free_blocks|1| | Qcache_free_memory| 134208800| | Qcache_hits|0| |