LINQ查询的索引 [英] Indexes for LINQ queries

查看:134
本文介绍了LINQ查询的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告知,有没有一种标准的方法可以使这样的查询更快

Please advise is there a standard way to make queries like this faster

var res = qlist.Where(o => o.left >= x && o.right >= x).ToList()

qlist对象最多可以包含一百万个元素,并且这种查询可能非常慢.是否有linq或类似的索引?

qlist object can contain up to million elements and such query can be very slow. Are there any indexes for linq or something similar?

谢谢

更新: 抱歉,无法在评论中回答. 它是linq-to-objects,目的是缓存数据库.

Update: Sorry cannot answer in comments. It is linq-to-objects, the aim is to cache DB.

推荐答案

除了告诉您为此使用数据库外(如已写过),您还可以通过两种方式提高性能:

Besides of telling you to use database for that (as written already), you can improve performance in two ways:

  1. 使用AsParallel:

  1. Use AsParallel:

var res = qlist.AsParallel().Where(o => o.left> = x& o.right> = x).ToList();

var res = qlist.AsParallel().Where(o => o.left >= x && o.right >= x).ToList();

您可以将项目插入两个不同的排序列表,一个通过left插入,另一个通过right插入.对于给定的x,通过二进制搜索找到等于(或几乎等于)的项目,然后从两个列表中获取该项目之后的所有项目.最终结果应该是从两个列表中选择的项目.我不知道这是否更有效.这取决于您要执行多少插入操作以及要找到多少项.

You can insert your items into two different sorted lists, one by left and the other by right. For a given x, find the item that equals (or nearly equals) to it with a binary search, and take all items after that item from both lists. The final result should be items that were selected from both lists. I don't know if it is more efficient. It depends how many inserts you do and how much items you'll find.

这篇关于LINQ查询的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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