An old system have arrived on our office for some changes and fix, but it is also suffering from performance issues. We don’t know exactly what is the source of this slowness.
While we were refactoring the old code we found several sql queries with the follow pattern (the queries are simplified for example purpose):
SELECT
(
SELECT X
FROM A
WHERE A.id = TABLE.id
) AS COLUMN1,
(
SELECT Y
FROM B
WHERE B.id = TABLE.id
) AS COLUMN1,
(
SELECT Z
FROM C
WHERE C.id = TABLE.id
) AS COLUMN1,
...
FROM
TABLE
WHERE
TABLE.id = @param;
These queries do several internal sub queries from every column they return.
We are planning to rewrite these queries on the follow pattern:
SELECT
A.X, B.Y, C.Z
FROM
TABLE
INNER JOIN A on A.ID = TABLE.ID
INNER JOIN B on B.ID = TABLE.ID
INNER JOIN C on C.ID = TABLE.ID
WHERE
TABLE.id = @param;
With inner joins they are easier to read and understand, but is it really any faster? Is it the better way to write them?
Unfortunately the first one we rewrote didn’t improve the query time, it made the query a bit slower.
Here is my question: should we rewriting all these queries? Are these sub-queries a good way to do this job? Are they faster the the inner-join way?
If I understand your question correctly, you are starting an operation to rewrite some of your SQL statements because you THINK there might be an issue with them.
My advice is to stop and first start to determine where your time is currently being spent.
Only after you have found that it’s in the queries with those scalar subselects AND it’s because of those scalar subselects, you should be rewriting them.
Until then: start tracing and examining.
Here are two threads from OTN that are used to guide people with performance problems:
http://forums.oracle.com/forums/thread.jspa?messageID=1812597
http://forums.oracle.com/forums/thread.jspa?threadID=863295
Regards,
Rob.
And: because of scalar subquery caching, your original query might be a lot faster than a rewritten query using joins.