空闲一段时间后,如何防止LocalDB分离MDF文件? [英] How to prevent LocalDB from detaching MDF file after some idle time?

查看:79
本文介绍了空闲一段时间后,如何防止LocalDB分离MDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个.NET 4.5(winforms)应用程序,该应用程序使用LocalDB通过以下连接字符串来处理本地MDF文件:

I'm building a .NET 4.5 (winforms) application that uses LocalDB to work with a local MDF file, using this connection string:

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\DB\DatabaseFile.mdf;Integrated Security=True;MultipleActiveResultSets=True

当我运行我的应用程序时,第一个SQL查询需要一些时间来执行-没什么大不了的,大约需要2到3秒钟.之后,所有下一个SQL查询将立即执行.我假设在第一次执行过程中需要额外的几秒钟才能将MDF文件附加到本地SQL Server服务.对吧?

When I run my application, the first SQL query takes some time to execute - nothing drastic, about 2 or 3 seconds. After that, all next SQL queries are executed instantly. I assume that the extra seconds during the first execution are needed to attach the MDF file to a local SQL Server service. Right?

但是,我注意到,如果自上一次执行SQL查询以来经过了10分钟(或大约10分钟),则下一个SQL查询将再次花费那2-3秒的时间来执行.我假设经过一段空闲时间后,MDF会分离,并且在调用新的SQL命令时,它将再次重新附加它.

I noticed, however, that if 10 minutes (or so) pass since an SQL query was last performed, the next SQL query will again take those 2-3 seconds more to execute. I assume that after some idle time, the MDF gets detached, and when the new SQL command is called, it re-attaches it once again.

我想知道,是否有一种方法可以覆盖此行为?

I'm wondering, is there a way to override this behavior?

我知道我可以创建一个Timer,每隔几分钟执行一次简单的查询,但是有没有更好,更干净的解决方案?

I know I could create a Timer, that performs a simple query every few minutes, but is there a better, cleaner solution?

推荐答案

我更怀疑您的数据/索引不再在内存中.

I would more suspect that your data / index is no longer in memory.

您可以运行一个非常快速的查询,例如选择"a",以了解是否是连接时间.

You could run a very fast query like select 'a' to know if it was the connect time.

关于强制您的表/索引保留在内存中.
我非常建议您让.NET和SQL进行自己的内存管理.

As for forcing your table / index to stay in memory.
I would so much advise you to let .NET and SQL do their own memory management.

2-3秒不是很多.
查看查询计划-也许您可以使其更快(甚至不在内存中).

2-3 seconds is not much.
Look at the query plan - maybe you can make it faster (even not in memory).

这篇关于空闲一段时间后,如何防止LocalDB分离MDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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