Foreign Key missing Index
Carlos Oliveira / July 14, 2014
AgendaForeign Key missing index
Introduction Quote Overview of Integrity Constraints Data Model POC – Tables and Foreign Key POC Trace File Analysis Conclusion Training & Reference Apps Svcs Legacy – Oracle PL/SQL Entry Level Training & Reference Thank You
IntroductionI am a forward-looking Information Systems Architect with a solid Oracle DBA background comprising the daily infrastructure tasks of the DBA, several projects as a Data Modeler, and performance management projects.
I Started on the mainframe business, and soon had a deep dive in application development for Oracle databases. After acquiring an Oracle certification, I worked on performance enhancement for applications using Oracle databases, and later worked several years as an infrastructure DBA, later I worked on data modeling projects and more recently a performance management project, on both application and database layers.
“Grammar, which knows how to control even kings.”
Molière
You can define integrity constraints to enforce business rules on data in your tables. Business rules specify conditions and relationships that
must always be true, or must always be false. Because each company defines its own policies about
things like salaries, employee numbers, inventory tracking, and so on, you can specify a different set of rules for each database table.
When an integrity constraint applies to a table, all data in the table must conform to the corresponding rule. When you issue a SQL statement that modifies data in
the table, Oracle ensures that the new data satisfies the integrity constraint, without the need to do any checking within your program.
Overview of Integrity Constraints
Data Model 1
POC – Tables and Foreign KeyDROP TABLE test_fact;DROP TABLE test_extension1;
CREATE TABLE test_fact(fact0 NUMBER(10), fact1 NUMBER(10));ALTER TABLE test_fact ADD ( CONSTRAINT PKSN_test_fact PRIMARY KEY (fact0));
CREATE TABLE test_extension1(fact1 NUMBER(10), description1 VARCHAR2(2000));ALTER TABLE test_extension1 ADD ( CONSTRAINT PKSN_test_extension1 PRIMARY KEY (fact1));
ALTER TABLE test_fact ADD ( CONSTRAINT FK_test_fact_fact1 FOREIGN KEY (fact1) REFERENCES test_extension1 (fact1));
POC 1-- Table test_extension1 was populated with 20.000 rows.-- 10.000 in range 1..10.000 and another 10.000 in range 10.001..20.000-- Table test_fact was populated with 10.000 rows, the values in fact1 column range from 1..10.000
> DELETE FROM test_extension1 WHERE fact1 > 10000;
10000 rows deleted.
Elapsed: 00:00:20.25
> UPDATE test_extension1 2 SET fact1 = fact1 + 10000 3 WHERE fact1 > 10000 and fact1 <= 20000;
10000 rows updated.
Elapsed: 00:00:21.27
Data Model 2
POC 2-- Table test_extension1 was populated with 20.000 rows.-- 10.000 in range 1..10.000 and another 10.000 in range 10.001..20.000-- Table test_fact was populated with 10.000 rows, the values in fact1 column range from 1..10.000
> DELETE FROM test_extension1 WHERE fact1 > 10000;
10000 rows deleted.
Elapsed: 00:00:00.37
> UPDATE test_extension1 2 SET fact1 = fact1 + 10000 3 WHERE fact1 > 10000 and fact1 <= 20000;
10000 rows updated.
Elapsed: 00:00:00.36
11
Trace File Analysis
Without Index With Index
12
Trace File Analysis – Without Index
13
Trace File Analysis – With Index
14
Conclusion•There’s a huge gain on creating indexes on columns with
foreign keys.•If DML operations are frequent on these tables without
indexes, the impact on the database is huge because the tables being read so frequently are loaded by Oracle into memory to speed up the process, however this action will impact the CPU.
•If the tables are in production for a long time, the index creation may have a negative impact in other queries running on the table, specially in older Oracle versions running on Rule. An impact analysis study on the index creating must be performed beforehand.
Training
• Performance Tuning Guide and Reference http://docs.oracle.com/cd/B10500_01/server.920/a96533/toc.htm
• SQL Reference http://docs.oracle.com/cd/B10500_01/server.920/a96540/toc.htm
• PL/SQL User's Guide and Reference http://
docs.oracle.com/cd/B10500_01/appdev.920/a96624/toc.htm
Resources at Oracle website
Thank you
Carlos Oliveira / July 14, 2014