What is better? Subqueries or inner joining ten tables?

后端 未结 5 1953
青春惊慌失措
青春惊慌失措 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: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.

提交回复
热议问题