Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does arrangement/order of tables in FROM clause make any difference in improving performance?

In general, does arrangement or order of tables in the FROM clause make any difference in improving the performance of the query? By arrangement I mean smallest table and largest table.

Any different experience/ideas/opinions/factors are also appreciated.

In my case, we're using PostgreSQL v8.2.3.

like image 545
Gnanam Avatar asked Jan 04 '11 11:01

Gnanam


People also ask

Does the order of the WHERE clause make a difference?

No. The order of columns in the WHERE clause does not matter at all.

Does the order of joins matter for performance?

Basically, join order DOES matter because if we can join two tables that will reduce the number of rows needed to be processed by subsequent steps, then our performance will improve.

Does WHERE clause order matter Postgres?

PostgreSQL doesn't care what order you write things in PostgreSQL doesn't care at all about the order of entries in a WHERE clause, and chooses indexes and execution order based on cost and selectivity estimation alone.

Does table order matter in SQL?

Answer. Generally, no, the order of the tables in the JOIN will not affect the overall results of the query. As long as you specify what columns to select, the results should appear essentially the same, just that the rows will be ordered according to the appearance in the first table.


2 Answers

For inner joins, it should not make any difference- the optimiser will generate plans doing the joins in as many different orders as is possible (up to geqo_threshold tables in the from clause).

Outer joins are not symmetric, so there the ordering in the statement is significant (although the actual execution order is still decided by the server).

like image 181
araqnid Avatar answered Oct 18 '22 05:10

araqnid


Are you using something like "SELECT FROM table1,table2" ? The tables are "implicitly" cross-joined, so I don't think it matters.

The first thing I would do is test two possible queries using EXPLAIN, and see if there is any difference?

like image 29
Nanne Avatar answered Oct 18 '22 05:10

Nanne