在具有重复查询联接的联合查询中,在每个查询中进行视图或重复联接是更好的选择

时间:2019-02-25 22:42:10

标签: sql sql-server

我有一个联合查询,它将几个选择查询联合在一起。每个选择查询都在具有地址信息的实体上。因此,每个选择语句具有相同的内部联接以获取地址,状态等。我的问题是,在每个select语句中重复这些联接是否更有效?还是我应该先创建一个视图/临时表,然后在每个select查询中对其进行引用?这就是我现在拥有的,它正在重复每个地址信息的联接。

Select 'Individual' As OwnerType, Horizon.Id as HorizonId, Ownership.Id As OwnershipId, Ownership.OwnershipPercentage, (PersonDetails.FirstName + ' ' + PersonDetails.LastName) As Name, Address.AddressLine1, Address.AddressLine2, Address.City, Address.ZipCode, State.Abbreviation as State
        From Horizon
        Inner Join Ownership On Horizon.Id = Ownership.HorizonId
        Inner Join OwnerXRef On Ownership.Id = OwnerXRef.OwnershipId
        Inner Join OwnerIndividual On OwnerXRef.IndividualId = OwnerIndividual.Id
        Inner Join PersonDetails On OwnerIndividual.IndividualPersonDetailsId = PersonDetails.Id
        Inner Join PersonDetailsAddressXRef On PersonDetails.Id = PersonDetailsAddressXRef.PersonDetailsId
        Inner Join Address On PersonDetailsAddressXRef.AddressId = Address.Id
        Inner Join State On Address.StateId = State.Id

    UNION All

    Select 'Business' As OwnerType, Horizon.Id as HorizonId, Ownership.Id As OwnershipId, Ownership.OwnershipPercentage, OwnerBusinessEntity.Name As Name, Address.AddressLine1, Address.AddressLine2, Address.City, Address.ZipCode, State.Abbreviation as State
        From Horizon
        Inner Join Ownership On Horizon.Id = Ownership.HorizonId
        Inner Join OwnerXRef On Ownership.Id = OwnerXRef.OwnershipId
        Inner Join OwnerBusinessEntity On OwnerXRef.BusinessEntityId = OwnerBusinessEntity.Id
        Inner Join Address On OwnerBusinessEntity.AddressId = Address.Id
        Inner Join State On Address.StateId = State.Id

    UNION All

    Select 'Gov-BLM' As OwnerType, Horizon.Id as HorizonId, Ownership.Id As OwnershipId, Ownership.OwnershipPercentage, OwnerBureauOfLandManagement.RegionalOffice As Name, Address.AddressLine1, Address.AddressLine2, Address.City, Address.ZipCode, State.Abbreviation as State
        From Horizon
        Inner Join Ownership On Horizon.Id = Ownership.HorizonId
        Inner Join OwnerXRef On Ownership.Id = OwnerXRef.OwnershipId
        Inner Join OwnerBureauOfLandManagement On OwnerXRef.BureauOfLandManagementId = OwnerBureauOfLandManagement.Id
        Inner Join Address On OwnerBureauOfLandManagement.AddressId = Address.Id
        Inner Join State On Address.StateId = State.Id

    UNION All

    Select 'Trust' As OwnerType, Horizon.Id as HorizonId, Ownership.Id As OwnershipId, Ownership.OwnershipPercentage, OwnerTrust.TrustName As Name, Address.AddressLine1, Address.AddressLine2, Address.City, Address.ZipCode, State.Abbreviation as State
        From Horizon
        Inner Join Ownership On Horizon.Id = Ownership.HorizonId
        Inner Join OwnerXRef On Ownership.Id = OwnerXRef.OwnershipId
        Inner Join OwnerTrust On OwnerXRef.TrustId = OwnerTrust.Id
        Inner Join Address On OwnerTrust.TrustAddressId = Address.Id
        Inner Join State On Address.StateId = State.Id

1 个答案:

答案 0 :(得分:1)

  

我的问题是,在每个位置重复这些连接是否更有效   选择语句,还是我应该先创建视图/临时表   在我的每个选择查询中都引用该信息。

使用view不会改变性能,因为view将在查询中扩展,并且内部将由服务器使用相同的查询。

使用temp table可能会有所帮助,但也可能会使查询变慢,这取决于您的数据分布。如果所有查询都使用查询重复部分中的相同数据子集,则可以提高性能,因为仅提取一次重复数据,但是如果重复部分为所有4个查询提取了不同的数据,则查询将变为慢点。我将尝试使用以下示例进行说明:

假设您有一个数据库,该数据库存储了过去10年中整个国家的离心式邮局的数据。这是海量数据。您只需要提取有关几个发件人的数据。 4个部门希望提取上个月其工作所发送的所有邮件。因此,您有4个部门的4个表,每个部门的人员以及每个部门的其他详细信息,因此您需要编写4个不同的查询,但它们仅在有关特定部门的某些列中有所不同,其余所有部分都是相同的,重复部分具有发送者,接收者,文档,选项之间的一些联接。如果您首先加入这些表并将结果保存到临时表中,那么如果不是Tb,将需要Gb,并且需要花很多时间才能完成,但是如果您进行4个不同的查询,则由于部门较小,每个查询将仅返回一些行。

为了了解什么是最好的,您应该了解您的数据。如果每个查询在重复部分中选择不同的行,并且您具有适当的索引,则最好使用4个不同的查询。相反,如果每个重复部分都提取几乎相同的数据,则最好将其提取一次,保存到临时表中并在每个查询中使用