Oracle update query very slow - sql

We have a rather simple query that gets stuck:
UPDATE BRI_PRINT_DOCUMENT pd
SET PRINT_STATUS_ID = 6
WHERE pd.PRINT_STATUS_ID = 1
AND pd.DOCUMENT_TYPE_ID IN (
SELECT jd.DOCUMENT_TYPE_ID
FROM BRI_JOBTYPE_DOCUMENTTYPE jd
WHERE jd.JOB_TYPE_ID = 2);
COMMIT;
The following query doesn't help:
UPDATE (SELECT *
from BRI_PRINT_DOCUMENT PD
INNER JOIN BRI_JOBTYPE_DOCUMENTTYPE BJD
on PD.DOCUMENT_TYPE_ID = BJD.DOCUMENT_TYPE_ID
AND JOB_TYPE_ID = 2
AND PRINT_STATUS_ID = 1) joined
SET joined.PRINT_STATUS_ID = 6;
We have problems understanding the problem as the following query is fast:
SELECT * FROM BRI_PRINT_DOCUMENT pd
WHERE PRINT_STATUS_ID = 1
AND pd.DOCUMENT_TYPE_ID IN (
SELECT jd.DOCUMENT_TYPE_ID
FROM BRI_JOBTYPE_DOCUMENTTYPE jd
WHERE jd.JOB_TYPE_ID = 2);
Any idea what causes the problem?
Although me and my colleague had the problem of the hanging query through sql developer / Datagrip, the problem got magically resolved when the db admin executed the exact same query with Toad.
There was no table lock, the db admin didn't receive any alert of problems and we have no idea what caused the problem.
Thanks Oracle :-)
Note: there are only a few thousands of records in our dev database.
Explain plan:

Related

PostgreSQL update not working as expected

With 20+ years of experience with MS Access and SQL Server, I'm not a novice with respect to SQL, but I am new to PostgreSQL and I have encountered an issue that makes me feel like a complete noob. I have a simple UPDATE query in which I want to update the destination table d with data from the source View m:
UPDATE chgman.tc_data
SET reporttime = m.reporttime, endtime = m.endtime,
itismessage = m.itismessage, shortdesc = m.shortdesc,
longdesc = m.longdesc, severity = m.severity,
tc_source = m.tc_source, tc_state = m.tc_state,
ushr_state = m.ushr_state, mainroad = m.mainroad,
start_location = m.start_location, end_location = m.end_location
FROM
chgman.tc_matched_raw AS m
INNER JOIN
chgman.tc_data AS d ON d.tc_id = m.tc_id;
The result of the query is that EVERY row in table d is populated with data from the FIRST row of View m.
I am prepared for the embarrassment - please enlighten me as to what I have done wrong...
The from/update in Postgres works a bit differently from SQL Server/MS Access.
This should do what you want:
UPDATE chgman.tc_data d
SET reporttime = m.reporttime, . . .
FROM chgman.tc_matched_raw m
WHERE d.tc_id = m.tc_id;
You don't repeat the table in the FROM clause -- that is a new reference to the table.

SQL Server : spatial query result are not showing

SELECT Distinct lu.ObjectID
FROM LAND_USE_EVW as lu
WHERE NOT EXISTS (SELECT LAND_USE_EVW.OBJECTID
FROM LAND_USE_EVW, MUNICIPALITIES_EVW
WHERE LAND_USE_EVW.Shape.STCentroid().STIntersects(MUNICIPALITIES_EVW.shape) = 1
)
I was trying to get a result that opposite of below
select LAND_USE_EVW.OBJECTID
from LAND_USE_EVW, MUNICIPALITIES_EVW
where LAND_USE_EVW.Shape.STCentroid().STIntersects(MUNICIPALITIES_EVW.shape) = 1
= 0 didn't work therefore I'm trying to come up with other idea.
But result shows nothing. No errors or warning.
found answer
WHERE NOT EXISTS (SELECT LAND_USE_EVW.OBJECTID
FROM LAND_USE_EVW, MUNICIPALITIES_EVW
removing LAND_USE_EVW from FROM LAND_USE_EVW solve the problem

Updating code in Oracle taking too much time

I am running the following code for 40000 rows
update L02_A_AVOX_DATA
set PWC_Exclusion_Flag = (select PWC_Exclusion_Flag from result
where L02_A_AVOX_DATA.PWC_SEQ_AVOX = result.PWC_SEQ_AVOX);
The code is running for 20 minutes . Dont know whats the error.
Can anyone help me in it
Can you try the following query, maybe that helps:
MERGE INTO L02_A_AVOX_DATA n USING (
SELECT PWC_Exclusion_Flag , PWC_SEQ_AVOX
FROM result
) o ON (n.PWC_SEQ_AVOX = o.PWC_SEQ_AVOX)
WHEN MATCHED THEN
UPDATE SET n.PWC_Exclusion_Flag = o.PWC_Exclusion_Flag;

SQL - updating Oracle 10g database from a complex query

trying to update a single column based on criteria. The rows that need to be updated are returned by the query
SELECT
it.objectid,
it.versionnumber,
it.itemnumber "Item Number",
it.itemtype,
itcovprem.basisofsettlement,
itcovprem.coverage "Coverage"
FROM
itemcoveragesandpremiums itcovprem,
items it,
policies pl
WHERE
pl.objectid = it.parentobjectid AND
pl.policytype in ('H','F') AND
it.objectid = itcovprem.itemobjectid AND
it.itemtype in ('SHOA','SHOB','SHOC','SHOD','SHOK','SHOL') and
it.versionnumber = itcovprem.itemversionnumber AND
((itcovprem.coverage like ('A - Dwg Bldg%')) or
(itcovprem.coverage like ('#42 - Repl Cost Plus%')) or
(itcovprem.coverage like ('#42 - Limited GRC%'))) and
it.effectivedate >= TO_DATE('01-JAN-2006', 'DD-MON-YYYY') and
exists (select * from itemcoveragesandpremiums icp where icp.itemobjectid = it.objectid and icp.itemversionnumber = it.versionnumber and icp.coverage like ('#42%')) and
itcovprem.basisofsettlement not in ('LGRC')
I've tried a few options to convert this to an update query, but no joy.
I get Error - line 3 - SQL command not properly ended when using
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
from itemcoveragesandpremiums as itcovprem
inner join items as it
on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber
inner join policies as pl
on pl.objectid = it.parentobjectid
where [cut, rest of query was below]
I get Error - line 6 - missing right parenthesis when trying use an inline query
update
(
SELECT
itcovprem.basisofsettlement as OLD
FROM
itemcoveragesandpremiums as itcovprem inner join items as it on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber inner join policies AS pl on pl.objectid = it.parentobjectid
WHERE [query snipped]
) t
set t.old = 'LGRC'
Seems that SQL*Plus just wants to stop looking at the update before it gets to the meat of my select query. I'm not sure if I'm formatting it incorrectly or going at it from the wrong direction. Any advice?
In your first attempt, the error at line 3 is because update doesn't support a from or join clause. In your second attempt the immediate error at line 6 is because you're trying to alias a table with as itcovprem; but you can only use as for column aliases, not for table aliases. (The first attempt is trying to do that too, but it isn't getting as far as encountering that problem). But you can't generally update an inline-view with joins anyway, so it would still error with that - something like an ORA-01779.
You would need to do something like:
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where (itemobjectid, itemversionnumber, basisofsettlement, coverage) in (
select it.objectid,
it.versionnumber,
itcovprem.basisofsettlement,
itcovprem.coverage
from ...
)
Assuming that itemobjectid, itemversionnumber, basisofsettlement, coverage identifies a row sufficiently such that you don't risk updating anything you shouldn't. It might be safer to add a rowid to the select and use that for the update instead to avoid ambiguity:
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where rowid in (
select itcovprem.rowid as target_rowid
from ...
)

Updating rows with a sub-query involved Oracle 11g

The script below returns the 01427 error that the single row sub-query returns more than one row. The rownum<2 gets a few rows updated. The obvious solution is looping through with pl/sql, but I am trying to determine if there is a SQL only solution.
UPDATE ldl.clens le
SET master_song_id =
(SELECT cf.song_id#
FROM lt.master_songs cf
WHERE le.lot_id = cf.lot_id
AND song_id#_fk =
(SELECT msc_songs.song_id#
FROM lt.msc_songs
WHERE msc_songs.song_name = le.song_name)
---- AND ROWNUM < 2
);
Any and all help and suggestions deeply appreciated!
MD
I'm not sure I grasped the relation between the tables, but if I did, you can use below UPDATE:
UPDATE ldl.clens le
SET master_song_id =
(SELECT cf.song_id#
FROM lt.master_songs cf
JOIN lt.msc_songs ms ON (cf.song_id#_fk = ms.song_id#)
WHERE
ms.song_name = le.song_name
AND le.lot_id = cf.lot_id)
;
It will work if msc_songs.song_name and master_songs.lot_id will give you a unique master_songs.song_id#.