Mysql 需要查询优化,因为子查询太多,并且子查询依赖于where条件

Mysql 需要查询优化,因为子查询太多,并且子查询依赖于where条件,mysql,Mysql,我正在为生产报告编写代码。 我写了这个问题 SELECT P.*, ( SELECT COUNT(id) AS cnt FROM bales WHERE create_date < '2019-11-01' AND product_id = P.id AND(TYPE = 'bale' OR TYPE = 'bag') ) AS before_prod, ( SELECT COU

我正在为生产报告编写代码。 我写了这个问题

SELECT
    P.*,
    (
    SELECT
        COUNT(id) AS cnt
    FROM
        bales
    WHERE
        create_date < '2019-11-01' AND product_id = P.id AND(TYPE = 'bale' OR TYPE = 'bag')
) AS before_prod,
(
    SELECT
        COUNT(id) AS cnt
    FROM
        bales
    WHERE
        (
            dispatched = '0' OR disp_bunch = '0'
        ) AND dispatch_date < '2019-11-01' AND product_id = P.id AND(TYPE = 'bale' OR TYPE = 'bag')
) AS before_dispatched,
(
    SELECT
        COUNT(id) AS cnt
    FROM
        bales
    WHERE
        create_date BETWEEN '2019-11-01' AND '2019-11-06' AND product_id = P.id AND(TYPE = 'bale' OR TYPE = 'bag')
) AS production,
(
    SELECT
        COUNT(id) AS cnt
    FROM
        bales
    WHERE
        (
            dispatched = '0' OR disp_bunch = '0'
        ) AND dispatch_date BETWEEN '2019-11-01' AND '2019-11-06' AND product_id = P.id AND(TYPE = 'bale' OR TYPE = 'bag')
) AS production_dispatched,
C.name AS category_name
FROM
    products P
INNER JOIN category C ON
    C.id = P.category
此查询正在运行,但由于所有表中的记录太多,因此需要花费太多时间。 此外,我只需要在以下位置记录:在\u prod之前、在\u调度之前、生产、生产\u调度所有这些子查询结果都应大于0

我尝试使用having子句,但它也花费了太多时间

我也尝试过php for loop,*LOGIC:首先,所有产品都比它的for loop产品要好。但速度要慢得多*

如何优化我的查询?

您可以改用join并选择case对符合条件的数据求和

select p.*, t.*
from products p
inner join (
    select t2.id, sum(case when create_date < '2019-11-01' then 1 else 0 end) as before_prod
        , sum(case when (dispatched = '0' or disp_bunch = '0') and  create_date < '2019-11-01' then 1 else 0 end) as before_dispatched
        , sum(case when create_date between '2019-11-01' and '2019-11-06' then 1 else 0 end) as production
        , sum(case when (dispatched = '0' or disp_bunch = '0') and create_date between '2019-11-01' and '2019-11-06' then 1 else 0 end) as production_dispatched
    from bales t1
    inner join product t2 on t2.id= t1.product_id 
    inner join category t3 on t3.id = t2.category
    where t1.TYPE in ('bale', 'bag')
    group by t2.id) t
on t.id = p.id
您可以改为使用join并选择case来对符合条件的数据求和

select p.*, t.*
from products p
inner join (
    select t2.id, sum(case when create_date < '2019-11-01' then 1 else 0 end) as before_prod
        , sum(case when (dispatched = '0' or disp_bunch = '0') and  create_date < '2019-11-01' then 1 else 0 end) as before_dispatched
        , sum(case when create_date between '2019-11-01' and '2019-11-06' then 1 else 0 end) as production
        , sum(case when (dispatched = '0' or disp_bunch = '0') and create_date between '2019-11-01' and '2019-11-06' then 1 else 0 end) as production_dispatched
    from bales t1
    inner join product t2 on t2.id= t1.product_id 
    inner join category t3 on t3.id = t2.category
    where t1.TYPE in ('bale', 'bag')
    group by t2.id) t
on t.id = p.id

根据此查询,已调度的生产和生产单位始终为0。发现两个错误1。在where和中,2。未知列p.id和分组依据p.id。根据此查询,调度的生产和生产始终为0。发现两个错误1。在where和中,2。未知列p.id和按p.id分组。