简体   繁体   English

Linq MVC5 MSQL select 包含大量列的表中的一些列,以便更好更快地查询

[英]Linq MVC5 MSQL select some columns from a table containing larg amount of columns in order to make better and fast query

For example a table that is containing like 30 columns例如一个包含 30 列的表

table1{col1,col2,col3,col4,col5,col6....,col30}

here we can do like在这里我们可以做

        List<table1> result = (from a in dbconn.table1.AsEnumerable()

                                        select new table1
                                        {
                                            col1= a.col1,
                                            col2= a.col2
                                        }).ToList();
        return result.ToList();

same as如同

 SELECT 
[Extent1].[col1] AS [col1], 
[Extent1].[col2] AS [col2],
[Extent1].[col3] AS [col3], 
....
....
....
[Extent1].[col30] AS [col30], 
FROM [dbo].[table1] AS [Extent1]

-> almost 2 minutes -> 差不多 2 分钟

but this is not correct because while doing this i am checking the result in SQL Server Profiler the result of this linq query is getting all the table table1 data then dumping to new table1 which is time consuming.但这不正确,因为在执行此操作时,我正在检查SQL Server Profiler中的结果,此 linq 查询的结果是获取所有表table1数据,然后转储到新 table1,这很耗时。

now this makes the query very slow because larg amount of data is there so in sql we can just现在这使得查询非常慢,因为有大量数据,所以在 sql 我们可以

select col1,col2 from table1 

-> almost 2 seconds -> 差不多 2 秒

how can we do it in linq with out getting the full table and make the linq same or near to SQL.我们如何在 linq 中做到这一点而不获取完整的表并使 linq 与 SQL 相同或接近。

AsEnumerable() is effectively a cast to IEnumerable, which makes member resolution find members of Enumerable instead of Queryable. AsEnumerable() 实际上是对 IEnumerable 的强制转换,这使得成员解析找到 Enumerable 的成员而不是 Queryable。 It's usually used when you want to force part of a query to run as SQL (or similar), and the remainder to run using LINQ to Objects.当您想强制查询的一部分以 SQL(或类似)运行,其余部分以 LINQ 运行到对象时,通常使用它。

(from a in dbconn.table1
     select new
     {
         col1= a.col1,
         col2= a.col2
     }).ToList();

Also, check indexes - will speed up more此外,检查索引 - 将加快速度

The first rule to remember when working with LINQ to SQL is next:使用 LINQ 到 SQL 时要记住的第一条规则是:

Calling .ToList() , .调用.ToList() , . ToArray() , .ToDictionary() and .AsIEnumerable() performs query materialization which means that your IQueriable will be transformed to SQL with all .Where() , .Select() , .Order() (etc.) predicates BEFORE it and will be executed. ToArray().ToDictionary().AsIEnumerable()执行查询物化,这意味着您的 IQueriable 将转换为 SQL 与所有.Where().Select().Order() (等)谓词之前它和将被执行。

In your example, you called .AsIEnumerable() directly on DbSet which means you will execute query without any predicates or limits and all data from this table from Database will be pushed to the memory and only after that your .Where() and .Select() will be applied, right in the memory在您的示例中,您直接在 DbSet 上调用.AsIEnumerable() ,这意味着您将执行没有任何谓词或限制的查询,并且来自数据库的该表中的所有数据都将被推送到 memory 并且仅在之后您的.Where().Select()将被应用,在 memory

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM