从DB分层数据 - 加入或不加入 [英] Hierarchical data from DB - to join or not to join

查看:122
本文介绍了从DB分层数据 - 加入或不加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到这个问题的一个清洁,高性能的解决方案,但我莫名其妙地卡住了。

I am trying to find a clean and performant solution for this problem, but am stuck somehow.

花絮:结果
-ASP.Net C#应用程序(.NET 3.5)结果
-MS-SQL Server 2005的

Trivia:
-ASP.Net C# application (.Net 3.5)
-MS-SQL Server 2005

这是数据看起来的样子:结果
分类 - >模板 - >实例

This is how the data looks like:
Category -> Template -> Instance

一个类别可以包含多个模板。结果
模板可以包含多个实例。

A Category can contain multiple Templates.
A Template can contain multiple Instances.

有是为每个3的等级和相应的数据库表中有很多列。

There is a class for each of these 3 and a corresponding database table with a lot of columns.

我想从数据库中品类齐全加载到一个C#类的类对象,包括所有相关的模板和实例对象。

I want to load a complete category from the database into a C# Category class object, including all the related template and instance objects.

我有两个选择现在:结果
1)的请在所有3个表的加入,一次读取所有的数据。的结果
颠倒:很多在数据库方面更快,在一个查询中的所有信息结果
下行:我发送了大量的冗余数据,因为每一行中有每个实例

I have two options now:
1) Do a join on all 3 tables and read all the data at once.
Upside: A lot faster on the database side, all information in one query.
Downside: I transmit a lot of redundant data, because in each row there is the same category and template data for each instance.

例(简体):

CategoryID | CategoryName | TemplateID | TemplateName | InstanceID | InstanceName  
1 | FirstCategory | 1 | FirstTemplate | 1 | FirstInstance   
1 | FirstCategory | 1 | FirstTemplate | 2 | SecondInstance  
1 | FirstCategory | 1 | FirstTemplate | 3 | ThirdInstance  
1 | FirstCategory | 1 | SecondTemplate | 4 | FourthInstance  

2)的我查询自身的每个表中,第一收集该类别数据,则与分类ID等相关的模板数据。的结果,
颠倒:直观的形式给出,容易处理的code面,没有冗余数据被取出结果
下行:多个查询服务器,可能是慢

2) I query each table on its own, first collecting the category data, then the related template data with the category ID and so on.
Upside: Intuitive aproach, easier to handle on the code side, no redundant data is fetched.
Downside: Multiple queries to the server, probably slower.

什么是去这里最好的方法是什么?我失去了一个选择吗?结果
解决方案1似乎有更好的性能比较,但看起来不干净给我。我就一定得数据的类别出一大堆数据行的。

What is the best way to go here? Am I missing an option?
Solution 1 seems to have better perfomance, but it looks "unclean" to me. I would have to get data for a category out of a whole bunch of data rows.

如果我选择的解决方案1,这是获取类别和模板数据的最佳方式?结果
从第一个数据行阅读并一度创造价值变动一个新的实例?结果
做一些分组?

If I choose solution 1, which is the best way to fetch the category and template data?
Read it from the first data row and create a new instance once the value changes?
Do some sort of grouping?

在此先感谢!这个问题让我头疼,因为天。

Thanks in advance! This problem is giving me headaches since days.

推荐答案

我使用的一个项目,我现在在做实体框架。虽然在某些情况下分析它,它的确使用选项1,带回表冗余数据。如此看来,微软选择了这种做法,他们所拥有的整个堆栈所以presumably知道如何使这个确切的问题一个很好的决定。

I'm using Entity Framework on a project I'm doing at the moment. While profiling it under certain scenarios, it indeed uses option 1 and brings back the table with redundant data. So it seems that Microsoft opted for this approach, and they own the entire stack so presumably know how to make a good decision about this exact problem.

有可能会出现一些启发式的决定使用选项2在某些情况下,但我还没有在我的分析看到它。此外,我还没有看到过EF返回多个结果集在单个查询。

There may be some heuristic that decides to use option 2 under certain scenarios, but I haven't seen it in my profiling. Furthermore I haven't seen EF ever return multiple result sets in a single query.

这篇关于从DB分层数据 - 加入或不加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆