1
작동하지만 매우 느린 저장 프로 시저가 있습니다.subselect inPL/pgSQL에서 한 번에 여러 레코드를 업데이트하는 방법
기본적으로 내가하고 싶은 것은 하나의 업데이트 명령으로 하위 쿼리에서 얻은 행 집합을 업데이트하는 것입니다. 다른주의 사항은 명세서에서 업데이트 한 행을 반환하고 싶다는 것입니다.
지금 루프를 사용하여 하나의 행을 가져 와서 업데이트하고 결과를 저장하는 것으로 돌아가며 작동하지만 실제로 느립니다.
제안 사항?
다음은 스키마 작성 문과 함께 현재 작업중인 버전입니다.
CREATE TABLE "queued_message"
(
id bigserial NOT NULL,
body json NOT NULL,
status character varying(50) NOT NULL,
queue character varying(150) NOT NULL,
last_modified timestamp without time zone NOT NULL,
CONSTRAINT id_pkey PRIMARY KEY (id)
);
CREATE TYPE returned_message as (id bigint, body json, status character varying(50) , queue character varying(150), last_modified timestamp without time zone);
CREATE OR REPLACE FUNCTION get_next_notification_message_batch(desiredQueue character varying(150), numberOfItems integer)
RETURNS SETOF returned_message AS $$
DECLARE result returned_message; messageCount integer := 1;
BEGIN
lock table queued_notification_message in exclusive mode;
LOOP
update queued_notification_message
set
status='IN_PROGRESS', last_modified=now()
where
id in (
select
id
from
queued_notification_message
where
status='SUBMITTED' and queue=desiredQueue
limit 1
)
returning * into result;
RETURN NEXT result;
messageCount := messageCount+1;
EXIT WHEN messageCount > numberOfItems;
END LOOP;
END;$$LANGUAGE plpgsql;
이 쓰기 가능한 공통 테이블에 대한 좋은 후보처럼 보인다 표현 (wCTE). 그것은 PL/PgSQL입니다. PostgreSQL은 Oracle 절차 언어 PL/SQL을 지원하지 않습니다. –