简体   繁体   English

在.Net 2.0应用程序中缓存数据库表。 最快的访问方式是什么?

[英]Cache a DB table in a .Net 2.0 app. What's the fastest way of accessing it?

I have a (static) table which I need to access very frequently. 我有一个(静态)表,需要经常访问。 Therefore I cache it at run time. 因此,我在运行时将其缓存。

For now I'm using a DataTable to store the information and the Select() to retrieve it (the filtering is usually spread over 2 or 3 columns). 现在,我正在使用DataTable存储信息,并使用Select()检索信息(过滤通常分布在2或3列中)。 But this appears to be very (!) slow and is a major bottleneck. 但这似乎很慢(!),并且是一个主要瓶颈。

Therefore: What is the best way of caching a DB table (in .Net 2.0) and how to access it the fastest? 因此:缓存数据库表的最佳方法是什么(在.Net 2.0中),以及如何最快地访问它?


[edit 1] [编辑1]

DataTable cache = new DataTable();
cache.Columns.Add("id", typeof(int));
cache.Columns.Add("data_id", typeof(int));
cache.Columns.Add("data", typeof(string));
cache.Columns.Add("value", typeof(float));

Eg: 例如:

DataRow[] result1 = cache.Select("id = 1 AND data_id = 2 AND data = 'some data'");
DataRow[] result2 = cache.Select("data = 'some other data'", "data desc");

[edit 2] [编辑2]

Having in mind the suggestion from @MichaelPerrenoud, I ended up implementing a Dictionary. 考虑到@MichaelPerrenoud的建议,我最终实现了Dictionary。 Please have in mind that this design is with respect to the structure of the DB table that I needed cached. 请记住,这种设计是针对我需要缓存的数据库表的结构而言的。

Dictionary<string, Dictionary<NewObject, float>> cache;
class NewObject {int id; int data_id;}

This way the cache has as the [key] the data and as the [value] a new Dictionary. 通过这种方式, 高速缓存具有如[键]的数据 ,并作为[值]新的字典。 And this latter has as the [key] the id + data-id and as the [value] the value . 并且后者具有[key] id + data-id和[value]作为value

In accessing the data, this Dictionary is ~%90 faster than the DataTable. 在访问数据时,此Dictionary比DataTable快〜%90

So I think a good solution for you would be something like this. 因此,我认为为您提供一个好的解决方案是这样的。 I'm making the assumption that you have the DataTable after you initially get the data. 我假设最初获取DataTable后就拥有DataTable

DISCLAIMER: I have not compiled this code - it's a concept - you may need to fix some syntax. 免责声明:我尚未编译此代码-这是一个概念-您可能需要修复一些语法。

Dictionary<string, List<DataRow>> CacheData(DataTable t, params string[] fields)
{
    Dictionary<string, List<DataRow>> cache = new Dictionary<string, List<DataRow>>();
    StringBuilder key = new StringBuilder();

    foreach (DataRow r in t.Rows)
    {
        key.Clear();
        foreach (string f in fields)
        {
            if (key.Length > 0) { key.Append("_"); }
            key.Append(Convert.ToString(r[f]);
        }

        if (!cache.ContainsKey(key.ToString())) { cache.Add(key.ToString(), new List<DataRow>()); }
        cache[key.ToString()].Add(r);
    }
    return cache;
}

Now, you can store those dictionaries any way you see fit and then pull data out of them when you need to, so for example if you needed to handle the first example it might look something like this to create the cache: 现在,您可以按照自己认为合适的方式存储这些词典,然后在需要时从中取出数据,例如,如果需要处理第一个示例,则创建缓存可能看起来像这样:

CacheData(table, "id", "data_id", "data");

and then to get data out you would build a string like this: 然后要获取数据,您将构建一个像这样的字符串:

string key = "1_2_some data";

and pull the list of rows out of the dictionary like this: 并将行列表从字典中拉出,如下所示:

dictionary[key];

and that would return a List<DataRow> for you to use. 这将返回List<DataRow>供您使用。


But, you'll only have to do this one time and then leverage those dictionaries over and over. 但是,您只需要这样做一次 ,然后一遍又一遍地利用这些词典。

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

相关问题 .NET应用程序中的大内存块。处理它们的最佳/推荐/可扩展方式是什么? - Large memory blocks in a .NET app. What's optimal/recommended/scalable way to handle them? 使用.Net绘制解析树的最简单,最快捷的方法是什么? - What's the simplest and fastest way to draw a parse tree using .Net? 什么是在.net 2中生成唯一集的最快方法 - what is the fastest way to generate a unique set in .net 2 什么是编写XML的最快方法 - what's the fastest way to write XML 什么是.NET程序最快的IPC方法? - What's the fastest IPC method for a .NET Program? 什么是.Net程序索引用户计算机上所有图像的最快方法? - What is fastest way for a .Net program to index all images on a user's computer? 使用LibTiff.Net从tif图像中获取tiff标签计数的最佳/最快方法是什么? - What's the best/fastest way to get the count of tiff tags from a tif image with LibTiff.Net? 将大量记录插入SQL Server数据库的最快方法是什么? - What is the fastest way to insert a large amount of records into a SQL Server DB? 在C#.NET 2.0中,反向执行foreach的简单方法是什么? - In C# .NET 2.0, what's an easy way to do a foreach in reverse? 这段代码有什么问题(.net 2.0) - what's wrong with this code (.net 2.0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM