简体   繁体   English

从同一服务器上的单独数据库联接表时的查询性能

[英]Query Performance when Joining table from separate database on same server

I am trying to make a massive query (Probably a bad idea, but I don't know another way to do this) that pulls data from two separate databases that are on the same server. 我正在尝试进行大规模查询(这可能是个坏主意,但我不知道另一种方法),该查询从同一服务器上的两个独立数据库中提取数据。 Here is what I have 这是我所拥有的

    Select Distinct PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment As ItemCode,
     concat(
      max(case when PartQuality.Quality_Seq = '1' Then Quality.DSC end),' ',
      max(Case When PartQuality.Quality_Seq = '2' then Quality.DSC End),' ',
      max(Case When PartQuality.Quality_Seq = '3' Then Quality.DSC End),' ',
      max(case when PartQuality.Quality_Seq = '4' Then Quality.DSC end),' ',
      max(Case When PartQuality.Quality_Seq = '5' then Quality.DSC End),' ',
      max(Case When PartQuality.Quality_Seq = '6' Then Quality.DSC End),' ',
      max(case when PartQuality.Quality_Seq = '7' Then Quality.DSC end),' ',
      max(Case When PartQuality.Quality_Seq = '8' then Quality.DSC End),' ',
      max(Case When PartQuality.Quality_Seq = '9' Then Quality.DSC End)) As ProdDesc,
      InventoryDB.dbo.ITEM.QtyOnHand
       From SelectDB.dbo.PartDetail
        Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment
        Join SelectDB.dbo.PartQuality on PartQuality.Item_ID = PartDetail.Item_ID
        Join SelectDB.dbo.Quality on Quality.QualityCode = PartQuality.QualityCode
        Join SelectDB.dbo.Part on Part.Item_ID = PartDetails.Item_ID
        Join SelectDB.dbo.PartPrefix on PartPrefix.PrefixCode = Part.PrefixCode
        Join SelectDB.dbo.ProductPart on ProductPart.Item_ID = PartDetail.Item_ID
        Join SelectDB.dbo.Product on Product.Product_ID = ProductPart.Product_ID
        Join SelectDB.dbo.ProductMod on PrdouctMod.ProductMod_ID = Product.ProductMod_ID
        Join SelectDB.dbo.MakeModel on MakeModel.MakeModel_ID = Product.MakeModel_ID
        Join SelectDB.dbo.ProductStyle on ProductStyle.Style_ID = ProductStyle.Style_ID                        
        Join SelectDB.dbo.ProductMake on ProductMake.Make_ID = MakeModel.Make_ID
         Where Product.ModelYear = @YEAR
          And ProductMake.Name = @MAKE
          And MakeModel.Name = @MODEL
          And ProductStyle.DSC = @STYLE
          And ProductMod.DSC = @MOD
          And PartPrefix.DSC = @LOC
           Group By PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment, ITEM.QtyOnHand

It currently workings but when I added in the following lines to obtain the QtyOnHand field from the InventoryDB, the query went from completing instantly to taking 20-30+ seconds to run. 它当前可以正常工作,但是当我添加以下各行以从InventoryDB获取QtyOnHand字段时,查询从立即完成变为要花20到30秒钟才能运行。

Select Distinct . . . . InventoryDB.dbo.ITEM.QtyOnHand
From . . . . 
Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment
. . ..
Where . . . . 
Group By . . . . ITEM.QtyOnHand

Since the query runs fine without these lines I figured it must be something with these lines. 由于查询在没有这些行的情况下运行良好,因此我认为这一定与这些行有关。 If I had to guess, it would be the Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment due to having to concatenate those three fields together and then search for matching value, but I'm not sure of anyway around that. 如果我不得不猜测的话,那是Join InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.AttachmentJoin InventoryDB.dbo.ITEM on ITEM.Itemcode = PartDetail.Item_ID+' '+PartDetail.Color+PartDetail.Attachment因为必须将这三个字段连接在一起,然后搜索匹配的值,但是我还是不确定。

You are probably correct that the string concatenation is slowing things down. 您可能是正确的,字符串连接正在减慢速度。

One method is to create a computed column and build an index on it: 一种方法是创建一个计算列并在其上建立索引:

alter table PartDetail add ItemCode as (Item_ID + ' ' + Color + Attachment);

create index idx_PartDetail_ItemCode on PartDetail(ItemCode);

The ItemCode column will then be materialized for the table, along with an index. 然后将为该表实现ItemCode列以及一个索引。 This should greatly speed the processing. 这将大大加快处理速度。

暂无
暂无

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

相关问题 使用单独的 where 子句优化从同一个表查询连接 3 次 - Query Joining from the same table 3 times with separate where clauses optimisation 多次连接到同一张表时的条件连接和查询优化 - Conditional Joining and query optimization when joining to the same table multiple times SQL-查询中来自同一表的列 - SQL - Joining columns from the same table in a Query A SELECT 查询从同一张表中加入数据 - A SELECT Query Joining Data From The Same Table 当使用单独的数据库加入查询时,如何选择一个标识字段使我知道该行来自哪个数据库? - When joining a query using separate databases, how can I select an identifying field that lets me know which database the row came from? join同一张表时,如何将join后的数据放在不同的行而不是不同的列? - When joining the same table, how to put the joined data in separate rows rather than separate columns? 减少基于同一表的联接时内部联接的查询数量 - Reducing number of query for inner join when joining base on same table 从同一个表中加入同一列两次时出现不明确的错误 - ambiguous error when joining same column twice from the same table 连接来自同一个表的两个聚合查询 - SQL Server - Joining two aggregate queries from the same table - SQL Server 提高 sql 查询性能以使用相同的列比较 2 个不同的行而不是加入另一个表 - Increasing sql query performance to compare 2 different rows with using same column than joining another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM