缓存查询结果与每次查询

时间:2015-05-26 15:27:45

标签: sql-server performance caching

这是对a previously asked question

的扩展

一些背景信息:

Querying Over 5,000 items from SQL database that eventually get sorted into a list depending on who the user is. 
One problem is that users are allowed to have customized lists which means that sorting these 5,000+ items in a uniform way is probably impossible.

Also, important that the amount of users (and users wanting to have custom item lists) is constantly growing, so there will be an unprecedented amount of ways to sort. Basically, we won't be paying attention to sorting this growing list.

Two methods we've come up with so far:
-Option 1: Cache item list PER USER (and sort the items needed after)
OR
-Option 2: Cache the list once for the whole site (every user accesses this one cached list)

The issue with Option 1 is that there will be duplicated lists in cache.
Although we do not have an amount of users that will strain our server's memory, we obviously want our system to have scalability.

The issue with Option 2 is that there will be duplicated items in the single, cached list.
With the amount of users we have, this can quickly turn into a disorganized list full of duplicated items (i.e. 5,000 items can quickly turn into 10,000 based on a few user's custom item preferences)

是否存在"中间地带"处理这种情况?结合了查询和缓存的一些好处。

即使回复是指向可能有用的来源的链接,我也会很感激,因为我在路上岔路口。

希望在我的头脑风暴中开辟新的可能途径。

此外,如果没有任何意义,请寻找有关如何改进此问题的建议。

提前致谢。

**编辑:**通过排序,我的意思是过滤。对于对此有些困惑,我深表歉意。

1 个答案:

答案 0 :(得分:0)

我的建议是将项目缓存在单个索引结构(数组,哈希表等)中。每个用户都维护一个与他们希望看到项目的首选顺序相对应的密钥列表(一种简单的方法是将用逗号分隔的字符串存储在User表的单个列中) - 当您加载用户时,您遍历此列表并查找缓存的索引结构上的项目,因此创建排序列表应该是O(n)操作(列表遍历而不是排序)。

在“不知道/不关心”的情况下,例如添加到用户尚未分配排序首选项的缓存中的项目,您可以获取用户首选项列表中的键和缓存中的键的设置差异,并将这些添加到用户首选项的末尾列表;例如,将所有键从缓存的索引结构复制到用户空间中的临时结构中,当您遍历用户的首选项列表时,从其临时结构中删除键,并且当您遍历首选项列表时,您遍历任何剩余密钥的临时结构,并将其相应的项添加到用户列表的末尾,并将剩余的密钥添加到用户首选项列表的末尾。