我有一个存储过程,它可以工作但速度非常慢。
基本上我想做的是在单个更新命令中更新从子查询中获得的一组行。另一个警告是我想返回我在语句中更新的行。
现在我正在使用循环来获取单行并更新它,使用返回存储结果,这有效但速度很慢。
建议?
这是当前的工作版本以及模式创建语句。
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;