从SQL数据库缓存的结果,或每次查询? [英] Cache results from sql database, or query each time?

查看:371
本文介绍了从SQL数据库缓存的结果,或每次查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成基于​​SQL的查询页面。

I'm generating pages based on an sql query.

这是查询:

CREATEPROCEDURE sp_searchUsersByFirstLetter 
    @searchQuery nvarchar(1)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT UserName
    FROM Users Join aspnet_Users asp on Users.UserId = asp.UserId
    WHERE (LoweredUserName like @searchQuery + '%')

我可以调用这个过程字母表中每个字母,并得到所有以该字母开头的用户。然后,我把这些用户到名单上我的网页之一。

I can call this procedure for each letter in the alphabet, and get all the users that start with that letter. Then, I put these users into a list on one of my pages.

我的问题是这样的:这将是更好的用户列表缓存到我的网络服务器,而不是每次查询数据库?像这样的:

My question is this: would it be better to cache the list of users to my webserver, rather than query the database each time? Like this:

HttpRuntime.Cache.Insert("users", listOfUsersReturnedFromQuery, null, DateTime.Now.AddHours(1), System.Web.Caching.Cache.NoSlidingExpiration);

其确定对我来说,如果用户列表是一个小时过时。这将是更有效,每次查询的数据库?

Its ok for me if the list of users is an hour out of date. Will this be more efficient that querying the database each time?

推荐答案

使用高速缓存最适合您的查询符合下列限制的情况下保留:

Using a cache is best reserved for situations where your query meets the following constraints:


  • 数据不是时间关键,即确保高速缓存命中将不会导致您的code到错过了最近的数据更新打破您的应用程序。

  • 数据不测序,即A,B,C,D,E被缓存,F由其他用户插入,你的用户将G和命中缓存,导致ABCDEG而不是ABCDEFG。

  • 的数据并没有太大变化。

  • 的数据被查询和频繁重复使用。

大小是不是真的,除非它要真能让RAM的一个因素。

Size isn't really a factor unless it's going to really tax your RAM.

我已经发现,向缓存的最佳表中的一个是设​​置表,其中数据实际上是静态的,被查询的几乎每一个页面请求,并且变化不必须是直接的

I have found that one of the best tables to cache is a settings table, where the data is practically static, gets queried on nearly every page request, and changes don't have to be immediate.

为你做将是检验其最进行查询,然后选择那些最高征税数据库服务器的最好的事情。出者,缓存任何你能负担得起。你也应该看看调整最大缓存对象年龄。如果你正在执行一个查询100次,第二,你可以通过简单地将其高速缓存1秒,这否定了大多数实际情况更新延迟的问题是削减率下降99%的因素。

The best thing for you to do would be to test which queries are performed most, then select those that are taxing the database server highest. Out of those, cache anything you can afford to. You should also take a look at tweaking maximum cached object ages. If you're performing a query 100 times a second, you can cut that rate down by a factor of 99% by simply caching it for 1 second, which negates the update delay problem for most practical situations.

这篇关于从SQL数据库缓存的结果,或每次查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆