0

我目前正在尝试使用 mysql 和 openreports 优化报告以用于会计。虽然我确信有更好的工具来完成这项工作,但这些是我目前必须使用的。我正在执行以下计算字段:

客户,订单数量,客户成本,客户调整,调整后的客户价格(客户成本 - 客户调整),供应商成本,供应商调整,调整后供应商价格(供应商成本 - 供应商调整),保证金((客户开单 - 供应商开单)/客户计费),调整保证金((调整客户价格-调整供应商价格)/调整客户)

这些表非常大,我必须为每个内部连接加入 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
;
4

1 回答 1

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
于 2012-09-21T17:44:04.307 回答