[英]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.Attachment
的Join 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.