简体   繁体   English

SQL Server多个多对多联接查询

[英]SQL Server multiple many-to-many join query

I currently have a one main table with multiple other tables associated with it via many-to-many joins (with join tables). 我目前有一个主表,并通过多对多联接(带有联接表)将其与多个其他表关联。 The application using this database needs to have search functionality that will print out multiple rows matching specific criteria, including all the values in the join tables. 使用该数据库的应用程序需要具有搜索功能,该功能将打印出符合特定条件的多行,包括连接表中的所有值。 The values from the join tables also need to be links that will enable a search for all other rows that match that value. 联接表中的值也需要是链接,以允许搜索与该值匹配的所有其他行。 I am trying to figure out how to do this without taxing the database. 我试图弄清楚如何在不增加数据库负担的情况下执行此操作。

Here is an example of the table structure 这是表结构的一个例子

**Metrics (Main Table)** 
MetricID (pk) 
Metric

**Domains (ValueList Table)** 
DomainID (pk) 
Domain

**MetricsDomains (Join Table)** 
MetricsDomainsID (pk) 
MetricID (fk) 
DomainID (fk)

**MetricTypes (ValueList Table)** 
MetricTypeID (pk) 
MetricType

**MetricsMetricTypes (Join Table)** 
MetricMetricTypesID (pk) 
MetricID (fk) 
MetricTypeID (fk)

**Studies (ValueList Table)** 
StudyID (pk) 
Study

**MetricsStudies (Join Table)** 
MetricsStudiesID (pk) 
MetricID (fk) 
StudyID (fk)

When someone searches for a Metric by various criteria, they should get output in table format that looks something like this: 当有人通过各种条件搜索指标时,他们应该以表格式获得输出,如下所示:

Metric1 | Description | Study1, Study2, Study3 | MetricType1, MetricType2 | Domain1, Domain2
Metric2 | Description | Study5, Study2, Study4 | MetricType2, MetricType3 | Domain5, Domain9

The Metric will be a link to the full description of the Metric. 度量标准将链接到度量标准的完整说明。 However, in addition, the Studies (ie., Study 1, Study 2, Study 3, etc.) and MetricTypes (MetricType1, Metric2, etc.) and Domains (Domain1, Domain 2, etc.) should also be links, that when clicked on, will perform a new search for all other metrics that contain that study, or type, or domain. 但是,此外,研究(即研究1,研究2,研究3等)和MetricTypes(MetricType1,Metric2等)和域(Domain1,Domain 2等)也应该是链接,单击时,将对包含该研究,类型或域的所有其他指标进行新搜索。 This leads me to believe I will also need the primary key of the study, type, or domain, in addition to the text, in order to place in the href. 这使我相信,除了文本之外,我还需要研究的主键,类型或域,以便将其放置在href中。

At any rate, considering one search could possibly return 20+ metrics, what I need to figure out is a good way to write an optimized query to return the results of the multiple many-to-many joins. 无论如何,考虑到一次搜索可能返回20多个指标,我需要弄清楚的是编写优化查询以返回多个多对多联接的结果的好方法。 I know that joining all of these tables in one query will generally result in a Cartesian product of all the joins, but I am not sure if there is another way to go about it. 我知道在一个查询中联接所有这些表通常会导致所有联接的笛卡尔积,但是我不确定是否还有其他方法可以解决。 I have also read about a way I could return the many-to-many results as a comma-separated list in a field using a method like this: 我还阅读了一种方法,可以使用如下方法在字段中以逗号分隔的列表形式返回多对多结果:

SELECT m.MetricID, Description, 
    STUFF((
    SELECT ', ' + s.Study
    FROM Studies s, Metrics_Studies ms 
    WHERE s.StudyID = ms.StudyID AND ms.MetricID = m.MetricID
    ORDER BY s.Study
    FOR XML PATH('')
    ),1,1,'') as Study, 
FROM Metrics m 
WHERE Metric_PK = 13

However, I am not sure of the performance impact of this method, or whether it will really get me what I am looking for since I think I may need the primary keys of the Studies as well. 但是,我不确定此方法对性能的影响,或者它是否真的能为我提供所需的信息,因为我认为我可能还需要研究的主键。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks! 谢谢!

I'd recommend doing it first with your multi-joins - only then will you know whether the perofrmance is good enough. 我建议您首先使用多联接进行操作-然后才能知道性能是否足够好。 As always you have to beware of premature optimisation. 与往常一样,您必须提防过早的优化。 Once you have your query running correctly against your normalised model, you can check the query plan, etc. This may highlight that you need to flatten a few of your joins, if that's the case you probably will have to store the data in two different formats, one for reporting / one for searching, etc. But 1st thing is to see if performance is actually acceptable out of the box. 一旦针对规范化模型正确运行了查询,就可以检查查询计划等。这可能会突出显示您需要拼合一些联接,如果那样的话,您可能必须将数据存储在两个不同的位置格式,一种用于报告/一种用于搜索,等等。但是第一件事是查看性能是否确实可以接受。 Hopefully that helps. 希望有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM