简体   繁体   English

Redis是否应该缓存数据库条目?

[英]Isn't Redis supposed to cache db entries?

My server is Debian 9.0. 我的服务器是Debian 9.0。 It has SSD HDD with 32 of RAM. 它具有带有32 RAM的SSD HDD。 and i7 processor with 4 (8) cores. 和具有四(8)核的i7处理器。 I have 4 websites. 我有4个网站。 2 of them are very small and two large. 其中2个很小,两个很大。 The large databases (mysql) are about 120 + 60 MB in size. 大型数据库(mysql)的大小约为120 + 60 MB。 My webserver is nginx, also using varnish. 我的网络服务器是nginx,也使用清漆。

Well, i want to learn about linux so i started reading articles about tuning nginx, mysql, php-fpm etc. I was tuning mysql, installed mysqltuner. 好吧,我想学习Linux,所以我开始阅读有关调优nginx,mysql,php-fpm等的文章。我正在调优mysql,安装了mysqltuner。 It advised me to set "query_cache_size" and "query_cache_type" to 0 which means disabling the cache.. 它建议我将“ query_cache_size”和“ query_cache_type”设置为0,这意味着禁用缓存。

I did. 是的 However my sites become really slower. 但是我的网站真的变慢了。 My 680 Kb web page was loading in 700 ms but after disabling the query_cache, the speed decreased to almost 1,5 seconds. 我的680 Kb网页在700毫秒内加载,但是在禁用了query_cache之后,速度降低到将近1.5秒。 It was interesting. 很有意思。 I set the query_cache_size back to 128M. 我将query_cache_size设置回128M。 So the speed was restored. 因此速度得以恢复。 I kept reading more about setting query_cache and read the article of Zaitsev (in percona.com) where he states that using a large cache is problematic and he says at the end of his article: " In many cases you may want to disable query cache at all and use external cache such as memcached which does not have this problem." 我继续阅读有关设置query_cache的更多信息,并阅读Zaitsev的文章(在percona.com中),他指出使用大容量缓存存在问题,他在文章结尾说:“在许多情况下,您可能希望禁用查询缓存并使用没有此问题的外部缓存(例如memcached)。”

https://www.percona.com/blog/2007/03/23/beware-large-query_cache-sizes/ https://www.percona.com/blog/2007/03/23/beware-large-query_cache-sizes/

So, i installed REDIS (which they say is better than memcached and does things better). 因此,我安装了REDIS(他们说这比memcached更好,并且做得更好)。 I installed it, i also installed the wordpress plugin for redis. 我安装了它,还安装了用于redis的wordpress插件。 i monitor redis and it works very fine. 我监视redis,它工作得很好。 However two things happen: 但是,发生两件事:

1) Redis didnt improve my site speed. 1)Redis并没有提高我的网站速度。 Its the same. 一样的。 No difference. 没有不同。 Perhaps its because my web pages are very small (700 kb at most) and my databases are small also. 也许是因为我的网页很小(最多700 kb),而我的数据库也很小。

2) When i disable mysql query cache, my WEB SITE gets very slow again. 2)当我禁用mysql查询缓存时,我的网站再次变得非常慢。

So isnt redis supposed to cache all DB entries and use them much faster than mysql? 那么redis是否应该缓存所有数据库条目并比mysql更快地使用它们? How come my webpage speed decreases when i disable query cache of mysql? 当我禁用mysql的查询缓存时,网页速度如何降低?

I agree that 700ms is too long. 我同意700毫秒太长。 Properly designed, you should be able to execute a hundred MySQL queries in that time; 经过适当设计,您应该能够在那时执行一百个MySQL查询; sounds like you aren't? 听起来像你不是吗?

I agree that a large Query cache is usually unwise: 我同意大型查询缓存通常是不明智的:

  • It takes away RAM from other caching. 它从其他缓存中删除了RAM。
  • Every modification to a table cause purging of all entries in the QC for that table. 对表的每次修改都会清除该表的QC中的所有条目。 If a table is changing often, you suffer overhead for that purging, while getting little or no benefit from caching. 如果表经常更改,那么清除将遭受开销,而从缓存中获得的收益很少或没有。

You have a modest sized database -- under 1GB for everything? 您的数据库大小适中-所有内容都不足1GB? With 32 GB of RAM, you should be able to cache all the data and indexes all the time. 有了32 GB的RAM,您应该能够一直缓存所有数据并建立索引。 So, slowdowns will be in poor indexing and/or poor query formulation. 因此,减速将归因于索引编制和/或查询编制不良。

I hope you are using InnoDB, not MyISAM. 我希望您使用的是InnoDB,而不是MyISAM。 With InnoDB, you need to set innodb_buffer_pool_size to no more than 70% of available RAM. 使用InnoDB,您需要将innodb_buffer_pool_size设置为不超过可用 RAM的70%。 You have several products running in the 32G?? 您在32G中运行着几种产品? So, I won't say 70% of 32. (Note: Swapping is terrible for performance.) Since you have so little data, perhaps 2G would be a good setting? 因此,我不会说32%的70%。(注意:交换性能很糟糕。)由于数据量太少,也许2G是一个很好的设置?

Let's see the queries in your "too slow" web page. 让我们在“太慢”的网页中查看查询。 Also, let's see SHOW CREATE TABLE for the tables. 另外,让我们看一下表的SHOW CREATE TABLE Do you know about "composite" indexes? 您知道“复合”索引吗? Do you know not to hide indexed columns in function calls? 您是否知道不隐藏函数调用中的索引列?

Perhaps my cookbook can help you from here. 也许我的食谱可以从这里为您提供帮助。 If you are using WP, it includes a section on how to speed up wp_postmeta . 如果使用的是WP,则其中包括有关如何加快wp_postmeta速度的wp_postmeta If you need more help, start another Question with the slowest query, its EXPLAIN , and its schema. 如果需要更多帮助,请以最慢的查询,其EXPLAIN和其模式来启动另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM