DbSchema Database Designer

DbSchema | Diagram Design Tips - The Secrets of Indexes & Fks

Publish on DbSchema Blog >>>

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.

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:


With foreign keys:



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 below will use this index.

SELECT * from employees WHERE firstname = ?

Important things to know about indexes:

1.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.

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

2.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.

3.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:

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

But won’t work for this one:

SELECT * from employees WHERE lastname=?

4.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.


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.


Foreign keys are 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.

Visually Manage Databases using DbSchema

DbSchema is a databases client and visual designer. DbSchema has a free Community Edition, which can be downloaded here.
DbSchema main features include:

DbSchema Designer alt >

Interactive Diagrams

Design tables, column and foreign keys directly in diagrams, by double-clicking them. Changes will be saved to the design model and, if DbSchema is connected to the database also into the database. More.

Connection Dialog alt >

Simple Connection Dialog

Choose the database location, the user and password, and simply get connected. Choose 'Edit Manually' into the JDBC URL combo to enter a custom URL. More.

Relational Data Explorer alt >

Relational Data Explorer

Explore data from multiple tables simultaneously, using foreign keys or virtual foreign keys. Double-click cells to edit the data. More.

Query Builder alt >

Query Builder

Create SQL Queries featuring JOINS, GROUP BY, ORDER BY just using the mouse. More.

SQL Query Editor alt >

SQL Query Editor

Edit and execute SQL Queries. The editor is autocompletion-enabled. More.

Schema Synchronization alt >

Design Schema in Team & Schema Deployment

DbSchema is using the design model, a copy of the schema structure, independent of the database.
The design model can be saved to file and shared in a team.
Connecting to another database you may compare the design model with the database, commit the differences or merge them in the design model. More.

Dark Theme alt >

Dark Theme

Configurable styles & dark theme. More.

Many features are available in the free Community edition.
The Pro edition adds capabilities to save the design to the model file, design schema in team and deploy the schema on multiple databases.

DbSchema can be downloaded for free. No registration is required.