簡體   English   中英

linq to sql將查詢轉換為“ select *”格式是否效率低下?

[英]Is it inefficient that linq to sql translates this query into the format: “select *”?

我對Linq to SQL並不是很熟悉,但是令我印象深刻的是:

var articles = 
  (from a in DB.Articles
  where 
  a.ArticleId == ArticleId.Question &&
  a.DeletedAt == null &&
  a.Votes >= minVotes
  orderby a.UpdatedAt descending
  select a).
  Take(maxarticles);

被翻譯成這樣:

string query = 
  "select top 10 * from articles
  where 
  ArticleId = 1 and 
  DeletedAt is null and 
  Votes >= -5
  order by UpdatedAt desc";

令我震驚的是,linq to sql願意使用“ select *”類型的查詢來清理所有內容,效率低下。 這不是效率低下嗎?

為什么linq to sql這樣做?

不使用“ SELECT *”的想法是避免帶來不必要的列。

在您的情況下,您專門要求linq to sql在指定“選擇a”時將所有列都帶入

如果要選擇較少的列,則必須在linq查詢中使用投影。

var articles = (from a in DB.Articles
    where a.ArticleId == ArticleId.Question 
        && a.DeletedAt == null 
        && a.Votes >= minVotes
    orderby a.UpdatedAt descending
    select new 
    {
       a.ArticleId,
       a.Votes
    })
    .Take(maxarticles);

做類似上面的事情會轉化成看起來像這樣的SQL ...

select top 10 
         ArticleId,
         Votes
from     articles
where    ArticleId = 1 
         and DeletedAt is null
         and Votes >= -5
order by UpdatedAt desc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM