从数据库查询还是从内存查询?哪个更快?
我正在尝试提高用 C# 和 .NET 2.0 开发的、处理大量文件的 Windows 服务的性能。我想每秒处理更多文件。
在其过程中,对于每个文件,该服务都会执行数据库查询以检索系统的某些参数。
这些参数每年都会发生变化,我认为如果将这些参数作为单例加载并定期刷新该单例,我会获得一些性能。我不会对每个正在处理的文件进行数据库查询,而是从内存中获取参数。
完成该场景:我使用 Windows Server 2008 R2 64 位,SQL Server 2008 是数据库,C# 和 .NET 2.0,如前所述。
我的方法正确吗?你会怎么办?
谢谢!
I am trying to improve the performance of a Windows Service, developed in C# and .NET 2.0, that processes a great amount of files. I want to process more files per second.
In its process, for each file, the service does a database query to retrieve some parameters of the system.
Those parameters change annually, and I am thinking that I would gain some performance, if a loaded those parameters as a singleton and refreshed this singleton periodically. Instead of make a database query for each file being processed, I would get the parameters from memory.
To complete the scenario : I am using Windows Server 2008 R2 64 Bits, SQL Server 2008 is the database, C# and .NET 2.0 as already mentioned.
I am right in my approach? What would you do?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,请将它们缓存在内存中。特别是当它们很大或很复杂时。
您应该注意每年在正确的时间使它们失效一次,具体取决于其准确性。
简单地将它们缓存一个小时甚至几分钟可能是一个很好的折衷方案。
Yes, do cache them in memory. Especially if they are large or complex.
You should take care to invalidate them at the right time once a year, depending how accurate that has to be.
Simply caching them for an hour or even for a few minutes might be a good compromise.
RAM 内存数据访问绝对比任何其他数据访问更快,除了注册表和 CPU 缓存等 CPU 内存之外,
即使您每分钟更改一次,缓存也会更快,所以是的,缓存该查询速度非常快
RAM memory data access is definitely faster that any other data access, except than cpu memories like registries and CPU cache
Caching would be faster even if you change it every minute, so yes, caching that query is very faster
跨网络或访问磁盘总是比内存访问慢几个数量级。
数据库可以在内存中缓存数据,因此如果您可以实现这一点并且不跨网络,那么数据库可能会更快,因为它们的数据访问模式/索引等...可能比您更快。重新编码。但是,这是最好的情况 - 如果您需要更快,内存缓存会有所帮助。
但是,请注意,内存缓存可能会增加复杂性和错误。您必须确定缓存数据的生命周期、如何刷新,并且它越复杂,您将遇到的奇怪的边缘情况状态错误就越多。尽管它们每年都会发生变化,但你必须处理好这个风口浪尖。
Crossing a network or going to disk is always orders of magnitude slower than in memory access.
Databases can cache data in memory so if you can achieve that and you're not crossing a network, the database might be faster since their data access patterns/indexes etc... may be faster than you're code. But, that's best case - if you need it faster, in memory caches help.
But, be aware that in memory caches can add complexity and bugs. You have to determine the lifetime of the cached data, how to refresh and the more complex it is, the more wierd edge case state bugs you will have. Even though they change annually, you have to handle that cusp.