IQueryable 与字典的性能对比

发布于 2024-11-07 14:26:18 字数 1368 浏览 0 评论 0 原文

我在启动时在我的应用程序中缓存了一大堆静态元数据。它来自数据库,并且有大量的外键关系。我正在研究对它们进行建模的最佳方法。 我刚刚开始使用 LINQ。

对我来说声明一个类很容易

public class AllData {
    public static IQueryable<libm_ColPurpose> IQ_libm_ColPurpose = libm_ColPurpose.All();
    public static IQueryable<libm_ColType> IQ_libm_ColType = libm_ColType.All();
...

(我使用 SubSonic 3 来生成我的类,但这不是重点)。 然后我可以使用 IQueryable>成员来访问我想要的任何内容,例如:

libm_ColType ct = AllData.IQ_libm_ColType.SingleOrDefault(x => x.ColTypeStr == this.DefaultJetColTypeStr);

在使用 IQueryable 之前,我使用字典来存储 FK 关系,因此为了模仿上面的代码,我将从预先存在的列表中编写以下代码>>列表

Dictionary<string, libm_ColType> colTypeByColTypeStr = new Dictionary<string, libm_ColType>();
foreach (libm_ColType x in list) { rtn.Add(x.ColTypeStr, x); }

,然后我可以使用

libm_ColType ct = colTypeByColTypeStr[this.DefaultJetColTypeStr];

“确定”,所以最后我们得到了问题!

通过 ID 进行字典查找非常高效,但是 IQueryable 解决方案更加灵活和优雅。

我想知道使用 IQueryable 会对性能造成多大影响。我怀疑每次调用该列表时都会对列表进行线性扫描,如果涉及大量记录,那么重复调用确实会累加。 如果我能够识别唯一值列并在第一次查找后生成并缓存一个哈希表,那就太好了,但我怀疑这不会成为该产品的一部分。

对于使用 LINQ,这对我来说有点破坏性。

请注意(我将再次重复),我不是从数据库中提取数据,它已经在内存中并且我在那里查询它,所以我只对查找内存中的 IQueryable< 感兴趣;T>.

I'm caching a whole bunch of static metadata in my app at startup. It's from a db and there are tons of Foreign Key relationships. I'm looking into the best way of modelling them.
I've just started off with LINQ.

It's easy for me to declare a class

public class AllData {
    public static IQueryable<libm_ColPurpose> IQ_libm_ColPurpose = libm_ColPurpose.All();
    public static IQueryable<libm_ColType> IQ_libm_ColType = libm_ColType.All();
...

(I'm using SubSonic 3 to generate my classes, but that's beside the point).
Then I can use the IQueryable<T> members to get access to anything I want, for example:

libm_ColType ct = AllData.IQ_libm_ColType.SingleOrDefault(x => x.ColTypeStr == this.DefaultJetColTypeStr);

Prior to using IQueryable I was using Dictionaries to store FK relationships, so to mimic the above code I'd code the following from a preexisting List<libm_ColType> list

Dictionary<string, libm_ColType> colTypeByColTypeStr = new Dictionary<string, libm_ColType>();
foreach (libm_ColType x in list) { rtn.Add(x.ColTypeStr, x); }

and then I could use

libm_ColType ct = colTypeByColTypeStr[this.DefaultJetColTypeStr];

OK, so finally we get to the question !

The Dictionary lookup by ID is extremely efficient, however the IQueryable solution is far more flexible and elegant.

I'm wondering how much of a performance hit I'm going to get using IQueryable. I suspect I am doing a linear scan of the list each time I call it, and that's really gonna add up over repeat calls if there are a lot of records involved.
It woul be great if I could identify unique-valued columns and have a hashtable generated and cached after the first lookup, but I suspect this is not gonna be part of the offering.

This is a bit of a dealbreaker for me regarding using LINQ.

Note (I'll repeat it again) that I'm NOT pulling data from a database, it's already in memory and I'm querying it there, so I'm only interesting in looking up the in-memory IQueryable<T>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2024-11-14 14:26:18

IQueryable 表示数据存储中的集合,因此内存中可能没有这些集合。如果您明确想要内存中的集合,那么我会回到您的字典。请记住,这并不妨碍您对数据使用 LINQ 查询。

IQueryable represents a collection in a data-store, so you probably don't have the collections in memory. If you explicitly want in-memory collections, then I would go back to your dictionaries. Remember, this doesn't prevent you from using LINQ queries over the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文