Quantcast
Channel: Planet PostgreSQL
Viewing all articles
Browse latest Browse all 9650

Andrew Dunstan: Left right out

$
0
0
In today's lesson, we cover the abuse of outer joins. These are incredibly useful, and often misunderstood. They also have the potential to affect the chosen execution plan quite dramatically. A query I was given today was running around 12s in the worst case. After some analysis of the horribly tortuous logic, I determined that one of the many left outer joins the query performed simply wasn't necessary, since the where clause logic effectively filtered out the rows where the right table data was missing. The join collected 1m rows from the right table, sorted them and then filtered them. When I turned this into an inner join the where condition was suddenly pushed down, only a few thousand rows were selected from the right table, and the query ran miraculously in 0.5s. All by the elimination of the two words "left outer".

Moral: outer joins are not free, and can in fact be quite expensive. Only use them when necessary.

Viewing all articles
Browse latest Browse all 9650

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>