多个查询与加入 [英] Multiple queries vs join

查看:80
本文介绍了多个查询与加入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10张桌子。每张桌子都取决于另一张桌子。我在一个查询中使用连接(超过10个连接)来获取结果。

问题是:



1.我找到很多因为对于例如每个主键,单个主键列的重复数据的数量。表1中有3个匹配,而表中有2个匹配,依此类推。因此,数据重复是3 * 2 * .. * n倍。这是否真的很重要,以便我获取单个记录?



2.如此多的连接会产生一些复杂性(不是问题)但是它会更好在提取速度上比较它们时使用多个查询而不是连接?

I have 10 tables. Each table depends on the other. I am using joins (more than 10 joins) in a single query to fetch result.
Problems are:

1. I find lots of repeated data for a single primary key column since for each primary key for eg. there are 3 matches in table 1 whereas 2 matches in table and so on. Thus data repeatation is 3*2*..*n times. Does this really matters in case I am fetching a single record?

2. So many joins create a bit of complexity(not an issue) but will it be better to use multiple queries instead of joins on comparing them on fetching speed?

推荐答案

除了 Advy Pandya [ ^ ],我建议你阅读: SQL连接的可视化表示 [ ^ ]
In addition to solution 1 by Advy Pandya[^], i'd suggest to read this: Visual Representation of SQL Joins[^]


您好,



在您的查询中,您将获得重复数据。重复背后的原因是表格可能与一个t连接很多关系。因此,您需要在连接中添加单独的条件以过滤掉重复的数据。单独的条件取决于您尝试获得的输出。



多个查询永远不会替换连接。我不确定您要获得的结果,但多个查询可能不是您的解决方案。即使你会得到输出,当然使用多个查询也会拒绝你的表现。



你问题的最佳解决方案是修复一对多关系过滤掉重复的数据。 ON之后的单独条件将帮助您过滤掉重复。



如果您有任何疑虑或疑问或需要更多信息,请与我们联系。 。



谢谢
Hello,

In your query you are getting repeated data.The reason behind the repetition is the tables might be connected with one to many relation. So, you will need to put separate condition in the join to filter out the repeated data.The separate condition depends on the output that you are trying to get.

Multiple queries are never replacement of join. I am not sure about the result that you are trying to get, but multiple queries may not be your solution. and even if you will get output, of course the performance will get refused using multiple queries.

The best solution for your problem is to fix the one to many relation by filter out the repeated data. separate condition after "ON" will help you to filter out the repetition.

Please let me know if you have any concern or query or if you need more information on this.

Thanks


这篇关于多个查询与加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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