update postgres中左关节表的空值

ewm0tg9j  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(176)

我的数据库文档和位置中有这两个表。

select * from  Document     

id  locid   loccode
1   1010    30
2   2020    30
3   3030    30
4   4040    40

select * from location

locid   loccode date    Status
1010    30  20-10-2019  A
2020    30  20-10-2019  A
3030    40  20-10-2019  A
4040    40  20-10-2019  A
6060    30  20-10-2019  A
7070    40  20-10-2019  A
8080    30  20-10-2019  D
9090    40  20-10-2019  D

我想更新位置表中的状态,该位置表的记录(左连接空值)在文档中不可用。我试着在下面的问题,但它需要更多的时间。

update location 
set status='D' 
from location A 
left join document B on A.locid=B.locid and A.loccode=B.loccode  
where b.id is NULL;

请帮我修一下

wljmcqd8

wljmcqd81#

我建议你 not exists :

update location 
set status = 'D' 
where not exists (
    select 1 
    from document d
    where d.locid = location.locid and d.loccode = location.loccode
)

为了提高性能,您需要一个索引 document(locid, loccode) ,因此子查询执行速度很快。索引 location(locid, loccode) 可能也有帮助。

相关问题