Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is better? Subqueries or inner joining ten tables?

Tags:

sql

select

oracle

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?

like image 343
Gustavo Cardoso Avatar asked Feb 07 '11 12:02

Gustavo Cardoso


People also ask

Which is better inner join or subquery?

I won't leave you in suspense, between Joins and Subqueries, joins tend to execute faster. In fact, query retrieval time using joins will almost always outperform one that employs a subquery. The reason is that joins mitigate the processing burden on the database by replacing multiple queries with one join query.

What is faster a correlated subquery or an inner join?

"Correlated subqueries" are faster than Normal joins.

Is subquery faster than CTE?

The performance of CTEs and subqueries should, in theory, be the same since both provide the same information to the query optimizer. One difference is that a CTE used more than once could be easily identified and calculated once. The results could then be stored and read multiple times.

Which is the most efficient join in SQL?

TLDR: The most efficient join is also the simplest join, 'Relational Algebra'. If you wish to find out more on all the methods of joins, read further. Relational algebra is the most common way of writing a query and also the most natural way to do so.


3 Answers

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.

like image 82
Rob van Wijk Avatar answered Oct 12 '22 21:10

Rob van Wijk


subquery actually runs once for every row whereas the join happens on indexes.

Use joins for better readability and maintainability as you have already mentioned in your questions.

like image 36
Sachin Shanbhag Avatar answered Oct 12 '22 20:10

Sachin Shanbhag


Joins will give you better performance, but I recommend taking a look at the execution plan whenever "optimising" queries.

like image 34
dogbane Avatar answered Oct 12 '22 21:10

dogbane