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!
- 878,688 hits
- Super excited to announce #jOOQ 3.5.0 with tons of new features! groups.google.com/forum/#!topic/… #DataTypes #StoredProcedures #Informix #DDL #UNION 23 hours ago
- Within 2015, #jOOQ will be more popular than #EclipseLink: buff.ly/1xVgPfU. Next goal: Become more popular than #Hibernate 1 day ago
Get Back in Control of Your SQL
Top Posts & Pages
- Add Some Entropy to Your JVM
- SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT
- 10 Subtle Best Practices when Coding Java
- Use MySQL's Strict Mode on all new Projects!
- 10 Common Mistakes Java Developers Make when Writing SQL
- Java Auto-Unboxing Gotcha. Beware!
- 10 More Common Mistakes Java Developers Make when Writing SQL
- Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT?
- 10 Things You Didn't Know About Java
- The 10 Most Popular DB Engines (SQL and NoSQL)
- Use MySQL’s Strict Mode on all new Projects! November 20, 2014
- jOOQ Tip of the Day: Discover all Primary Keys November 14, 2014
- Don’t Migrate to MariaDB just yet. MySQL is Back! November 12, 2014
- Have You Ever Wondered About the Difference Between NOT NULL and DEFAULT? November 11, 2014
- Don’t Miss out on Awesome SQL Power with FIRST_VALUE(), LAST_VALUE(), LEAD(), and LAG() November 7, 2014
- Painless Access from Java to PL/SQL Procedures with jOOQ November 4, 2014
- 10 Things You Didn’t Know About Java November 3, 2014
- A RESTful JDBC HTTP Server built on top of jOOQ October 31, 2014
- Let’s Stream a Map in Java 8 with jOOλ October 23, 2014
- Stop Claiming that you’re Using a Schemaless Database October 20, 2014