查询优化:在一个SQL语句中基于SELECT循环和更新多行

时间:2014-02-28 01:36:46

标签: postgresql

CREATE TABLE account (
    account_id SERIAL PRIMARY KEY,
)

CREATE TABLE transaction (
    transaction_value integer,
    business_period_id FOREIGN_KEY,
    account_id FOREIGN KEY  
)

CREATE TABLE business_period (
    business_period_id SERIAL PRIMARY KEY
)

CREATE TABLE account_balance (
    account_id FOREIGN KEY,
    account_balance integer 
)

创建业务周期以及该业务期间的事务。我需要一种方法来根据期间内发生的事务更新account_balance。

我目前正在做这个

BEGIN
SELECT account_id, SUM(transaction_value) AS net_impact 
FROM transactions 
WHERE business_period_id=$1 
GROUP BY account_id;
-- at this point this query returns to the client side

假设这会返回一个名为account_impacts的数组,它是:

var account_impacts = [[account_id, net_impact], [account_id, net_impact], [account_id, net_impact], ...]

在客户端执行单独的更新语句(伪代码):

for (var account_impact in account_impacts) {
    var $account_id = account_impact[0]
    var $net_impact = account_impact[1]

    // Execute the follow SQL query
    UPDATE account_balance 
    SET account_balance = account_balance + $net_impact
    WHERE account_id = $account_id
}
COMMIT

此查询速度很慢,并且开始影响性能。 我在客户端循环,然后单独执行更新查询。有没有办法在SQL中将所有这些作为单个查询执行。

1 个答案:

答案 0 :(得分:0)

这可以通过一个查询来完成:

UPDATE  account_balance ab
SET     account_balance = account_balance + t.transaction_value
FROM (
    SELECT
        account_id
    ,   sum ( transaction_value ) as transaction_value
    FROM    transaction t
    WHERE   business_period_id = 1
    GROUP BY account_id
) t
WHERE   ab.account_id = t.account_id;