在同一查询或触发建议中插入和更新

Insert and update in same query or trigger suggestion

我想知道以这种方式插入和更新是否是正确的方法,我认为缺点是更新中的 payout_date 与插入事务中的 [=25] 不完全相同=],在大数据集中,这可能是个问题...

此功能只是将符合条件的支出放入交易 table,并由 java 应用程序进一步处理。我想同时将 payout_date 放入支出 table 中,这表明它已被移动。

或者我应该使用触发器?但是我不知道如何为这种情况构造它。

这是示例数据集和代码:

https://www.db-fiddle.com/f/kNn3c3F96nqEwHoq9biF81/1

create table payouts 
(payout_id integer,
 customer_id integer,
 update_time timestamptz,
 amount integer,
 is_blocked boolean,
 payout_date timestamptz,
 paid_by integer);

 create table users
 (customer_id integer,
  account_number integer);

  create table transactions
  (transaction_id serial, 
   transaction_type integer,
   transaction_date timestamptz,
   account_number integer,
   reference_id integer,
   amount integer, 
   external_id integer);

    insert into payouts (payout_id, customer_id, update_time, amount, is_blocked)
    values 
    (1,1000, now() - interval '5 hours', 323, FALSE),
     (2,1005, now() - interval '5 hours', 66, FALSE),
      (3,1077, now() - interval '5 hours', -200, FALSE),
       (4,1077, now() - interval '5 hours', 88, TRUE),
        (5,2112, now() - interval '5 hours', 153, FALSE);

       insert into users
       values
       (1000, 2000),
        (1005, 2005),
         (1077,2077),
          (2112,3112),
           (1033,2033);




INSERT INTO transactions
( transaction_type
, transaction_date
, account_number
, reference_id
, amount)

SELECT
    100
  , now()
  , u.account_number
  , p.payout_id
  , p.amount 
  FROM
  payouts p
  JOIN users u ON u.customer_id = p.customer_id
  LEFT JOIN transactions t ON t.reference_id = p.payout_id and t.account_number = u.account_number and t.transaction_type = 100 
  WHERE payout_date is null and p.amount > 0  and is_blocked is FALSE
  AND t.account_number is null 
  AND t.reference_id is null;

  UPDATE payouts p 
  set
    payout_date = now()
  , paid_by = 111 -- variable in function

  FROM  payouts p2 
  JOIN users u ON u.customer_id = p2.customer_id
  LEFT JOIN transactions t ON t.reference_id = p2.payout_id and t.account_number = u.account_number and t.transaction_type = 100
  WHERE p.payout_id = p2.payout_id  
  AND p2.payout_date is null and p2.amount > 0  and p2.is_blocked is FALSE
  AND t.account_number is NOT null 
  AND t.reference_id is NOT null;

我使用 returning

成功创建了一批插入和更新

和data_selected作为 (SELECT 100 , 现在() , u.account_number , p.payout_id , p.amount 从 支出 p 在 u.customer_id = p.customer_id 上加入用户 LEFT JOIN 事务 t ON t.reference_id = p.payout_id and t.account_number = u.account_number and t.transaction_type = 100 WHERE payout_date 为空且 p.amount > 0 且 is_blocked 为 FALSE AND t.account_number 为空 AND t.reference_id 为空; ),

insert_data as (

INSERT INTO transactions
( transaction_type
, transaction_date
, account_number
, reference_id
, amount)

returning reference_id)


update payouts p
set
    payout_date = now()
  , paid_by = adminId

FROM insert_data id

where p.payout_id =  id.reference_id;