Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用派生表优化查询?_Mysql - Fatal编程技术网

Mysql 如何使用派生表优化查询?

Mysql 如何使用派生表优化查询?,mysql,Mysql,下面的查询是存储过程的一部分。我想,如果我能找出如何修复下面的查询,我就可以将这些修复应用到存储过程的其余部分 set @startDate = '2020-12-01'; set @endDate = '2020-12-31'; select cast(x.Year as char), cast(x.Original as char), cast(x.Beginning as char), cast(x.Additions as char),

下面的查询是存储过程的一部分。我想,如果我能找出如何修复下面的查询,我就可以将这些修复应用到存储过程的其余部分

set @startDate = '2020-12-01';
set @endDate = '2020-12-31';

select  cast(x.Year as char),
        cast(x.Original as char),
        cast(x.Beginning as char),
        cast(x.Additions as char),
        cast(x.Collections as char),
        cast(x.Ending as char) 
from(   select  b.Year,     
                format(case when d.Year is not null then d.County when org.year is not null then ifnull(org.amt,0) else 0 end,2) Original ,
                format(case when b.year = (select collectoryear -1 from Entity) then 0 else beg.amt end,2) Beginning,
                format(case when b.year >= (select collectoryear -1 from Entity) then ifnull(org.amt,0) else 0 end,2) Additions, 
                format(case when b.year = (select collectoryear -1 from Entity) then 0 else beg.amt end + case when b.year >= (select collectoryear -1 from Entity) then ifnull(org.amt,0) else 0 end - ending.amt ,2) Collections,
                format(ending.amt,2) Ending
        from Bill b 
        left join DOriginal d on d.year = b.year 
        join (select b.year, sum(bli.amount) amt from Bill b join BillItem bli on bli.bill_id = b.id where  bli.payment = 0 and bli.refund = 0 and bli.adjust = 0 and bli.type = 5 group by b.year ) org on org.year = b.year
        join (select b.year, sum(bli.amount) amt from Bill b join BillItem bli on bli.bill_id = b.id where dateApplied < @startDate and bli.type = 5 group by b.year ) beg on beg.year = b.year
        join (select b.year, sum(bli.amount) amt from Bill b join BillItem bli on bli.bill_id = b.id where dateApplied <= concat(@endDate,' 24:59:59') and bli.type = 5 group by b.year ) ending on ending.year = b.year
        where b.year > (select collectorYear -11 from Entity) 
        and b.year < (select collectorYear from Entity) 
        group by b.year
        order by Year desc 
        ) x;
编辑:正在进行中

select b.year, d.Year dYear, d.County, e.collectoryear, sum(bli.amount) amt
from
Bill b 
join Entity e
join BillItem bli on bli.bill_id = b.id
left join DOriginal d on d.year = b.year 
where b.year > (e.collectoryear -11) ```

对于初学者,将到达Entity.collectoryear的6个子查询替换为JOIN以获得一次

只加入bill和billitem一次,然后使用CASE将数据拆分为3个选项

你用的是什么版本?在过去,优化器似乎没有对@variables进行任何合理的优化。一个简单的替代方法是使用连接,例如

然后改变


不要使用子查询,立即强制转换。子查询本身看起来像是多余的。。。我建议您尝试从头开始重新创建它,而不使用子查询。您将如何更改它以避免子查询?我建议您创建新问题。描述任务本身,而不是你试图解决它,创建示例小提琴,显示它所需的输出,添加详细的解释。你是对的,但问题是我几乎不知道自己在做什么。我正在尝试处理超出我自己能力范围的查询。我使用的是v6.3.10,我用一个连接替换了6.Entity.collectyear,这很有帮助。当你建议我写3个案例陈述时,你能再解释一下吗?我真的很感激我编辑了我的帖子来展示我目前所拥有的。我想我需要where子句中的强制转换,但是原始版本中的“if not null”让我很反感。6.3.10不是MySQL版本。选择什么@版本;给你什么?@EthanGraybeal-我又加了一个提示。
select b.year, d.Year dYear, d.County, e.collectoryear, sum(bli.amount) amt
from
Bill b 
join Entity e
join BillItem bli on bli.bill_id = b.id
left join DOriginal d on d.year = b.year 
where b.year > (e.collectoryear -11) ```
 JOIN ( SELECT '2020-12-01' AS startDate,
               '2020-12-01' + INTERVAL 1 MONTH AS endDate ) AS dates
            where  b.year > 
            (
                SELECT  collectorYear -11
                    from  Entity
            )
              and  b.year < 
            (
                SELECT  collectorYear
                    from  Entity
            )
JOIN ( SELECT collectorYear FROM Entity ) AS e
WHERE b.year > e.collectorYear - 11
  AND b.year < e.collectorYear