我有两个表,t1,t2在架构中 - ' Schema, t1有100万行,大约有30列。
我正在运行如下所示的查询,
select b.Contract_ID,
b.Signed,
b.Funding_Source,
b.Theater, b.Country,
b.Partner, b.BEID,
b.Contract_Type, b.TCV,
b.Technology,
b.Partner_Signature_Date,
b.Signature_Date, b.Term,
b.Purchase_Order,
b.Contract_Baseline,
b.Organic_Rate, b.Target_Organic,
b.Target_Incremental,
b.Total_Bookings_Target,
b.Tracking_Start,
b.Tracking_End, b.Qualifier_BEID,
b.Qualifier_BEGeoID,
a.`CSC Global Ultimate ID`,
a.`CSC Company Target ID`,
b.Qualifier_Theater,
b.Qualifier_Country, a.SCMS,
b.Qualifier_POType, b.MCO,
b.Contract_Status, b.Accrual_Start,
b.Accrual_End, b.Booking_Start,
b.Booking_End, b.TMS_Level_1,
b.TMS_Level_2, b.TMS_Level_3,
b.Pf,
b.Allocation,a.`Fiscal Period ID`,
a.Bookings*b.Allocation as Bookings
from
(SELECT * FROM `Schema`.`t1` where `BEID` in
(select distinct (BEID) from Schema.`t2`)) a
INNER JOIN
(SELECT * FROM `Schema`.`t2`) b
where
a.`Business Entity Id` = b.Qualifier_BEID
and a.`BE Geo Id` = b.Qualifier_BEGeoID
and a.`Country` = b.Qualifier_Country
and a.`PO Type` = b.Qualifier_POType
and a.`Product Family` = b.Pf
这是查询,但需要45分钟才能运行。关于一些事情的一点背景。
select distinct (BEID) from Schema.
t2 产生137个数值(最多4个数字)。
答案 0 :(得分:1)
首先,写下这样的查询
select b.Contract_ID,
b.Signed,
b.Funding_Source,
b.Theater, b.Country,
b.Partner, b.BEID,
b.Contract_Type, b.TCV,
b.Technology,
b.Partner_Signature_Date,
b.Signature_Date, b.Term,
b.Purchase_Order,
b.Contract_Baseline,
b.Organic_Rate, b.Target_Organic,
b.Target_Incremental,
b.Total_Bookings_Target,
b.Tracking_Start,
b.Tracking_End, b.Qualifier_BEID,
b.Qualifier_BEGeoID,
a.`CSC Global Ultimate ID`,
a.`CSC Company Target ID`,
b.Qualifier_Theater,
b.Qualifier_Country, a.SCMS,
b.Qualifier_POType, b.MCO,
b.Contract_Status, b.Accrual_Start,
b.Accrual_End, b.Booking_Start,
b.Booking_End, b.TMS_Level_1,
b.TMS_Level_2, b.TMS_Level_3,
b.Pf,
b.Allocation,a.`Fiscal Period ID`,
a.Bookings*b.Allocation as Bookings
from
`Schema`.`t1` a
INNER JOIN
`Schema`.`t2` b on a.BEID = b.BEID
where
a.`Business Entity Id` = b.Qualifier_BEID
and a.`BE Geo Id` = b.Qualifier_BEGeoID
and a.`Country` = b.Qualifier_Country
and a.`PO Type` = b.Qualifier_POType
and a.`Product Family` = b.Pf
第二,确保您在Schema
。t1
上创建了一个索引。并且与schema.t2一样
答案 1 :(得分:1)
使其成为没有子查询的标准连接,并将所有条件移至连接条件。仅当BEID在t2中多次出现时才使用distinct
。
select distinct
-- various columns as per your posted query
from t1 a
join t2 b on a.BEID = b.BEID
and a.`Business Entity Id` = b.Qualifier_BEID
and a.`BE Geo Id` = b.Qualifier_BEGeoID
and a.`Country` = b.Qualifier_Country
and a.`PO Type` = b.Qualifier_POType
and a.`Product Family` = b.Pf
将索引放在t2(BEID)
上,或者更好地放在t2(BEID, Qualifier_BEID, Qualifier_BEGeoID)
我省略了这些列,因为它们对问题并不重要。