What is better? Subqueries or inner joining ten tables?

后端 未结 5 1956
青春惊慌失措
青春惊慌失措 2021-02-12 17:49

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.<

相关标签:
5条回答
  • 2021-02-12 18:04

    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.

    0 讨论(0)
  • 2021-02-12 18:05

    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.

    0 讨论(0)
  • 2021-02-12 18:07

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

    0 讨论(0)
  • 2021-02-12 18:14

    Here inner joining is better. Below are the reasons:

    1- In your main query, you are referring/using the values from the table used in sub query. Join is meant for this. Your ask is - "Get me some values by joining different tables as these can not be obtained from one table".

    Sub query should be used when columns from sub query is not referred in the main query. Like:

    select * from emp where deptno in ( select deptno from dept ); 
    

    Here you are asking- "Get me all employees who works in department number deptno". You are not much concerned about this deptno in dept.

    2- Another reason is readability, that you already mentioned.

    3- Performance-wise you need not worry as optimizer knows what to do.

    For more details, please check here.

    0 讨论(0)
  • 2021-02-12 18:16

    As this answer argues, it should not affect the performance. However, some query optimizers might perform better on JOINs, so you should make some experiments on your system.

    And now for something completely different: JOINing each table to the next one might be more aesthetic than JOINing all with TABLE, and prevents errors whenever the id appears more than once in one of the tables:

    SELECT
        A.X, B.Y, C.Z
    FROM
        TABLE
        INNER JOIN A on A.ID = TABLE.ID
        INNER JOIN B on A.ID = B.ID
        INNER JOIN C on B.ID = C.ID
    WHERE
        TABLE.id = @param;
    
    0 讨论(0)
提交回复
热议问题