Does the order of tables in straight joins, with no hint directives, affect performance?

£可爱£侵袭症+ 提交于 2021-02-10 05:38:12

问题


All SQL-based RDBMS' (versions up to 10 years old):

Does the order of tables in a straight join query (with no hint directives) make a difference for optimum performance and memory management? I heard that the last join should be the largest table. How does your DB's query optimizer handle this scenario?


回答1:


Just to add more on the subject... YES and NO, depends. That is my answer. It depends on many factors, wich RDBMS you are using (MySQL, MSSQL Server, Oracle, DB2...), type of join, size of tables (aka number of rows), indexes and more.

The previous answers says yes and no and appeal to using hints on the queries. But your question is:

Does the order of tables in a join statement make a difference...

In my opinion, this leaves out the use of query hints as you are forcing the query optimizer to use the order prefered by you.

So, reusing the answer posted on my first comment to your question, @Mark Brackett correctly points out that rearranging your joins (without query hints) does not affect the performance as the query optimizer will still use the most efficient execution plan for the present query. Maybe it will be not the most efficient so you can use hints and force to use the order you want on the joins and therefore modify the execution plan of the query.

More discussion on the subject on the following links: Does order matter in a JOIN clause? Optimize join methods




回答2:


Answer to your question - Yes,Order of the table make difference in join.

You can also let the optimizer know about execution plan.

The ORDERED hint causes Oracle to join tables in the order in which they appear in the FROM clause.

For example, this statement joins table TAB1 to table TAB2 and then joins the result to table TAB3:

 SELECT /*+ ORDERED */ TAB1.COL1, TAB2.COL2, TAB3.COL3
     FROM TAB1, TAB2, TAB3
    WHERE TAB1.COL1 = TAB2.COL1
         AND TAB2.COL1 = TAB3.COL1;

If you omit the ORDERED hint from a SQL statement performing a join, the optimizer chooses the order in which to join the tables. You may want to use the ORDERED hint to specify a join order if you know something about the number of rows selected from each table that the optimizer does not. Such information would allow you to choose an inner and outer table better than the optimizer could.

Usually, if you analyze the tables, the optimizer selects an efficient star plan. You can also use hints to improve the plan. The most precise method is to order the tables in the FROM clause in the order of the keys in the index, with the large table last. Then use the following hints:

/*+ ORDERED USE_NL(FACTS) INDEX(FACTS FACT_CONCAT) */



回答3:


No.

As far as Informix is concerned, anyway. The Optimiser will decide for itself what order to process the tables, and the order they appear in the FROM clause is of no consequence. Unless you choose to override the default behaviour.

You can use the +ORDERED hint to the Query Optimiser to force it to join the tables in the order they are presented in the WHERE clause, ie:

SELECT --+ORDERED 
       x.col1, y.col2, z.col3
  FROM z, y, x
  WHERE ...

forces the Optimiser to scan z, join to y, and join to x, even if that creates an intermediate Cartesian product. So it should be used with care.


NB: This answer was written when the question was only tagged Informix, not multiple RDBMS technologies.



来源:https://stackoverflow.com/questions/11407605/does-the-order-of-tables-in-straight-joins-with-no-hint-directives-affect-perf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!