Slow query with correlated EXISTS subquery
Hi,
I'm looking for some advice on how to improve the performance of a slow running query:
select ent.rowid as rid, xdoc.summary
FROM XXX_XMLDOCUMENT xdoc,
XXX_ENTITYMAIN ent
WHERE xdoc.id = ent.id AND xdoc.incidentidentifier = ent.incidentidentifier
AND ( contains ( xdoc.content, {SMITH}, 2 ) > 0 OR
EXISTS (SELECT 1 FROM
XXX_MULTIMEDIA xm
WHERE xm.incidentidentifier = xdoc.incidentidentifier and xm.xdoc_id = xdoc.id
AND Contains (xm.DATA, {SMITH}, 3 ) > 0 ));
On our Oracle 11.2.0,1 db this takes around 25 seconds to execute.
After some investigation I believe the issue is related to the EXISTS subquery, I tried re-working the query to avoid this and get much better results - the following executes in around 1 second: