I have these two tables in my database document and location.
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
i want to update status in location table whose record (left join null values) not available Document. i try below query but it taking more time.
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;
Please help me how to fix it
I would recommend 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
)
For performance, you want an index on document(locid, loccode)
, so the subquery executes fast. An index on location(locid, loccode)
might also help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.