DEV Community

suryansh taragi
suryansh taragi

Posted on

DATABASE INDEXING IN PoatgreSQL

INDEXING

In PostgreSQL, indexing is like making a cheat sheet for the database to find things faster. Imagine you have a big book (table) and you want to quickly find specific information (rows) without reading the whole book.

So, an index is like a smart bookmark. It's a quick reference guide that helps PostgreSQL find what you're looking for without going through every page of the book. When you ask the database a question (query), it checks the index first to see if it can answer your question without reading the entire table.

PostgreSQL offers different types of these bookmarks, like B-tree, hash, GiST, SP-GiST, and BRIN, each designed for specific types of questions or ways of organizing information.

But, there's a catch. Creating these bookmarks takes up extra space, and it can make adding, changing, or deleting information a bit slower. So, you need to be thoughtful about when and where to use them. It's like deciding where to put your cheat sheet - it can be super helpful, but you don't want it to get in the way of actually using the book. So, choose wisely based on the questions you often ask the database and how your data is organized.

Types Of Index

1. B-Tree

In PostgreSQL, the B-tree index is like the go-to tool for efficiently handling data. It's so handy that it's the default choice - whenever you tell PostgreSQL to create an index without specifying the type, it automatically goes for the B-tree.

Picture the B-tree index as a well-organized tree structure. At the top, there's the root node, kind of like the big boss. This boss points to other nodes, and each node has lots of key-value pairs. The keys are like labels for quick finding, and the values guide us straight to the actual data in the table.

Making a B-tree index in PostgreSQL is simple. You just use the CREATE INDEX command. Here's how you do it:

CREATE INDEX index_name ON table_name;
Enter fullscreen mode Exit fullscreen mode

single column indexing

To create a B-tree index on a specific column in a table rather than indexing the entire table, you can use the following syntax:

CREATE INDEX index_name ON table_name (column_name);
Enter fullscreen mode Exit fullscreen mode

Here's an example using a table named "sales_info" and inserting sample data:

CREATE TABLE sales_info (
  sales_id integer NOT NULL, 
  email VARCHAR, 
  location VARCHAR, 
  item_purchased VARCHAR, 
  price VARCHAR
);

INSERT INTO sales_info (
  sales_id, email, location, item_purchased, price
) VALUES 
  (1, 'halie46@gmail.com', 'London', 'Headphone', '$50'), 
  (2, 'romaine21@gmail.com', 'Australia', 'Webcam', '$50'), 
  (3, 'frederique19@gmail.com', 'Canada', 'iPhone 14 pro', '$1259'), 
  (4, 'kenton_macejkovic80@hotmail.com', 'London', 'Wireless Mouse', '$20'), 
  (5, 'alexis62@hotmail.com', 'Switzerland', 'Dell Charger', '$15'), 
  (6, 'concepcion_kiehn@hotmail.com', 'Canada', 'Longitech Keyboard', '$499');
Enter fullscreen mode Exit fullscreen mode

To create a B-tree index on the "sales_id" column:

CREATE INDEX idx_sales_id ON sales_info (sales_id);
Enter fullscreen mode Exit fullscreen mode

Creating an index on specific columns can significantly improve query performance, especially when dealing with large datasets.

Hash Index

Hash indexes are optimized for rapid key-value searches. They excel in scenarios where query conditions involve equality checks on indexed columns, offering swift data retrieval by directly mapping the hash function to the location of the desired data. Hash indexes are particularly effective for operations like = or IN, emphasizing efficiency in equality comparisons.

Similar to other index types, hash indexes require regular maintenance during data modifications (inserts, updates, and deletes) to uphold data consistency. However, it's worth noting that maintaining hash indexes can be comparatively more resource-intensive than B-tree indexes. This is attributed to the additional overhead of resolving collisions and rehashing data.

To establish a hash index in PostgreSQL, you can employ the CREATE INDEX statement with the USING HASH clause, as demonstrated below:

CREATE INDEX hash_name ON table_name USING HASH (column_name);
Enter fullscreen mode Exit fullscreen mode

This command generates a hash index named "hash_name" on the specified column of the table.

It's crucial to note that while PostgreSQL supports hash indexes, they may not be the most suitable choice for range queries or sorting tasks. In such cases, B-tree indexes are typically preferred, being the default and widely used index type.

Here's an example illustrating the creation of a hash index on the "sales_id" column in the "sales_info" table:

CREATE INDEX idx_sales_id ON sales_info USING HASH(sales_id);
Enter fullscreen mode Exit fullscreen mode

CREATE INDEX idx_sales_id ON sales_info USING HASH(sales_id);

EXPLAIN (ANALYZE) 
SELECT 
  * 
FROM 
  sales_info 
WHERE 
  sales_id = 5;
Enter fullscreen mode Exit fullscreen mode

GiST and SP-GiST Indexes

GiST (Generalized Search Tree) and SP-GiST (Space-Partitioned Generalized Search Tree) indexes are advanced index types in PostgreSQL that provide support for a wide range of data types and search operations.

They are particularly useful for handling complex data structures and spatial data, GiST indexes are what you use if you want to speed up full-text searches.

Creating GiST and SP-GiST Indexes:

To create a GiST or SP-GiST index in PostgreSQL, you can use the CREATE INDEX statement with the USING GIST or USING SPGIST clause, respectively.

Here's an example of creating a GiST index on a geometry column:

CREATE INDEX index_geometry ON table_name USING GIST (geometry_column);
Enter fullscreen mode Exit fullscreen mode

And here's an example of creating an SP-GiST index on a tsvector column:

CREATE INDEX index_text_search ON table_name USING SPGIST (tsvector_column);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)