level 1
cheni4ling
楼主
SELECT
pc.Id AS ClassId,
pc.NAME AS ClassName,
vb.username AS RealId,
vb.RealName AS RealName,
ppf.MaterialId AS NaterialId,
ppf.NAME AS NaterialName,
ppf.FileNumber AS FileNumber,
ppm.NAME AS MaterialTypeName,
pi.EnterpriseCode AS EnterpriseCode,
ROUND(IFNULL(SUM(pi.Weight), 0), 2) AS lqWeight,
IFNULL(fc.fccount, 0) AS fcWeight,
IFNULL(rt.RetWeight, 0) AS RetWeight,
ROUND(IFNULL(SUM(pi.Weight), 0) - IFNULL(fc.fccount, 0) - IFNULL(rt.RetWeight, 0), 2) AS jyWeight,
hq.Num AS Num
FROM
ps_inventoryDispatchDetails pidd
INNER JOIN ps_inventoryDispatch pid ON pidd.InventoryDispatchId = pid.Id
LEFT JOIN pb_class pc ON pid.ClassId = pc.Id
LEFT JOIN ps_inventory pi ON pidd.InventoryId = pi.Id
LEFT JOIN (
SELECT InventoryId, SUM(RetWeight) AS RetWeight
FROM ps_inventoryReturn
GROUP BY InventoryId
) rt ON rt.InventoryId = pi.id
LEFT JOIN vben_user vb ON pidd.CreateUserId = vb.Id
LEFT JOIN pb_pd_formula ppf ON ppf.Id = pi.FormulaId
INNER JOIN pb_pd_materialtype ppm ON ppm.CODE = pi.MaterialTypeId AND ppm.NAME='物料'
LEFT JOIN (
SELECT
a.QyCode,
ROUND(SUM(a.Weigh), 2) AS fccount,
a.CreateUserId AS CreateUserId
FROM ql_errorpreventiondetails a
WHERE !a.IsDeleted
AND a.CreateTime >= '2025-08-01'
AND a.CreateTime <= '2025-09-01'
AND a.MaType IN ('1', '2','3','4')
GROUP BY a.QyCode, a.CreateUserId
) fc ON fc.CreateUserId = pidd.CreateUserId AND fc.qycode = pi.EnterpriseCode
LEFT JOIN (
SELECT
b.CreateUserId AS CreateUserId,
sum( b.Num ) AS Num ,
c.ProductId,c.FormulaId
FROM
ql_errorprevention b
JOIN pl_producerequest c on c.id=b.ProduceRequestId
WHERE
! b.IsDeleted
AND b.CreateTime >= '2025-08-01'
AND b.CreateTime <= '2025-09-01'
-- AND b.CreateUserId =426
GROUP BY b.CreateUserId ,c.ProductId,c.FormulaId ) hq ON fc.CreateUserId = hq.CreateUserId
and hq.ProductId=ppf.MaterialId
and hq.FormulaId=ppf.id
WHERE
pid.ReceiveTime >= '2025-08-01'
AND pid.ReceiveTime <= '2025-09-01'
-- AND vb.id = 426
-- AND pc.id= 1
-- AND ppf.MaterialId=282
AND ppm.NAME ='物料'
GROUP BY
vb.username,
pi.EnterpriseCode
ORDER BY
pc.NAME 说明a表目前有百万数据,后续会有千万甚至更多数据,如何才能将1个月查询结果控制在几秒,目前已尝试过程 视图 子查询,索引优化,均无法达到想要的结果
2025年09月22日 08点09分
1
pc.Id AS ClassId,
pc.NAME AS ClassName,
vb.username AS RealId,
vb.RealName AS RealName,
ppf.MaterialId AS NaterialId,
ppf.NAME AS NaterialName,
ppf.FileNumber AS FileNumber,
ppm.NAME AS MaterialTypeName,
pi.EnterpriseCode AS EnterpriseCode,
ROUND(IFNULL(SUM(pi.Weight), 0), 2) AS lqWeight,
IFNULL(fc.fccount, 0) AS fcWeight,
IFNULL(rt.RetWeight, 0) AS RetWeight,
ROUND(IFNULL(SUM(pi.Weight), 0) - IFNULL(fc.fccount, 0) - IFNULL(rt.RetWeight, 0), 2) AS jyWeight,
hq.Num AS Num
FROM
ps_inventoryDispatchDetails pidd
INNER JOIN ps_inventoryDispatch pid ON pidd.InventoryDispatchId = pid.Id
LEFT JOIN pb_class pc ON pid.ClassId = pc.Id
LEFT JOIN ps_inventory pi ON pidd.InventoryId = pi.Id
LEFT JOIN (
SELECT InventoryId, SUM(RetWeight) AS RetWeight
FROM ps_inventoryReturn
GROUP BY InventoryId
) rt ON rt.InventoryId = pi.id
LEFT JOIN vben_user vb ON pidd.CreateUserId = vb.Id
LEFT JOIN pb_pd_formula ppf ON ppf.Id = pi.FormulaId
INNER JOIN pb_pd_materialtype ppm ON ppm.CODE = pi.MaterialTypeId AND ppm.NAME='物料'
LEFT JOIN (
SELECT
a.QyCode,
ROUND(SUM(a.Weigh), 2) AS fccount,
a.CreateUserId AS CreateUserId
FROM ql_errorpreventiondetails a
WHERE !a.IsDeleted
AND a.CreateTime >= '2025-08-01'
AND a.CreateTime <= '2025-09-01'
AND a.MaType IN ('1', '2','3','4')
GROUP BY a.QyCode, a.CreateUserId
) fc ON fc.CreateUserId = pidd.CreateUserId AND fc.qycode = pi.EnterpriseCode
LEFT JOIN (
SELECT
b.CreateUserId AS CreateUserId,
sum( b.Num ) AS Num ,
c.ProductId,c.FormulaId
FROM
ql_errorprevention b
JOIN pl_producerequest c on c.id=b.ProduceRequestId
WHERE
! b.IsDeleted
AND b.CreateTime >= '2025-08-01'
AND b.CreateTime <= '2025-09-01'
-- AND b.CreateUserId =426
GROUP BY b.CreateUserId ,c.ProductId,c.FormulaId ) hq ON fc.CreateUserId = hq.CreateUserId
and hq.ProductId=ppf.MaterialId
and hq.FormulaId=ppf.id
WHERE
pid.ReceiveTime >= '2025-08-01'
AND pid.ReceiveTime <= '2025-09-01'
-- AND vb.id = 426
-- AND pc.id= 1
-- AND ppf.MaterialId=282
AND ppm.NAME ='物料'
GROUP BY
vb.username,
pi.EnterpriseCode
ORDER BY
pc.NAME 说明a表目前有百万数据,后续会有千万甚至更多数据,如何才能将1个月查询结果控制在几秒,目前已尝试过程 视图 子查询,索引优化,均无法达到想要的结果