首页 技术 正文
技术 2022年11月20日
0 收藏 664 点赞 3,069 浏览 2150 个字

0.

1.参考

提升网站访问速度的 SQL 查询优化技巧

缓存一切数据,读取内存而不是硬盘IO

如果你的服务器默认情况下没有使用MySQL查询缓存,那么你应该开启缓存。开启缓存意味着MySQL 会把所有的语句和语句执行的结果保存下来,如果随后有一条与缓存中完全相同的语句需要执行,那么MySQL 就会返回缓存的结果。缓存不会过时,因为MySQL 会在表数据更新后刷新缓存。

MySQL缓存–服务器缓存query cache

https://dba.stackexchange.com/questions/109030/cant-enable-mysql-5-6-query-cache

Look for the [mysqld] group header in my.cnf and put those lines under it

[mysqld]
query_cache_type = 1
query_cache_size = 4096M
query_cache_limit = 2M
query_cache_strip_comments =1

Every change to a table requires scanning the 4GB to purge entries for that table.  一旦数据更新,会清空内存的所有缓存并更新,所以应该合理设置缓存总大小 query_cache_size

2.MySQL 查询缓存

当前设置

mysql> show variables like “query_cache%”;
+——————————+———+
| Variable_name | Value |
+——————————+———+
| query_cache_limit | 1048576 |    #单条最大 1MB,1000条数据返回45KB,这里设置为 51200 比较合适
| query_cache_min_res_unit | 4096 |     #100条数据返回4.8KB,这里设置为 5120 比较合适,不够会再申请一块
| query_cache_size | 1048576 |    #总共分配1MB,修改为 52428800,即50MB,能够缓存 1w 个 100条查询结果。
| query_cache_type | OFF |      #修改为 1 或 ON 缓存除了以 SELECT SQL_NO_CACHE 开头的所有查询结果。另一个选项是 2 或 DEMAND 只缓存以 SELECT SQL_CACHE 开头的查询结果。

| query_cache_wlock_invalidate | OFF |  #表锁定时认为缓存不可用,修改为 ON
+——————————+———+
5 rows in set (0.01 sec)

mysql> select @@query_cache_type;
+——————–+
| @@query_cache_type |
+——————–+
| OFF |
+——————–+
1 row in set, 1 warning (0.00 sec)

mysql> set @@query_cache_type=ON;
ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it
mysql> exit;

修改缓存设置  vi /etc/my.cnf 重启 service mysqld restart

query_cache_limit = 50K
query_cache_min_res_unit = 5K
query_cache_size = 50M
query_cache_type = 1
query_cache_wlock_invalidate = ON

查询缓存状态

mysql> show status like “Qcache%”;
+————————-+———-+
| Variable_name | Value |
+————————-+———-+
| Qcache_free_blocks | 1 |   #太多碎片,最小分配内存单位设置不合理?
| Qcache_free_memory | 52365352 |
| Qcache_hits | 0 |        #命中缓存的查询次数
| Qcache_inserts | 12 |    #插入次数
| Qcache_lowmem_prunes | 0 |   #总缓存空间不足?
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 12 |    #现有缓存个数
| Qcache_total_blocks | 26 |
+————————-+———-+
8 rows in set (0.00 sec)

设置 query_cache_min_res_unit 为 5K 或 10K 下面结果都是1:2,所以还是设置为 5K???

Qcache_queries_in_cache | 1152 |
| Qcache_total_blocks | 2307 |

3.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295