如何根据后续更新从表中获得前k个结果

时间:2013-11-11 17:10:33

标签: java oracle plsql

我有一张表有近百万条记录(id从1到百万)。从表中我需要获取带有id1的前K(k = 3,4,5,任意数字)行,然后更新其他行(值在一栏中)根据结果。然后再次为id2做前k等等。 table的主键是id,guid。

我用光标编写了这个程序来执行此操作。我从java传递了从1到百万的数字到这个程序,但由于使用了游标,这非常非常慢。有什么替代品吗?

由于

代码段

CREATE OR REPLACE PROCEDURE displayGreedyAd (passedId IN int, k IN int)
IS
CURSOR topK IS
SELECT * FROM table G1 WHERE G1.Id = passedId AND G1.guid IN   
    (SELECT G.guid
    FROM (SELECT *
          FROM table
          WHERE table.somevalue <= table.Balance AND table.Id = passedId        -- this balance is updated below
          ORDER BY table.someothervalue DESC) G
    WHERE ROWNUM <= k)
    FOR UPDATE;

SingleRecord topK%ROWTYPE;  
BEGIN

IF NOT topK%ISOPEN THEN
    OPEN topK;
END IF; 

FETCH topK INTO SingleRecord;  

WHILE topK%FOUND 
LOOP
    IF (SingleRecord.somevalue >= SingleRecord.somevalue2) THEN
        UPDATE table
        SET Balance = Balance - SingleRecord.someothervalue -- this needs to be updated in the table
        WHERE table.guid = SingleRecord.guid; 
    END IF;

    FETCH topK INTO SingleRecord;  
END LOOP; 

IF topK%ISOPEN THEN
    CLOSE topK;
END IF;
END;
/

The table looks something like this

guid id amleft totamt amtspend  priority
1    1   20     20      7       2
1    2   20     20      11      1
1    3   20     20      2       3
2    1   30     30      4       1
2    3   30     30      12      2
2    4   30     30      7       3
..
..

After 1st iteration with k =1 and id =1 and subtracting last column with amountleft

fetch top 1 values for id=1 and lowest  priority and put it in a separate table(guid,id, amountleft,totalamount), after which this table will look like 

guid id amleft totamt amtspend  priority
1    1   13     20      7       2
1    2   13     20      11      1
1    3   13     20      2       3
2    1   26     30      4       1
2    3   26     30      12      2
2    4   26     30      7       3

then fetch for id =2 and so on, amleft gets updated after each fetch to a position where amleft < amtspend.

Thanks

0 个答案:

没有答案