How to limit rows in PostgreSQL update statement

Moj*_*jib 9 postgresql update

My table employees contain more than ten million data. i want to update 10k rows in commission_pct column where commission_pct is null and set zero(0) value.

in oracle I can easily solve this by using rownum.

update employees
set commission_pct=0
where commission_pct is null and rownum<=10000;
Run Code Online (Sandbox Code Playgroud)

but postgresql does not support rownum.

how to solve this in postgresql?

CL.*_*CL. 14

You need to search for the desired rows with a subquery, and use the primary key of the table to relate those rows to the table in the UPDATE statement.

一般来说,rownum可以用row_number()窗口函数替换(参见,例如,在更新语句中使用窗口函数),但对于这种情况,使用更容易limit

UPDATE employees
SET commission_pct = 0
WHERE id IN (SELECT id
             FROM employees
             WHERE commission_pct IS NULL
             LIMIT 10000);
Run Code Online (Sandbox Code Playgroud)


Jas*_*sen 5

如果更新的确切行数并不重要,并且目标只是保持事务持续时间简短,则可以使用随机表达式或涉及某些均匀分布列的表达式来限制更新的行数。

当重复查询时,需要修改这些表达式。

id 的值。

UPDATE employees
SET commission_pct = 0
WHERE commission_pct is null and id < 100000
Run Code Online (Sandbox Code Playgroud)

id 的 mod 100

UPDATE employees
SET commission_pct = 0
WHERE commission_pct is null and id % 100 = 0
Run Code Online (Sandbox Code Playgroud)

随机选择

UPDATE employees
SET commission_pct = 0
WHERE commission_pct is null and random() < 0.01
Run Code Online (Sandbox Code Playgroud)

“生日”

UPDATE employees
SET commission_pct = 0
WHERE commission_pct is null and 
 day_of_birth - date_trunc(year,day_of_birth)::date = 0
Run Code Online (Sandbox Code Playgroud)