Entity structure: query executing "select from" for no reason

I am having some issues with entity infrastructure. I am making a simple selection from a view in a database. However, when I look at the SQL that EF generates, it executes the query twice using select. So should it work? It seems very inefficient.

var reads = (from rt in ctx.C2kReadsToTransfer
                     where rt.ReadDt > fromDate
                     && rt.ReadDt < toDate
                     select rt);

This will convert to the next SQL

SELECT 

 [Extent1].[AMRID] AS [AMRID]
 , [Extent1].[Comments] AS [Comments]
 , [Extent1].[ExternalSystemType] AS [ExternalSystemType]
 , [Extent1].[LastReadDt] AS [LastReadDt]
 , [Extent1].[ReadDt] AS [ReadDt]
 , [Extent1].[Reading] AS [Reading]
 , [Extent1].[Units] AS [Units]
 , [Extent1].[Transferred] AS [Transferred]



 FROM 
     (SELECT 
      [ReadsToTransfer].[AMRID] AS [AMRID]
      , [ReadsToTransfer].[Comments] AS [Comments]
      , [ReadsToTransfer].[ExternalSystemType] AS [ExternalSystemType]
      , [ReadsToTransfer].[LastReadDt] AS [LastReadDt]
      , [ReadsToTransfer].[ReadDt] AS [ReadDt]
      , [ReadsToTransfer].[Reading] AS [Reading]
      , [ReadsToTransfer].[Transferred] AS [Transferred]
      , [ReadsToTransfer].[Units] AS [Units]
      FROM [dbo].[ReadsToTransfer] AS [ReadsToTransfer])
    AS [Extent1]

This seems very inefficient, especially if the table contains about 250 million rows, like ours. Also, if I stick with .Take (2000) at the end of the code, it just puts “select top 2000” only on the first selection. Thus, to select the top 2000 of the internal sample, which is the entire table.

Any thoughts on this?

+3
1

... SELECT - (, ) SELECT, ...

TOP 2000, , SELECT, , SELECT; , SELECT, .

, SELECT : , .

+2

Source: https://habr.com/ru/post/1732284/


All Articles