如何优化我们使用事务平板的查询?
How to optimize query where we are using transaction slab?
我需要根据交易平板生成账龄报告,即以 2、5、10 等为间隔。用户将 select 报告开始日期和结束日期,然后我们需要为该特定日期范围生成报告。报告的形式应该是将事务分成多个块;例如 0-2、3-4、5-6、6-8 和大于 8 天。我为每个 slab 写了一个子查询,它给了我所需的数据。但是由于数据每天都在增加,因此需要花费太多时间。我也试过索引,但它并没有提高性能。
select id_ledger as ledger_id,
customer_id,
title as ledger,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab1,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2 * 2))
and (('2021-10-23' - transaction_date) > (2 * 1))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab2,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2 * 3))
and (('2021-10-23' - transaction_date) > (2 * 2))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab3,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2 * 4))
and (('2021-10-23' - transaction_date) > (2 * 3))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab4,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) > (2 * 4))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab5,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and 1 = 1
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as balance
from ledgers l;
注意:
2021-10-1 ==> 开始日期
2021-10-24 ==> 结束日期
2021-10-23 ==> 报告日期
并且 2 被 select 编辑为板编号。
如何提高此查询的性能?
提前致谢。
你可以试试这个,应该会更快:
select id_ledger as ledger_id, customer_id, title as ledger,
, COALESCE( sum(dr_amount) FILTER (WHERE ('2021-10-23' - transaction_date) <= (2)) OVER ()
- sum(clearance_amount) FILTER (WHERE ('2021-10-23' - transaction_date) <= (2)) OVER ()
, 0
) as slab1
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 2) and ('2021-10-23' - transaction_date) > (2 * 1)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 2) and ('2021-10-23' - transaction_date) > (2 * 1)) OVER ()
, 0
) as slab2
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 3) and ('2021-10-23' - transaction_date) > (2 * 2)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 3) and ('2021-10-23' - transaction_date) > (2 * 2)) OVER ()
, 0
) as slab3
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 4) and ('2021-10-23' - transaction_date) > (2 * 3)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 4) and ('2021-10-23' - transaction_date) > (2 * 3)) OVER ()
, 0
) as slab4
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) > (2 * 4)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) > (2 * 4)) OVER ()
, 0
) as slab5
, COALESCE(sum(dr_amount) - sum(clearance_amount), 0) AS balance
from ledgers l
inner join journal_voucher_details j
on j.ledger_id = l.customer_id
where cr_amount = 0
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by j.ledger_id
我需要根据交易平板生成账龄报告,即以 2、5、10 等为间隔。用户将 select 报告开始日期和结束日期,然后我们需要为该特定日期范围生成报告。报告的形式应该是将事务分成多个块;例如 0-2、3-4、5-6、6-8 和大于 8 天。我为每个 slab 写了一个子查询,它给了我所需的数据。但是由于数据每天都在增加,因此需要花费太多时间。我也试过索引,但它并没有提高性能。
select id_ledger as ledger_id,
customer_id,
title as ledger,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab1,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2 * 2))
and (('2021-10-23' - transaction_date) > (2 * 1))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab2,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2 * 3))
and (('2021-10-23' - transaction_date) > (2 * 2))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab3,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) <= (2 * 4))
and (('2021-10-23' - transaction_date) > (2 * 3))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab4,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and (('2021-10-23' - transaction_date) > (2 * 4))
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as slab5,
(
select COALESCE((sum(dr_amount) - sum(clearance_amount)), 0) as diff_value
from journal_voucher_details
where cr_amount = 0
and 1 = 1
and ledger_id = l.customer_id
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by ledger_id
) as balance
from ledgers l;
注意: 2021-10-1 ==> 开始日期
2021-10-24 ==> 结束日期
2021-10-23 ==> 报告日期
并且 2 被 select 编辑为板编号。
如何提高此查询的性能?
提前致谢。
你可以试试这个,应该会更快:
select id_ledger as ledger_id, customer_id, title as ledger,
, COALESCE( sum(dr_amount) FILTER (WHERE ('2021-10-23' - transaction_date) <= (2)) OVER ()
- sum(clearance_amount) FILTER (WHERE ('2021-10-23' - transaction_date) <= (2)) OVER ()
, 0
) as slab1
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 2) and ('2021-10-23' - transaction_date) > (2 * 1)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 2) and ('2021-10-23' - transaction_date) > (2 * 1)) OVER ()
, 0
) as slab2
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 3) and ('2021-10-23' - transaction_date) > (2 * 2)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 3) and ('2021-10-23' - transaction_date) > (2 * 2)) OVER ()
, 0
) as slab3
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 4) and ('2021-10-23' - transaction_date) > (2 * 3)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) <= (2 * 4) and ('2021-10-23' - transaction_date) > (2 * 3)) OVER ()
, 0
) as slab4
, COALESCE(sum(dr_amount) FILTER (where ('2021-10-23' - transaction_date) > (2 * 4)) OVER ()
- sum(clearance_amount) FILTER (where ('2021-10-23' - transaction_date) > (2 * 4)) OVER ()
, 0
) as slab5
, COALESCE(sum(dr_amount) - sum(clearance_amount), 0) AS balance
from ledgers l
inner join journal_voucher_details j
on j.ledger_id = l.customer_id
where cr_amount = 0
and transaction_date >= '2021-10-01'
and transaction_date <= '2021-10-24'
group by j.ledger_id