gpt4 book ai didi

mysql - LEFT JOIN 性能问题 mysql

转载 作者:行者123 更新时间:2023-11-29 12:42:48 25 4
gpt4 key购买 nike

这个查询工作正常并带来 598 行:

SELECT pdwspend.`Parent Supplier Name` , pdwspend.`Child Supplier ID` , pdwspend.`Cat     Tree - Cate` , sum( pdwspend.`Spend Value in LC` )
FROM pdwspend
WHERE pdwspend.Version = 'FINAL'
AND pdwspend.YearMonth >= '201310'
AND pdwspend.YearMonth <= '201409'
AND pdwspend.`Excludable` != 'Excluded'
AND pdwspend.`Year` = '2014'
AND pdwspend.`BU ID` = 'BU1'
GROUP BY pdwspend.`Parent Supplier Name`

我想携带除 pdwspend.子供应商 ID = scraps.BW 父编号 之外的所有内容。应该有大约 70 个匹配项,因此,查询应该带来大约 528 行。这是我的查询:

SELECT pdwspend.`Parent Supplier Name` , pdwspend.`Child Supplier ID` , pdwspend.`Cat Tree - Cate` , sum( pdwspend.`Spend Value in LC` ) 
FROM pdwspend
LEFT JOIN scrubs ON pdwspend.`Child Supplier ID` = scrubs.`BW Parent Number`
WHERE pdwspend.Version = 'FINAL'
AND pdwspend.YearMonth >= '201310'
AND pdwspend.YearMonth <= '201409'
AND pdwspend.`Excludable` != 'Excluded'
AND pdwspend.`Year` = '2014'
AND pdwspend.`BU ID` = 'BU1'
AND scrubs.`BW Parent Number` IS NULL
GROUP BY pdwspend.`Parent Supplier Name`

但是mySQL运行后就死机了。

请指教。

最佳答案

尝试使用不存在:

SELECT s.`Parent Supplier Name` , s.`Child Supplier ID` , s.`Cat     Tree - Cate` ,
sum(s.`Spend Value in LC` )
FROM pdwspend s
WHERE s.Version = 'FINAL' AND s.YearMonth >= '201310' AND s.YearMonth <= '201409' AND
s.`Excludable` <> 'Excluded' AND s.`Year` = '2014' AND s.`BU ID` = 'BU1' AND
NOT EXISTS (SELECT 1
FROM scrubs sc
WHERE s.`Child Supplier ID` = sc.`BW Parent Number`
)
GROUP BY s.`Parent Supplier Name` ;

为了提高性能,您需要在 scrubs(BW Parent Number) 上建立索引。

关于mysql - LEFT JOIN 性能问题 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25881609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com