我有一个包含近一百万条记录的表(id 从 1 到百万)。从表中我需要使用 id1 获取前 K(k=3,4,5 任意数量)行,然后更新其他行(一列中的值) 基于结果。然后再次为 id2 的 top k 执行此操作,依此类推。表的主键是 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