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”
Everytime you read an excellent article and don't share it, god kills a kitten!
- 1,397,930 hits
- Brains of the #jOOQ community, we summon ye. Awesome feature idea: buff.ly/1ONn4IR But how to implement it? With annotations? 15 hours ago
- RT @robsmallshire: .@PeterHilton Fluent interfaces (e.g. the LINQ or Java Streams) are also fertile ground for naming practice discussions.… 16 hours ago
Get Back in Control of Your SQL
Top Posts & Pages
- SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT
- 10 More Common Mistakes Java Developers Make when Writing SQL
- Java 8 Friday: 10 Subtle Mistakes When Using the Streams API
- 10 Subtle Best Practices when Coding Java
- Java 8 Friday Goodies: Map Enhancements
- 10 Common Mistakes Java Developers Make when Writing SQL
- Does Java 8 Still Need LINQ? Or is it Better than LINQ?
- Do You Really Understand SQL's GROUP BY and HAVING clauses?
- The 10 Most Popular DB Engines (SQL and NoSQL)
- How JPA 2.1 has become the new EJB 2.0
- Is Your Eclipse Running a Bit Slow? Just Use This Simple Trick! April 21, 2015
- How JPA 2.1 has become the new EJB 2.0 April 15, 2015
- jOOQ Tuesdays: Vlad Mihalcea Gives Deep Insight into SQL and Hibernate April 14, 2015
- It’s the Little Things: The PL/SQL NULL Statement, and why Every Language Should have One April 13, 2015
- How to FlatMap a JDBC ResultSet with Java 8? April 9, 2015
- How to Avoid the Dreaded Dead Lock when Pessimistic Locking – And some Awesome Java 8 Usage! April 7, 2015
- How to Use Java 8 Streams to Swiftly Replace Elements in a List April 2, 2015
- Don’t be Fooled by Generics and Backwards-Compatibility. Use Generic Generic Types April 1, 2015
- Hack up a Simple JDBC ResultSet Cache Using jOOQ’s MockDataProvider March 26, 2015
- jOOQ vs. Hibernate: When to Choose Which March 24, 2015