是否有更好的方法来连接多个表而不是用户'INNER JOIN'

时间:2015-09-28 15:03:19

标签: sql sql-server-2008 join inner-join

我正在编写一个查询来从八个表中获取50列。出于好奇,有没有更好的方法来获取数据,而不是使用表A-H的一系列inner join Table A on Table B.orderNumber = Table A.OrderNumber

运行的数据库是SQL Server 2008。

以下是我仍在写的初始查询:

SELECT
    /*Buyer and Seller information for order number */
   [QCV_BuyerSellers].[OrderNumber] AS OrderNum
  ,[QCV_BuyerSellers].[OrderGuid] AS Order_GUID
  ,[QCV_BuyerSellers].[Buyer1_EntityTypeId] AS ENT_TYPE
  ,[QCV_BuyerSellers].[Buyer1_EntityTypeName] AS ENT_TYPE_NAME
  ,[QCV_BuyerSellers].[Buyer1_FullName] AS FULL_NAME
  ,[QCV_BuyerSellers].[Buyer1_FirstName] AS BF_Name
  ,[QCV_BuyerSellers].[Buyer1_MiddleName] AS BM_Name
  ,[QCV_BuyerSellers].[Buyer1_LastName] AS BL_Name
  ,[QCV_BuyerSellers].[Buyer1_TIN] AS B_Tin1
  ,[QCV_BuyerSellers].[Buyer1_PhoneHome] AS B_PhoneHome
  ,[QCV_BuyerSellers].[Buyer1_PhoneWork] AS B_PhoneWork

  ,[QCV_BuyerSellers].[Buyer2_FullName] AS FULL_NAME2
  ,[QCV_BuyerSellers].[Buyer2_FirstName] AS BF_Name2
  ,[QCV_BuyerSellers].[Buyer2_MiddleName] AS BM_Name2
  ,[QCV_BuyerSellers].[Buyer2_LastName] AS BL_Name2
  ,[QCV_BuyerSellers].[Buyer2_TIN] AS B_Tin2
  ,[QCV_BuyerSellers].[Buyer2_PhoneHome] AS B_PhoneHome2
  ,[QCV_BuyerSellers].[Buyer2_PhoneWork] AS B_PhoneWork2

  ,[QCV_BuyerSellers].[Seller1_FirstName] AS SF_Name
  ,[QCV_BuyerSellers].[Seller1_MiddleName] AS SM_Name
  ,[QCV_BuyerSellers].[Seller1_LastName] AS SL_Name
  ,[QCV_BuyerSellers].[Seller1_TIN] AS S_Tin
  ,[QCV_BuyerSellers].[Seller1_PhoneHome] AS S_PhoneHome
  ,[QCV_BuyerSellers].[Seller1_PhoneWork] AS S_PhoneWork

  ,[QCV_BuyerSellers].[Seller2_FirstName] AS SF_Name2
  ,[QCV_BuyerSellers].[Seller2_MiddleName] AS SM_Name2
  ,[QCV_BuyerSellers].[Seller2_LastName] AS SL_Name2
  ,[QCV_BuyerSellers].[Seller2_TIN] AS S_Tin2
  ,[QCV_BuyerSellers].[Seller2_PhoneHome] AS S_PhoneHome2
  ,[QCV_BuyerSellers].[Seller2_PhoneWork] AS S_PhoneWork2

  /*OMFILE Property table fields by order number */
  ,[OMFILE_PROPERTY].[PropertyAddress1] AS Prop_Adress
  ,[OMFILE_PROPERTY].[PropertyCity] AS Prop_City
  ,[OMFILE_PROPERTY].PropertyCounty AS Prop_County
  ,[OMFILE_PROPERTY].PropertyState AS Prop_State 
  ,[OMFILE_PROPERTY].PropertyZip AS Prop_Zip
  ,[OMFILE_PROPERTY].PropertyBriefLegal1 AS Prop_Brief1
  ,[OMFILE_PROPERTY].PropertyBriefLegal2 AS Prop_Brief2

  ,[OMEXT2_SUBDIVISION].SubdPUDFlag AS SD_PUD_FLAG
  ,[OMEXT2_SUBDIVISION].SubdCondominiumFlag AS SD_Condo_Flag

   /*OMFILE Payoff Fields for order number */
  ,[OMFILE_PAYOFFS].[Payoff1Name] 
  ,[OMFILE_PAYOFFS].[Payoff1LoanNumber] 
  ,[OMFILE_PAYOFFS].[Payoff1Phone] 

  ,[OMFILE_PAYOFFS].[Payoff2Name] 
  ,[OMFILE_PAYOFFS].[Payoff2LoanNumber] 
  ,[OMFILE_PAYOFFS].[Payoff2Phone]

  /*Loan Number & Amount From  OMFILE_LENDERLOAN table */
  ,[OMFILE_LENDERLOAN].[LoanNumber]  
  ,[OMFILE_LENDERLOAN].[LoanAmount] 

 FROM [REO].[dbo].[V_BuyerSellers] 
  INNER JOIN [REO].[dbo].[OMFILE_PROPERTY] 
  on V_BuyerSellers_Flat.OrderNumber = OMFILE_PROPERTY.OrderNumber
  INNER JOIN 
  [REO].[dbo].[OMEXT2_SUBDIVISION]
  on [REO].[OrderNumber] = [OMEXT2_SUBDIVISION].[OrderNumber]
  INNER JOIN [REO].[dbo].[OMFILE_PAYOFFS] 
  on [OMFILE_PROPERTY].[OrderNumber] = [OMFILE_PAYOFFS].[OrderNumber] 
  INNER JOIN [REO].[dbo].[OMFILE_LENDERLOAN] 
  on [OMFILE_PAYOFFS].[OrderNumber] = [OMFILE_LENDERLOAN].[OrderNumber] 

  WHERE [QCV_BuyerSellers].[OrderNumber] = 'QCT-8735410'

2 个答案:

答案 0 :(得分:0)

我建议首先使用“包含实际执行计划”运行查询(Ctrl-M切换此开/关)并查找可能提高性能的缺失索引。您的查询没有任何可以重新考虑的明显缺陷。看起来你已经在使用一个视图,你可以从这个查询中创建一个新的视图并对其进行索引,但是不能保证索引视图的性能优于引用索引表的视图,通常是底层视图的索引桌子仍在使用。

有一些方法可以避免连接多个表的性能损失,但它们有其缺点。您可以在不使用WHERE的情况下从此查询创建表,并将OrderNumber字段编入索引。如果你还没有准备好实现一个合适的数据仓库,这是一条黑暗的道路,因为你可能最终会得到很多一次性的报告表,并且根据环境可能需要更新不合理的间隔。

答案 1 :(得分:-2)

一般来说,JOINS提供比使用旧学校方式更好的性能和可读性,即WHERE上使用PK = FK

我坚持使用JOINS

如果需要考虑性能,建议您使用SQL Profiler进行完整分析。

我还建议您撰写一个视图,以便在复杂查询的SELECT上获得更好的效果。