Composite index and include columns in PostgreSQL

Composite index and include columns in PostgreSQL

In PostgresSQL, both composite indexes and indexes with included columns are used to enhance query performance, but they serve different purposes

Composite Index

A composite index is an index that include two or more columns as part of index key. The order of columns in a composite index is matters, for example, an index on (a, b) can be used for queries on a, a and b, but not efficiently for b alone.

Example :

-- Create composite index on columns name and grades on table student
CREATE INDEX idx_name_grades ON student (name, grades);

-- Query using index
SELECT * FROM student  WHERE name = 'Alice' AND grades = 90

-- Query not using index
SELECT * FROM student WHERE grades = 90        

Index with include columns

An index with included columns adds additional columns to the index that are not part of the index key. These included columns are stored in the index but are not used for lookups or ordering.

Example:

-- Create index with include column 
CREATE INDEX idx_customer_id_order_date ON orders (customer_id, order_date) INCLUDE (total_amount);

-- Query using index
SELECT customer_id, order_date, total_amount
FROM orders
WHERE customer_id = 1 AND order_date = '2024-06-16';        

Comparision between Composite Index and Index with include columns

Here are some comparision between these two.

Index Key

  • Composite key: All columns are part of index key
  • Index with include columns: Only specified key column

Order of columns in query

  • Composite key: Order matters
  • Index with include columns: Order of included columns doesn't matter

Covering Queries

  • Composite key: Can cover queries on key columns
  • Index with include columns: Can cover queries needing key and included columns


Viet Dung Nguyen

Backend Developer | Python | Odoo framework | Django rest framework

9 个月

very helpful!

回复
Steve Loc

??Java Developer | Database | Fullstack

9 个月

It very helpful, but it maybe better if I can see the result or execute plan of these queries. Thanks for sharing haha <3

回复
D??ng Xuan ?à

??Java Software Engineer | Oracle Certified Professional

9 个月

Very helpful!

回复
Cuong Manh Doan

? Software Engineer | Fullstack developer

9 个月

Interesting!

要查看或添加评论,请登录

Bùi Minh Hoàng的更多文章

  • Rank in MySQL

    Rank in MySQL

    Introduction to MySQL RANK() function The RANK() fucntion assigns a rank to each row within the partition of a result…

  • Fuzzy Search in Django with PostgreSQL

    Fuzzy Search in Django with PostgreSQL

    In real life, sometimes we want to search football but have the wrong typo, like footbal or fotball, but Google or…

    2 条评论
  • Why does offset make your query slow?

    Why does offset make your query slow?

    When working with Pagination, whenever i receive an API request with a URL like: /employee?page=10&count=10, my sql…

    4 条评论
  • Setup Redis Replication

    Setup Redis Replication

    In this article, i will setup Redis replication with 1 master and 3 replicas. First, create the project folder: RedisM…

    6 条评论
  • Yields and List in Python

    Yields and List in Python

    In this article, i will compare two common ways to return a sequence of values from a function is using yield to create…

    1 条评论
  • Django ORM: prefetch_related

    Django ORM: prefetch_related

    In previous article, i mentioned select_related to minimize time hit database in Django, so in this article, i addition…

    1 条评论
  • Django ORM select_related.

    Django ORM select_related.

    In Django, select_related are designed to stop the deluge of database queries that are caused by accessing related…

  • Pessimitic locking and Optimistic locking in Django: Managing concurrent transactions

    Pessimitic locking and Optimistic locking in Django: Managing concurrent transactions

    In previous article, i mentioned using select_for_update() to solve concurrent transactions. select_for_update() can be…

    6 条评论
  • Using select_for_update() to solve concurrent transactions

    Using select_for_update() to solve concurrent transactions

    The Bank Account Transfer Problem A classic example of concurrent transaction issues is the bank account transfer…

    5 条评论
  • Transaction in Django ORM

    Transaction in Django ORM

    Django’s default behavior is to run in autocommit mode. Each query is immediately committed to the database, unless a…

    1 条评论

社区洞察

其他会员也浏览了