DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Trending

  • A Guide to Container Runtimes
  • Solid Testing Strategies for Salesforce Releases
  • Internal Developer Portals: Modern DevOps's Missing Piece
  • Unlocking AI Coding Assistants Part 2: Generating Code
  1. DZone
  2. Data Engineering
  3. Databases
  4. The Secrets of Indexes and Foreign Keys

The Secrets of Indexes and Foreign Keys

By 
Rafaelo Condret user avatar
Rafaelo Condret
·
Mar. 18, 20 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
29.0K Views

Join the DZone community and get the full member experience.

Join For Free

Indexes and foreign keys are great tools when confronted with large databases. They can be the answer to a good design and great performance. In this article, I will go through some tips that helped me understand how to use these tools efficiently and streamline my work with complex databases.

Every image example was done with DbSchema. I enjoy this tool because it is diagram oriented, integrates many features, and has a very good price. 

Foreign Keys

It’s a common mistake to avoid creating foreign keys in a database because they negatively impact the performance. It is true that foreign keys will impact INSERT, UPDATE and DELETE statements because they are data checking, but they improve the overall performance of a database. 

The main benefit of foreign keys is that they enforce data consistency, meaning that they keep the database clean. 

As an example, consider the two tables below. If the two are missing foreign keys, and we delete a department, then the employees associated with it will remain in the employee table as “bad records”. These scenarios happens very often and leads to a database full of junk that decreases performance.  

Without foreign keys: 

No foreign keys

No foreign keys

With foreign keys:


With foreign keys

With foreign keys

Indexes

An index for a database is like a table of contents for a book. Searching for data based on a column that is part of the index will allow us to make use of the index to quickly access the record. 

Consider the employee table with an index on firstname. Executing the query bellow will use this index.

SQL
 




xxxxxxxxxx
1


 
1
SELECT * from employees WHERE firstname = ?



Important things to know about indexes:

Databases Only Use One Index per Table and Query

Let's say we create two indexes on the employee table, idx_firstname and idx_lastname. If we execute the query below, the database will decide to use only one of the two indexes. 

SQL
 




xxxxxxxxxx
1


 
1
 SELECT * from employees WHERE firstname = ? and lastname=? 



Index Comparison

When executing a query, the database will compare the indexes with regard to the number of different entries that they contain. 

For example, if we have 2,000 different records in for firstname and 5,000 different records for lastname, it is more probable that using the lastname index will return a smaller number of rows that fit our search criteria. Therefore, it will use that index. 

Composite Indexes

An index that contains two or more columns is a composite index. When using a composite index, the query should always contain the first column of the index.

For example, consider we have the index idx(firstname, lastname). This index will work perfectly on the next query

SQL
 




xxxxxxxxxx
1


 
1
SELECT * from employees WHERE firstname = ? and lastname=?



But won’t work for this one

SQL
 




xxxxxxxxxx
1


 
1
SELECT * from employees WHERE lastname=?



Clustered Indexes

Giving the analogy from the beginning, in a clustered index, the table of contents is located at the end of the book. In a non-clustered index, the table of contents is located in a different place, outside the book. Clustered indexes are recommended when you have fewer updates in your data. 

When creating a clustered index, the table itself becomes the index. 

Clustered index

Clustered index

On the left side of the employees table, we can see how the indexes have different notations. The employee_id is a primary key, firstname and lastname form a composite index, while deparment_id is a unique index. 

Conclusion

Foreign keys are very useful for keeping a database clean and won't affect the SELECT statements. Indexes help you quickly browse the database and find the data you need. Knowing how and when to use them will improve database performance and make your work easier. 

Database sql Table of contents

Opinions expressed by DZone contributors are their own.

Related

  • How to Restore a Transaction Log Backup in SQL Server
  • How to Attach SQL Database Without a Transaction Log File
  • A Deep Dive into Apache Doris Indexes
  • Spring Boot Sample Application Part 1: Introduction and Configuration

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!