简体   繁体   English

SQL Server缓存问题

[英]Sql server Caching issue

I am using Sql Server 2008 R2, vs 2008, C# on .Net 3.5 and SqlHelper. 我使用的是Sql Server 2008 R2,vs。2008,C#on .Net 3.5和SqlHelper。

A method, say, updateData(int id) loads some data related to "id", does lots of calculations and processing and recommits the data. 一个方法,比如updateData(int id)加载一些与“id”相关的数据,进行大量的计算和处理并重新提交数据。 There is a separate programme doThings(int id) which does a set of tasks, wherein each task manipulates data related to "id" and then commits that data within a transaction. 有一个单独的程序doThings(int id)执行一组任务,其中每个任务操纵与“id”相关的数据,然后在事务中提交该数据。 Some of these tasks are calling updateData(int id) after they commit, however, updateData(int id) is not updating the data as per the latest commit. 其中一些任务在提交后调用updateData(int id),但是,updateData(int id)不按照最新提交更新数据。 After a while if I rerun updateData(int id), then the update is completed as it should. 如果我重新运行updateData(int id)一段时间,那么更新就完成了。

To make it more clear, here's what the program is doing- 为了更清楚,这是该计划正在做的事情 -

doTask(int id) //updates some table related to id and calls updateData()
doTask(int id) //updates some table related to id and calls updateData()
doTask(int id) //updates some table related to id and calls updateData()
doTask(int id) //updates some table related to id and calls updateData()
...

Output - Data is not updated 输出 - 数据未更新

After a while - 过了一会儿 -

updateData() //recalculates and updates the data

Output - Data is updated 输出 - 数据已更新

I know SQL Server caches query result sets. 我知道SQL Server缓存查询结果集。 However, if I have updated a part of the table which is in cache then shouldn't it recreate the result set. 但是,如果我更新了缓存中的表的一部分,则不应重新创建结果集。 I have tried this in different environments (testing and production) without much help. 我在不同的环境(测试和生产)中尝试过这个,没有太多帮助。

Each of doTask(int id) is using Transactions to update the database and that transaction is completed sans which last call of updateData will not update the data as it did eventually. 每个doTask(int id)都使用Transactions来更新数据库,并且该事务已完成,而最后一次调用updateData将不会像最终那样更新数据。 Once its transaction is completed, updateData() is called which also uses transactions . 一旦其事务完成,就调用updateData(),它也使用事务 Is there anything related to caching that can help here? 有什么与缓存相关的东西可以帮到这里吗? Because the amount of code that i will have to post here is tremendous and I am not even allowed to do that. 因为我必须在这里发布的代码量是巨大的,我甚至不允许这样做。

I tried 2 things and it worked in both the cases- 我尝试了两件事,它在两种情况下都有效 -

  1. added a thread.sleep of 500ms 添加了一个500毫秒的thread.sleep
  2. debugged and held off the call to updateData() in doTask() for just a second 调试并在doTask()中暂停对updateData()的调用仅一秒钟

I know SQL Server caches query result sets 我知道SQL Server缓存查询结果集

This is incorrect. 这是不正确的。 SQL Server does not cache query results, nor does any other relational database for the matter . SQL Server不会缓存查询结果, 也不会 缓存 任何其他关系数据库

You need to provide a clear example of what the problem is, the table structure, the queries you run, the expected result and the actual result. 您需要提供问题所在的明确示例,表结构,运行的查询,预期结果和实际结果。 As is, this question is unanswerable. 原来,这个问题是无法回答的。

Based on your comment about result caching I'm not convinced you understand transactions and isolation. 根据您对结果缓存的评论,我不相信您理解事务和隔离。 Assuming your code (which was not shown at all) at the very least does a query as opposed to showing again and again the same locally cached data, what you describe could be explained by lost updates (incorrect code in your app) and as well by operating under snapshot isolation and not understanding the behavior. 假设您的代码(根本没有显示)至少执行查询而不是一次又一次地显示相同的本地缓存数据,您所描述的内容可以通过丢失更新(应用程序中的代码不正确)以及通过在快照隔离下操作而不了解行为。

Sql server wont cache the results it caches the plan to generate the results. Sql server不会缓存它缓存计划生成结果的结果。 Can you put up what the query is that UpdateData calls? 你能提出UpdateData调用的查询吗? It seems maybe the transaction has begun but not committed? 似乎交易已经开始但未提交? Or if you are running the same UpdateData(), it might be creating a lock like condition on the table preventing other instances from updating. 或者,如果您正在运行相同的UpdateData(),则可能会在表上创建类似锁的条件,从而阻止其他实例进行更新。

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

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