LINQ Skip()问题 [英] LINQ Skip() Problem

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

问题描述

以下C#语句阻止了该过程,并且在以下情况下无法检索数据: itemToSkip大于0.

The C# statement below blocks the process and cannot retrieve data if itemToSkip is greater than 0.

 int itemToSkip = 100;
 int itemToTake = 1000;

 var itemList = db.MYTABLEs.Skip(itemToSkip).Take(itemToTake).ToList();

我该如何解决?有什么问题吗?

How can I fix it? what is the problem?

推荐答案

不确定您拥有的提供db.MYTABLEs的提供程序.除非我们知道db.MYTABLEs的行为,否则实际上不可能回答您的问题.

Not sure what provider you have that provides db.MYTABLEs. It is really not possible to answer your question unless we know how db.MYTABLEs behaves.

在普通的LINQ中,跳过不只是跳过而已;它必须遍历数据量才能跳过.因此,对于您的14gb数据表,它将遍历第一个跳过"记录数.如果此迭代速度很慢,则您不会通过跳过来节省任何CPU/时间.

In normal LINQ, skip does not just skip ahead; it has to iterate through the amount of data in order to skip. Therefore, for your 14gb data table, it will be iterating through the first "skip" number of records. If this iteration is slow, you are not saving any cpu/time by skipping.

对于某些提供商,例如对于SQL源,可以使用游标来实现跳过,这又可能很慢.如果是SQL Server,则可以使用更快的关键字对其进行优化.

For some providers, e.g. an SQL source, skip may be implemented using cursors, which can again be slow. If it is SQL Server, it may be optimized with a keyword which may be faster.

如果是LINQ-to-SQL,它将使用"NOT EXISTS"子句将查询转换为SQL,这将极其很慢,因为它必须经过 entire 表,如果NOT EXISTS子句未命中索引.请参阅以下内容(链接):

If it is LINQ-to-SQL, it translates the query into SQL using a "NOT EXISTS" clause, which will be extremely slow because it has to go through the entire table if the NOT EXISTS clause does not hit an index. See the following (link):

LINQ to SQL可以跳过 通过使用带有SQL NOT的子查询 EXISTS子句.此翻译有 有以下限制:

LINQ to SQL translates Skip by using a subquery with the SQL NOT EXISTS clause. This translation has the following limitations:

  • 该参数必须是一个集合.即使已订购,也不支持多集.

  • The argument must be a set. Multisets are not supported, even if ordered.

生成的查询可能比为应用Skip的基本查询生成的查询复杂得多.这种复杂性可能会导致性能下降甚至超时.

The generated query can be much more complex than the query generated for the base query on which Skip is applied. This complexity can cause decrease in performance or even a time-out.

换句话说,文档说不要这样做."

In other words, the docs says "don't do it."

仅适用于具有随机访问功能的提供商,例如内存数组将非常快地跳过,因为提供程序可以直接跳转.

Only for providers with random-access features, e.g. an in-memory array, will skip be really fast because the provider can just jump ahead.

最糟糕的情况是,如果您使用的是提供程序上运行的提供程序,而该提供程序使用跳过/获取"功能会自动对整个数据集进行排序.如果您有14gb的数据,那么这种排序将真的很慢.

The worst case will be if you are running on a provider that automatically sorts the entire data set if you use Skip/Take. If you have 14gb of data, then this sort is going to be really slow.

您需要进行更多实验,以查看您的程序是否处于跳过状态,或者只是占用所有试图迭代的cpu.

You need to experiment some more to see if your program is hanging on skip, or just hogging all the cpu trying to iterate through.

如果您只想将数据分成可管理的块,则可能不应该使用跳过/获取,它会每次都重新查询数据源.

If you are only trying to divide your data into manageable chunks, you probably should not be using skip/take, which requeries the data source every time.

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

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