简体   繁体   English

哪一个更快? 查询1个实体,并在其他相关实体上进行往返? 还是刚离开1次往返旅行?

[英]Which one is faster? Query 1 Entity and make roundtrip on other related entities? or just left join it with 1 roundtrip?

public class Item
{
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public decimal Cost { get; set; }
    public Department ItemDept { get; set; }
    public Category ItemCat { get; set; }
    public List<Supplier> Suppliers { get; set; }
    public List<Barcode> Barcodes { get; set; }
    public List<Discount> Discounts { get; set; }

}

Im using Dapper so my query is plain SQL talking to Sql Server 2008 我正在使用Dapper,因此我的查询是与SQL Server 2008对话的普通SQL

how do I improve performance on this? 我该如何提高性能? assuming the query is get Item by Id(Primary key) which one is faster or better? 假设查询是通过ID(主键)获取Item的,哪个更快或更佳? - with one query full of joins(left join, inner join..etc) (1 roundtrip only) or - query the item first then execute another query for related tables(many roundtrips) or do you guys have any recommendations? -使用一个充满连接的查询(左连接,内部连接等)(仅1个往返)或-首先查询该项目,然后对相关表执行另一个查询(许多往返),或者您有什么建议吗?

This is too long for a comment. 这个评论太长了。

The best way to answer a performance question is to try it out on your data on your systems. 回答性能问题的最佳方法是在系统上的数据上进行尝试。 You can then check what really works better. 然后,您可以检查真正有效的方法。

Informed speculation says that letting the database do the joins is the right thing to do. 明智的猜测是,让数据库进行联接是正确的做法。 Databases are designed for this operation and they should implement joins effectively. 数据库是为此操作而设计的,它们应该有效地实现联接。 Perhaps more importantly, there is overhead for each query that you run. 也许更重要的是,您运行的每个查询都有开销。 Overhead to compile the query. 开销来编译查询。 Latency is passing the query to the database. 延迟将查询传递给数据库。 Database overhead in processing the results and getting them back to the application. 处理结果并将其返回给应用程序的数据库开销。

That also suggests that doing more work in the database is a good thing. 这也表明在数据库中进行更多工作是一件好事。 But, you should check on your system. 但是,您应该检查系统。

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

相关问题 如何在Entity Framework Core中为几个多对一相关实体建立几个左联接查询 - How to build several left join query in Entity Framework Core for several many to one related entities 在没有往返的情况下更新实体框架6 - Update in Entity Framework 6 without a roundtrip 使用 Dapper 单批(一次往返)更新和查询 - Update and query in single batch (one roundtrip) using Dapper EntityFramework更新与数据库的一次往返 - EntityFramework Update with one roundtrip to the database 如何在调用 AddAsync 后加载相关实体而不再次往返数据库 - How to load a related entity after call AddAsync without making another roundtrip to the database 使用 Dapper.NET 在一次往返中执行多个 SQL 语句 - Multiple SQL statements in one roundtrip using Dapper.NET 使用往返浮动到字符串 - float to string using roundtrip 无法往返DateTime格式 - Cannot Roundtrip DateTime Format Linq To Entities中的左外部联接(不能在LINQ to Entities查询中构造实体或复杂类型。) - Left outer join in Linq To Entities (The entity or complex type cannot be constructed in a LINQ to Entities query.) 往返串行化-反序列化的替代 - replacement for roundtrip serialize-deserialize
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM