CodeGym /Courses /SQL SELF /Main Types of Indexes: B-TREE, HASH<...

Main Types of Indexes: B-TREE, HASH, GIN, GiST

SQL SELF
Level 37 , Lesson 1
Available

So, in the PostgreSQL world, there are a few types of indexes, and each one is made for its own unique job. It's kinda like picking a way to get around: you might use a bike for a chill ride in the park, but if you need to get across town, you'll probably grab a car. Same deal here—different indexes are better for different jobs.

The main types of indexes in PostgreSQL include:

  • B-TREE indexes: all-purpose indexes for most jobs.
  • HASH indexes: optimized for exact matches.
  • GIN indexes: perfect for searching arrays and JSONB.
  • GiST indexes: used for complex data types, like geographic stuff.

Indexes are made to speed up row searching. There are 4 different optimization types: each one speeds up certain actions and works better with certain data types.

You can't really manage indexes directly. All you can do is just pick the index type: none, or one of the ones listed above. Let's break down each one so you know when and how to use them.

B-TREE Indexes

B-TREE (short for "balanced tree") is the most common index type, the backbone of PostgreSQL. This index creates a tree-like structure where data is organized to make searching, sorting, and filtering faster.

Imagine a library with shelves, and each shelf has books sorted alphabetically. If you're looking for a book that starts with "M", you don't have to check every book—you just start in the middle. Balanced trees work kinda like that.

When should you use them?

Pretty much always! B-TREE indexes are especially useful for:

  • Range searches: WHERE price > 100.
  • Sorting: ORDER BY name ASC.
  • Equality searches: WHERE id = 42.

Here's how you create one:

-- Create a B-TREE index for the price column in the products table:
CREATE INDEX idx_price ON products(price);

When you write a query like WHERE price > 100, PostgreSQL can use this index and won't have to scan the whole table.

HASH Indexes

HASH indexes use hash tables for super fast lookups. Their strong suit is exact value matching. But there's a catch: HASH indexes don't support range searches or sorting.

It's like a card catalog where every card has a specific number. If you want card number 42, the librarian finds it instantly. But if you ask, "show me cards numbered from 40 to 50," you're outta luck.

HASH indexes are only good for exact searches:

  • WHERE email = 'user@example.com'.
  • SELECT ... WHERE id = 123.

If you need ranges or sorting, HASH isn't your friend.

Example:

-- Create a hash index for the email column in the users table:
CREATE INDEX idx_email_hash ON users USING HASH (email);

Now PostgreSQL will use this index for queries like WHERE email = 'user@example.com'.

Heads up: HASH indexes are for special cases and are used less often than B-TREE.

GIN Indexes (Generalized Inverted Index)

GIN is a specialized index that does some real magic with arrays, JSONB, and text data. Imagine you have a cabinet with thousands of drawers, and each drawer is labeled. For example, the "apples" drawer has all the apples, the "bananas" drawer has all the bananas. To find apples or bananas, you don't have to dig through every drawer—you just go straight to the one you need.

GIN indexes are for:

  • Searching arrays: @> (contains), <@ (is contained by).
  • JSONB data: WHERE jsonb_data @> '{"key": "value"}'.

Example

-- Create a GIN index for the tags column, which contains arrays:
CREATE INDEX idx_tags_gin ON products USING GIN (tags);

Now PostgreSQL can quickly find products where the tags are, for example, "electronics" and "recommended".

GiST Indexes (Generalized Search Tree)

GiST indexes are a powerful tool for working with more complex data types, including geographic coordinates and ranges. They build trees that are optimized for spatial searches and range queries.

Picture a city map where every point is marked by its coordinates. You can quickly find all points within a 5 km radius of where you are.

GiST is good for:

  • Geographic data: SELECT ... FROM locations WHERE ST_DWithin(geom, point, distance).
  • Range searches: WHERE date_range && '[2023-01-01, 2023-12-31]'.

Example:

-- Create a GiST index for the location column, which has geographic data:
CREATE INDEX idx_location_gist ON places USING GiST (location);

Now you can run complex geographic queries, like finding the nearest points.

Index Comparison Table

Index Type Good for... Usage Examples Notes
B-TREE Range searches, sorting price > 100, ORDER BY name ASC All-purpose index.
HASH Exact equality checks email = 'user@example.com', id = 42 Doesn't support ranges.
GIN Arrays, JSONB tags @> '{tech}', jsonb_data @> '{"key": "value"}' Faster for complex data.
GiST Geography, ranges, distances ST_DWithin(geom, point, distance) Used for geo data.

Now you know the main types of indexes in PostgreSQL and how to use them. Remember: picking the right index is a strategic move that can make your queries fly. Checkmate, slowdowns!

2
Task
SQL SELF, level 37, lesson 1
Locked
Creating a `B-TREE` index to speed up data sorting
Creating a `B-TREE` index to speed up data sorting
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION