Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql O/R映射:单个复杂查询与多个简单查询_Sql_Linq_Performance_Entity Framework 4_Cartesian Product - Fatal编程技术网

Sql O/R映射:单个复杂查询与多个简单查询

Sql O/R映射:单个复杂查询与多个简单查询,sql,linq,performance,entity-framework-4,cartesian-product,Sql,Linq,Performance,Entity Framework 4,Cartesian Product,我对SQL查询的结果集是如何从服务器传输到客户机持怀疑态度 大多数O/R映射程序都支持即时加载和延迟加载,两者都有各自的优点和缺点。 e、 Entity Framework4.NET具有出色的即时加载支持 但是,假设我们有这样一个模型: BlogPost { public string Body {get;set;} ICollection<Comment> Comments {get;set;} } var posts = context .P

我对SQL查询的结果集是如何从服务器传输到客户机持怀疑态度

大多数O/R映射程序都支持即时加载和延迟加载,两者都有各自的优点和缺点。 e、 Entity Framework4.NET具有出色的即时加载支持

但是,假设我们有这样一个模型:

BlogPost
{
    public string Body {get;set;}
    ICollection<Comment> Comments {get;set;}
}
var posts = context
          .Posts
          .Include(post => post.Comments)
          .Where(post => post.Id == 1)
          .First();
这将导致一个SQL查询,在该查询中,每条注释的每一行上都会重复Post的所有数据

假设我们在一篇特定的帖子和帖子上有100条评论。正文是大量的文本。这不好吗? 或者,数据在发送到客户端时是否以某种方式进行了压缩,从而最大限度地减少了每行重复数据的开销

确定一个这样的查询是否比两个简单的查询(一个用于获取帖子,一个用于获取评论)更有效的最佳方法是什么

在开发环境中对此进行基准测试是毫无意义的,这里有多个因素: SQL server上的CPU负载 网络负载 应用服务器物化对象上的CPU负载

对此有何想法

[编辑] 澄清:

两个查询类似于以下内容:

BlogPost
{
    public string Body {get;set;}
    ICollection<Comment> Comments {get;set;}
}
var posts = context
          .Posts
          .Include(post => post.Comments)
          .Where(post => post.Id == 1)
          .First();
sql

结果

id , topic, body , etc...
id,postid, commenttext , etc...
p.id , p.topic, __p.body__, c.id, c.postid, c.commenttext
sql

结果

id , topic, body , etc...
id,postid, commenttext , etc...
p.id , p.topic, __p.body__, c.id, c.postid, c.commenttext
第一个查询将生成一行,第二个查询将生成与注释数量相同的行

对于单个查询,行数与特定帖子的注释数相同,但所有帖子数据在每一行上重复

结果

id , topic, body , etc...
id,postid, commenttext , etc...
p.id , p.topic, __p.body__, c.id, c.postid, c.commenttext
p、 正文将在每行上重复,从而使结果集非常大。
假设p.body包含大量的数据-

我认为这实际上可以归结为以下几点:

有多少个帖子? 获取帖子的评论有多复杂? 如果您有数百万篇文章,那么最好使用一个查询,即使您对每个文章都有几个注释,因为聚合的往返时间将比传输附加数据的时间差得多。 所以,我认为你需要有一双敏锐的眼睛-
而且,我认为在开发环境中进行基准测试并非毫无意义,因为它至少可以给出两种方法之间的关系。

使用一个返回大量行的查询几乎总是比许多只返回一行的查询更快

但是,在您的情况下,首先检索用户,然后通过单个查询检索所有注释可能比在一个查询中获取所有内容更有效