缓存资金与db级指数?

时间:2022-02-21 04:09:25

Does Cache Money gem eliminate the need for DB-level indices (the ones typically used to improve query retrieval)?

缓存货币gem是否消除了对db级索引(通常用于改进查询检索)的需求?

2 个解决方案

#1


1  

Caching = answers to you query are retrieved from cache (RAM), if they're there. The database is only accessed on cache miss.

缓存=查询的答案从缓存(RAM)中检索,如果它们存在的话。数据库只在缓存缺失时访问。

Indexing = helping the database answer some queries (much) faster (a simplistic definition, but enough for my purpose here).

索引=帮助数据库更快地回答一些查询(这是一个简单的定义,但是对于我这里的目的来说已经足够了)。

So, the cache speeds up queries by not hitting the database at all, whereas the index speeds up queries that go through the cache and hit the database.

因此,缓存通过完全不撞数据库来加速查询,而索引则加速通过缓存并撞向数据库的查询。

The benefit you could get from caching depends on the specifics of your application. If most queries ask for the same data, or you have much few updates than reads, it should prove a good idea to use a caching layer. However, even in the best scenarios I've seen, 15-20% of queries still reach the database. That means that you cannot rely on caching alone.

缓存的好处取决于应用程序的细节。如果大多数查询请求相同的数据,或者更新比读取少得多,那么使用缓存层应该是一个好主意。然而,即使在我见过的最好的情况下,仍然有15-20%的查询到达数据库。这意味着您不能仅依赖缓存。

But most important - database indices are simple to define and usually require zero maintenance unless you become huge. Caching otoh is far more complicated - for the cache-money gem you'll need to install, configure and maintain memcached. That isn't rocket science, but still adds another layer of complexity on top of your database. In other words, this is usually premature optimization. I recommend that you start with DB indices and only add a caching layer when you find that you really need it.

但是最重要的——数据库索引是简单的定义,通常需要零维护,除非你变得庞大。缓存otoh要复杂得多——对于cache-money gem,您需要安装、配置和维护memcached。这不是火箭科学,但仍然增加了另一层复杂性在您的数据库之上。换句话说,这通常是过早的优化。我建议您从DB索引开始,只有当您发现确实需要时才添加缓存层。

#2


0  

Nothing can replace the need for an index.

没有什么能取代对索引的需求。

A caching layer may change the workload and lower the load on the database, thus mitigating the impact of missing indexes. With a caching layer you may need other indexes for optimal performance, but you still need them if you want to have the best possible performance.

缓存层可能会更改工作负载并降低数据库的负载,从而减轻缺失索引的影响。使用缓存层,您可能需要其他索引以获得最佳性能,但是如果您希望获得最佳性能,仍然需要它们。

#1


1  

Caching = answers to you query are retrieved from cache (RAM), if they're there. The database is only accessed on cache miss.

缓存=查询的答案从缓存(RAM)中检索,如果它们存在的话。数据库只在缓存缺失时访问。

Indexing = helping the database answer some queries (much) faster (a simplistic definition, but enough for my purpose here).

索引=帮助数据库更快地回答一些查询(这是一个简单的定义,但是对于我这里的目的来说已经足够了)。

So, the cache speeds up queries by not hitting the database at all, whereas the index speeds up queries that go through the cache and hit the database.

因此,缓存通过完全不撞数据库来加速查询,而索引则加速通过缓存并撞向数据库的查询。

The benefit you could get from caching depends on the specifics of your application. If most queries ask for the same data, or you have much few updates than reads, it should prove a good idea to use a caching layer. However, even in the best scenarios I've seen, 15-20% of queries still reach the database. That means that you cannot rely on caching alone.

缓存的好处取决于应用程序的细节。如果大多数查询请求相同的数据,或者更新比读取少得多,那么使用缓存层应该是一个好主意。然而,即使在我见过的最好的情况下,仍然有15-20%的查询到达数据库。这意味着您不能仅依赖缓存。

But most important - database indices are simple to define and usually require zero maintenance unless you become huge. Caching otoh is far more complicated - for the cache-money gem you'll need to install, configure and maintain memcached. That isn't rocket science, but still adds another layer of complexity on top of your database. In other words, this is usually premature optimization. I recommend that you start with DB indices and only add a caching layer when you find that you really need it.

但是最重要的——数据库索引是简单的定义,通常需要零维护,除非你变得庞大。缓存otoh要复杂得多——对于cache-money gem,您需要安装、配置和维护memcached。这不是火箭科学,但仍然增加了另一层复杂性在您的数据库之上。换句话说,这通常是过早的优化。我建议您从DB索引开始,只有当您发现确实需要时才添加缓存层。

#2


0  

Nothing can replace the need for an index.

没有什么能取代对索引的需求。

A caching layer may change the workload and lower the load on the database, thus mitigating the impact of missing indexes. With a caching layer you may need other indexes for optimal performance, but you still need them if you want to have the best possible performance.

缓存层可能会更改工作负载并降低数据库的负载,从而减轻缺失索引的影响。使用缓存层,您可能需要其他索引以获得最佳性能,但是如果您希望获得最佳性能,仍然需要它们。