Do foreign key constraints influence query transformations in Oracle?

前端 未结 2 1210
执念已碎
执念已碎 2020-12-16 01:22

I have a situation like this:

create table a(
  a_id number(38) not null,
  constraint pk_a primary key (id)
);

create table b(
  a_id number(38) not null
)         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 01:37

    Yes, having foreign key constraints in place can improve query performance. There are various transforms that are open to the optimizer when appropriate foreign key constraints exist that are not generally available. For example, if you were to join A and B but only select data from B, the optimizer could eliminate A from the query plan entirely if there was a foreign key constraint in place (this sort of thing comes in very handy when you've got useful views that join in more tables than your current query strictly needs because you don't have to trade the performance costs of the extra joins against the code reuse from using an existing view). They also come in handy when you're doing things like using things like query rewrite to rewrite a query to use a materialized view in a data warehouse/ DSS type system.

    Tom Kyte has a presentation Metadata Matters that talks about how various types of constraints, along with other pieces of metadata, can influence the optimizer.

提交回复
热议问题