多个嵌套查询

时间:2012-09-21 17:19:10

标签: mysql sql report

我目前正在尝试使用mysql和openreports来优化报告以供会计使用。虽然我确信有更好的工具可供使用,但这些都是我现在需要处理的工具。我正在做以下计算字段:

客户端, 订单数量, 客户成本, 客户调整, 调整后的客户价格(客户成本 - 客户调整), 供应商成本, 供应商调整, 调整供应商价格(供应商成本 - 供应商调整), 保证金((客户收费 - 供应商收费)/客户收费), adj margin((adj。客户价格 - 供应商价格)/ adj client)

表非常大,我必须为每个内连接加入4个表。之前的报告中有一个联合,并且在联合的每一侧都有大约20个嵌套选择,所有这些都包含至少四个联接和计数。由于我正在基于先前的字段进行计算,因此每个嵌套选择逐渐变大,包含先前计算中的所有嵌套选择,计数和计算。该查询现在是400行并且非常昂贵并且对系统负担,特别是对于较大的客户端。

我意识到这个工作有更好的工具,但我想知道这种情况的理想解决方案是什么。我可以在查询中创建用户定义的变量并在以后使用它吗?如果每个客户端有多个查询,这会有效吗?我不能在这篇文章中包含整个400行查询,但我可以提供任何有用的其他详细信息。任何见解将不胜感激。

select office_1.name as 'Client'
,count(distinct(property_1.id)) as 'Total Billed Orders',
(select format(coalesce(sum(serviceprice_2.amount),0),2)
    from cap.service_price serviceprice_2
    inner join cap.service service_2 on service_2.id = serviceprice_2.service_id
    inner join cap.service_area servicearea_2 on servicearea_2.id = service_2.service_area_id
    inner join cap.property property_2 on property_2.id = servicearea_2.property_id
    inner join cap.office office_2 on office_2.id = property_2.client_id
    where serviceprice_2.price_context = 'CLIENT'
    and serviceprice_2.price_type_id = 236
    && service_2.date_client_billed between '2012-09-01' and now()
    and service_2.gl_code_ap='4700-70-000'
    and service_2.date_cancelled is null
    and office_2.id = office_1.id) as 'Client Price'
/* other calculations between here */
from cap.service service_1
inner join cap.service_area servicearea_1 on servicearea_1.id = service_1.service_area_id
inner join cap.property property_1 on property_1.id = servicearea_1.property_id
inner join cap.office office_1 on office_1.id = property_1.client_id
inner join cap.office vendor_1 on vendor_1.id = service_1.vendor_id
where 
service_1.date_client_billed between '2012-09-01' and now()
and service_1.date_cancelled is null
and office_1.id = 26377 
and service_1.gl_code_ap='4700-70-000'
group by office_1.id
;

1 个答案:

答案 0 :(得分:0)

使用视图可以简化查询,但不会影响性能。

create view view1 as
select *
from 
    cap.service_price serviceprice_2
    inner join cap.service service_2 on service_2.id = serviceprice_2.service_id
    inner join cap.service_area servicearea_2 on servicearea_2.id = service_2.service_area_id
    inner join cap.property property_2 on property_2.id = servicearea_2.property_id
    inner join cap.office office_2 on office_2.id = property_2.client_id
;
create view view2 as
select *
from 
    cap.service service_1
    inner join cap.service_area servicearea_1 on servicearea_1.id = service_1.service_area_id
    inner join cap.property property_1 on property_1.id = servicearea_1.property_id
    inner join cap.office office_1 on office_1.id = property_1.client_id
    inner join cap.office vendor_1 on vendor_1.id = service_1.vendor_id

现在查询更易于管理:

select @client_price := format(coalesce(sum(serviceprice_2.amount),0),2)
from view1
where serviceprice_2.price_context = 'CLIENT'
    and serviceprice_2.price_type_id = 236
    && service_2.date_client_billed between '2012-09-01' and now()
    and service_2.gl_code_ap='4700-70-000'
    and service_2.date_cancelled is null
    and office_2.id = office_1.id
;
select 
    office_1.name as 'Client'
    ,count(distinct(property_1.id)) as 'Total Billed Orders',
    @client_price
/* other calculations between here */
from view2
where 
    service_1.date_client_billed between '2012-09-01' and now()
    and service_1.date_cancelled is null
    and office_1.id = 26377 
    and service_1.gl_code_ap='4700-70-000'
group by office_1.id