加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

ASP.Net – C# – SQL Server – 只刷新缓存的DataTable,只记录

发布时间:2020-12-16 09:57:10 所属栏目:asp.Net 来源:网络整理
导读:我在我的Web应用程序中缓存了一个大型DataTable,这是一个返回大型数据集的复杂查询的结果.在缓存此数据表的同时,运行以“刷新”此缓存的查询仍然需要很长时间,主要是由于返回的数据量很大. 为了加快速度,我正在考虑对我的表实现时间戳类型方法,以限制我的查
我在我的Web应用程序中缓存了一个大型DataTable,这是一个返回大型数据集的复杂查询的结果.在缓存此数据表的同时,运行以“刷新”此缓存的查询仍然需要很长时间,主要是由于返回的数据量很大.

为了加快速度,我正在考虑对我的表实现时间戳类型方法,以限制我的查询只返回已更改的行.

然后我打算将这个较小的数据集与我的缓存数据表合并.

有没有人做过类似的事情,或者有什么东西可以解决这个问题吗?

如果我直接潜入,我觉得这可能会重新发明车轮状况.

解决方法

就我个人而言,我之前使用过时间戳方法并且确实运行良好 – 它通过仅检索自上次读取后已更改的数据,确实提高了缓存效率.

或者,我建议使用SqlCacheDependency类,它可以为您保持最新的缓存.我不能评论任何现实世界的优点,或性能比较与时间戳方法,因为我自己没有使用它.

还有一篇关于SqlCacheDependency here的有用文章

更新:
是的,我认为它不会真正刷新数据.听起来你必须自己做.
从第二个链接:

When the data changes—and only
then—the cache items based on that
data are invalidated and removed from
the cache. The next time you request
that item from the cache,if it is not
in the cache,you can re-add the
updated version to the cache and be
assured that you have the latest data

第二个链接中还有SQL 2005特定的实现说明:

SQL Server 2005 monitors changes to
the result set of a particular SQL
command. If a change occurs in the
database that would modify the results
set of that command,the dependency
causes the cached item to be
invalidated. This allows SQL Server
2005 to provide row-level
notification.

我个人认为我会采用时间戳方法(这是我以前做过的),因为我无法从中看到SqlCacheDependency会带来任何性能上的好处 – 我认为它会降低性能(只是更容易)实行).有一天,我会试着尝试使用SqlCacheDependency进行正确的性能分析:)

更新2:
关于将新数据合并到现有数据表中,我认为数据表的Merge方法是您想要的.

The Merge method is used to merge two DataTable objects that have largely similar schemas. A merge is typically used on a client application to incorporate the latest changes from a data source into an existing DataTable.


When merging a new source DataTable into the target,any source rows with a DataRowState value of Unchanged,Modified,or Deleted,is matched to target rows with the same primary key values. Source rows with a DataRowState value of Added are matched to new target rows with the same primary key values as the new source rows.

您只需确保在数据表上定义作为主键的列.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读