ほぼ100万件のレコード(1から100万のID)を持つテーブルがあります。テーブルから、id1で上位K(k = 3,4,5任意の数)行を取得し、他の行(1列の値)を更新する必要があります) 結果に基づきます。次に、id2 の上位 k に対しても同様に実行します。テーブルの主キーは id、guid です。
これを行うために、カーソルを使用してこの手順を記述しました。Java からこのプロシージャに 1 から 100 万までの数値を渡していますが、カーソルを使用しているため非常に低速です。それに代わるものはありますか?
ありがとう
コードスニペット
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