How schema meta data impacts Oracle query transformations
I was recently wondering about some issue I had encountered between two tables. If tables undergo a lot of INSERT / UPDATE / DELETE statements, it may appear to be better to remove some constraints, at least temporarily for the loading of data. In this particular case, the foreign key relationship was permanently absent and I found the join between the two tables to be a potential source of bad query execution plans in greater contexts. So my intuition told me that this could be optimised by adding the constraint again, as Oracle would then be able to formally use that information for query transformations. I asked the question on Stack Overflow and it seems I was right:
But what does that mean? It’s simple. If you have two tables A and B, and you join them on A.ID = B.A_ID, having a foreign key constraint on B.A_ID may make all the difference. Let’s say, you execute this:
select B.* from B join A on A.ID = B.A_ID
Without a foreign key on B.A_ID
When B.A_ID is set (i.e. is not null), there is still no guarantee that there actually exists a corresponding A.ID. A is not part of the projection. Intuitively, it is thus not needed, but it cannot be optimised away, because the query will actually have to check for existing A.ID per B.A_ID.
With a foreign key constraint
When B.A_ID is set, then there must be a corresponding, unique A.ID. Hence, the JOIN can be ignored in this case. This has powerful implications on all sorts of transformation operations.
For more details, have a look at Tom Kyte’s presentation “Meta Data Matters”
That was awesome, right? Share it with your friends!
- 979,882 hits
- RT @mariofusco: 10 things your static language can’t do melix.github.io/blog/2014/12/1… << finally a clear explanation of dynamic langs pros. Thanks… 13 hours ago
- RT @DZone: The Inconvenient Truth About Dynamic vs. Static Typing by @lukaseder bit.ly/1wt10OI 2 days ago
Get Back in Control of Your SQL
Top Posts & Pages
- 10 Common Mistakes Java Developers Make when Writing SQL
- SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT
- 10 Subtle Best Practices when Coding Java
- The 10 Most Popular DB Engines (SQL and NoSQL)
- Does Java 8 Still Need LINQ? Or is it Better than LINQ?
- 10 More Common Mistakes Java Developers Make when Writing SQL
- Do You Really Understand SQL's GROUP BY and HAVING clauses?
- Java 8 Friday: 10 Subtle Mistakes When Using the Streams API
- The Inconvenient Truth About Dynamic vs. Static Typing
- Java 8 Friday: No More Need for ORMs
- A True SQL Gem You Didn’t Know Yet: The EVERY() Aggregate Function December 18, 2014
- jOOQ Tuesdays: Yalım Gerger brings Git to Oracle December 16, 2014
- Really Too Bad that Java 8 Doesn’t Have Iterable.stream() December 15, 2014
- The Inconvenient Truth About Dynamic vs. Static Typing December 11, 2014
- Don’t be “Clever”: The Double Curly Braces Anti Pattern December 8, 2014
- Do You Really Understand SQL’s GROUP BY and HAVING clauses? December 4, 2014
- How to Integrate Commercial Third-Party Artefacts into Your Maven Build December 2, 2014
- jOOQ Newsletter: November 28, 2014 – Black Friday jOOQ Sale – Only Today! November 28, 2014
- Use MySQL’s Strict Mode on all new Projects! November 20, 2014
- jOOQ Tip of the Day: Discover all Primary Keys November 14, 2014