Open In App

Foreign Key Indexing and Performance in PostgreSQL

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

As the world of databases is always changing, PostgreSQL is one of the autonomous options because of its dependability, capability to handle huge amounts of data, and fast performance. Effective indexing, especially for foreign keys, is an important key for enhancing the speed of PostgreSQL.

Appropriate indexes for foreign keys can greatly speed up queries, protect database integrity, and improve the performance and efficiency of the database. Through this article, we will learn how to optimize your PostgreSQL database with foreign key indexing.

Understanding Foreign Key Indexing in PostgreSQL

Foreign keys establish relationships between tables in a relational database, bolstering referential integrity and enforcing data constraints. When a foreign key constraint is established between tables in PostgreSQL, indexing can be implemented at either end of the foreign key relationship: the target table or the source table.

1. Indexing at the Target of a Foreign Key

A foreign key index is created by PostgreSQL on the referenced columns when a foreign key constraint is added. The index will consequently boost JOIN operations and other queries that deal with foreign key relationships by facilitating fast lookups in the referenced table.

2. Indexing at the Source of a Foreign Key

Instead of creating an index on the primary key of the referenced table, you can also make an index on the foreign key columns in the referencing table. This index helps in locating records in the referencing table quickly, improving the efficiency of queries that fetch data from connected tables.

Example of Foreign Key Indexing and Performance in PostgreSQL

Consider a scenario where we have two tables: orders and customers. The orders table has a foreign key constraint referencing the customer_id column in the customers table.

To optimize query performance, we can create an index on the customer_id column in both the orders and customers tables.

-- Create tables
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
name VARCHAR(100)
);

CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INT REFERENCES customers(customer_id),
order_date DATE
);

-- Insert sample data
INSERT INTO customers (name) VALUES ('John'), ('Alice'), ('Bob');

INSERT INTO orders(customer_id, order_date) VALUES
(1, '2024-03-01'),
(1, '2024-03-05'),
(2, '2024-03-02'),
(3, '2024-03-03');

Customer Table:

Customer_table

customers Table

Order Table:

orders-table

Orders Table

Query Performance Comparison

Let’s compare query performance with and without indexes:

Query without index

-- Query without index
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 1;

Output:

Seq Scan on orders (cost=0.00..31.75 rows=10 width=16) (actual time=0.013..0.015 rows=2 loops=1)
Filter: (customer_id = 1)
Rows Removed by Filter: 2
Planning Time: 0.047 ms
Execution Time: 0.029 ms
(5 rows)

Query with index

-- Query with index
CREATE INDEX ON orders (customer_id);
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 1;

Output:

Bitmap Heap Scan from orders (cost=4.22..8.24 rows=1 width=16) (actual time=0.033..0.034 rows=2 loops=1)
Recheck Cond: ( id_customer = 1)
Heap Blocks: exact=1
-> Bitmap Index Scan on orders_customer_id_idx (cost=0.00..4.22 rows=1 width=0) (actual time=0.024..0.024 rows=2 loops=1)
Index Cond: Where (customer_id = 1),
Planning Time: 0.124 ms
Execution Time: 0.050 ms
(6 rows)

Identifying Missing Indexes

PostgreSQL offers ways to find missing indexes and improve query speed. The EXPLAIN command shows query execution plans that can help identify where indexes are needed. There are also external tools like pg_stat_statements and pgBadger that give information about query performance, making it easier to see which indexes are missing.

-- Check for missing indexes

EXPLAIN ANALYZE SELECT * FROM orders WHERE order_date = '2024-03-01';

EXPLAIN ANALYZE SELECT * FROM customers WHERE name = 'John';

Output:

-- Query for creation of missing index on orders table is as under

Seq Scan on orders (cost=0.00..37.88 rows=1 width=16) (actual time=0.032..0.032 rows=1 loops=1).
Filter: (order_date = '2024-03-01'::date)
Rows Removed by Filter: By 3
Planning Time: 0.064 ms
Execution Time: 0.040 ms
(5 rows)

-- Output query on customers table missing index

Seq Scan on customers (cost=0.00..32.75 rows=1 width=36) (actual time=0.019..0.019 rows=1 loops=1)
Filter: (name = 'John'::varchar)
Rows Removed by Filter: Incentives are implemented among crew members to maintain a standard of cleanliness and orderliness on board.
Planning Time: 0.064 ms
Execution Time: 0.027 ms
(5 rows)

Should We Create Indexes for All Foreign Keys?

The creation of indexes on foreign keys can up the speed of database queries. On the contrary, it’s not a good decision to utilize a large number of indexes. By the way, only index creation of columns that are frequently used in a query is recommended. The ideal columns to index are the ones used in JOIN operations, in WHERE clause, and in ORDER BY clause. Indexes on these fields will significantly increase your query speed.

Conclusion

In conclusion, Implementing foreign key indexing in PostgreSQL is a must as it contributes to the data performance and increases data accuracy. An index of both the target and source performance will not only make the query performance faster but also make it faster to fetch the data. It improves the general functionality of PostgreSQL by ensuring it is capable of treating inputs correctly. Thoroughly developing and administrating indexes guarantee that PostgreSQL is a dependable system that is fast-paced enough to be used in different aspects.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads