MySQL
POSTGRESQL
Search
SQL
SQL SERVER

How and Why to Add an Index in SQL

intro

An index in SQL is a friend to many developers and DBAs. Or is it? Find out everything you need to know about indexing in this blog!

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE

Let’s face it — if you’ve heard about databases, you know your way around indexes. Some of you may know indexes better, some a little worse, but nonetheless, saying that many consider indexes one of your friends is no exaggeration. What is an index in SQL? How does it work? Why to use it? Time to find it out!

What are Indexes?

On a high level, an index in SQL is simply a data structure that helps quickly find rows. If you are a frequent reader of this blog, you will know that multiple types of database management systems offer multiple distinct types of indexing in SQL to choose from.

MySQLers will say that there are many indexes you can pick from. However, contrary to popular belief, indexes in MySQL aren’t categorized into many categories, but they fall into two instead — all indexes, no matter what your use case is, will be of one or the other type:

  1. B-tree, or Balanced Tree, indexes.
  2. R-tree, or Spatial, indexes.

Other database management systems, especially those notorious for their widespread support of indexing (PostgreSQL, anyone?), offer a more feature-rich selection, but nonetheless, one thing remains: indexes are data structures that are used to quickly find data.

Types of Indexes

The types of indexes that are available in a particular database management system will be directly dependent on what database management system is in use. Contrary to a popular belief, non-relational database management systems like MongoDB also support indexing.

After all, indexing isn’t much different than in relational database management systems either. According to the docs, given that an appropriate index exists for a query, MongoDB would use the index to limit the number of documents it must scan.

However, for an index in SQL to be applied and used properly, we first must make adequate choices. In other words, we must choose an adequate index for our use case properly.

In this case, users of PostgreSQL will have a feature-rich choice menu, while users of other database management systems like SQL Server or MySQL will have fewer options to choose from, but that shouldn’t cause any issues no matter what database management system you find yourself using because:

  • PostgreSQL is renowned for its wide support for various data types and indexing types.
  • MySQL offers a wide variety of indexes to select from: B-Tree indexes alone include vanilla indexes, covering and clustered indexes, unique indexes, full-text indexes, as well as descending and ascending indexes. A spatial index is a fit for geospatial data.
  • MongoDB supports indexing by limiting the number of scanned documents when an index is in use.
  • Indexes in SQL Server are similar to indexes in MySQL in that they can be clustered, non-clustered, unique, filtered, and have other similar characteristics. SQL Server DBAs will agree with me saying that an index in SQL Server is also frequently only referred to as a B-tree index, but that’s a general reference and there are many other nuances you should be wary about. Find out more in the official documentation.

A table depicting the types of indexes in various database management systems can be seen below:

DatabaseTypeIndex Specifics
MySQL/MariaDB/Percona ServerRelational (SQL)All indexes that are not B-tree indexes are R-tree indexes. R-tree indexes are spatial indexes, and B-tree indexes include fulltext indexes, covering indexes, unique indexes, and other indexes. More information here.
SQL ServerRelational (SQL)Various index types available for use, including clustered, non-clustered, hash indexes, unique and columnstore indexes, and more. More information here.
MongoDBNon-Relational (NoSQL)Wide support for indexing including single field indexes, compound indexes, multikey indexes, geospatial indexes, text indexes, hash indexes, and other types. All details available here.
PostgreSQLObject-RelationalWidely known for its extensive support for data types, PostgreSQL supports several index types including B-tree, hash, GiST, SP-GiST, GIN, BRIN.

Which Index to Choose and When?

With so many choices involving indexing, it’s only natural that people get lost in the jungle. It’s obvious that there are many choices you can employ, but nonetheless, choosing a proper index type is a challenge for many. Below, you will find some general guidelines advising you on how to choose an index type:

  1. Evaluate your use case. What is your application used for and why are you considering using indexes in the first place? Is it because your data grew to a level where ordinary SELECTs don’t cut it anymore or are there other reasons?
  2. What database management system are you using? Carefully consider the type of database management system in use. The type of DBMS you find yourself using and your use case will directly influence the types of indexes you will be able to choose from.
  3. Consider your server and related variables. You’re probably adding indexes to make SELECT queries perform faster: with that comes a bunch of other related things. Before adding indexes, you not only need to consider your DBMS and the specific use case it assists with but also its internal characteristics, such as the buffer pool for your specific storage engine, the size of its log files (these are important when you’re restoring data from a crash), and a bunch of other configuration variables. Configure your database properly so that your database can make use of the resources available on your server before considering using indexes. Perhaps you don’t even need an index?
  4. What type of index do you need? Last but not least, walk yourself through the documentation of your specific database management system and consider the type of index you need to support your operations.

Besides these variables, there may be a couple of other things worthy of your consideration — these may include the load towards your database, client or employer requirements, and even the location of your server when it comes to CDN choices and the like.

Also keep in mind that SQL indexes make reading operations faster at the expense of slowing down INSERT, UPDATE, and DELETE operations because when data is inserted, updated, or deleted, entries in the SQL index need to be updated together with it. Discover more in our guide on 10x query performance with a database index.

However, no matter your choices and the situation you are in, there are a bunch of other things you need to think about when using an index in SQL databases.

Beyond Indexing — SQL Clients

One of the primary things necessitating your consideration would be related to SQL clients — you have to run your SQL queries somewhere, right?

Here’s where SQL clients like DbVisualizer come in — with its powerful auto-complete, query-building, visualization, and schema inspection capabilities (there are more!), it can help you to understand how, what, and when indexes are used in your database as well as provide you with powerful equipment to deal with all kinds of issues:

DbVisualizer Listing Columns in the car_data table in MariaDB
DbVisualizer Listing Columns in the car_data table in MariaDB

In the screenshot above, you see DbVisualizer’s ability to depict columns in a table. I would direct your attention to the fact that in this scenario, DbVisualizer is not only able to depict the fields and the indexes that they contain, but also provide you with a bunch of other information that you can act on.

In particular:

  • Type will tell you whether you can use an index in SQL on that column in the first place (you cannot index TEXT values), whether columns are defined to be NULL or not will inform you on what values they store as well as their weight (all NULL values come with a size too).
  • Default will depict the default value for that specific column.
  • Extra will depict whether the column increments automatically or not (meaning that NULL values will turn into numbers).
  • Privileges will depict the privileges that are necessary to access that column.

That’s a good chunk of information — and that information is only relevant to a very small part of your database, too! Now imagine having such an amount of information wherever you go.

Awesome, right? Why not try DbVisualizer today? Click here and receive a free 21-day trial of the software!

Summary

In SQL, an index is a data structure that acts like an index in a book helping your database (and, consequentially, your application) quickly find rows. A query that uses an index in SQL can be completed quicker because it can eliminate certain rows from consideration.

By choosing an index to assist your use case, you’re choosing a reliable friend that needs to be cared for — that friend, however, also promises you to make your query performance faster.

FAQ

What is an Index in SQL?

An index in SQL is a data structure that helps your database quickly find data. Using an index, a database is able to find data that the user asks for by acting pretty much like an index in a book.

What SQL index should I choose?

The exact type of index in SQL you will need to employ depends on multiple factors including what kind of a database management system you find yourself using, the index type, the amount of data stored in a specific table, what tables are accessed, your query structure, etc.

In most cases, you will use a B-tree index — however, the exact type of that index (covering, unique, vanilla, full-text, etc.) depends on the factors stated above.

Why should I use DbVisualizer?

Consider using DbVisualizer because it’s one of the most popular SQL clients that is available on the market today. DbVisualizer helps you with everything from visualizing the output of queries and automatically completing them to exporting your precious data in a variety of formats.

Learn more about DbVisualizer and start using it today!

Where can I learn more about databases?

To learn more about database management systems and things related to them, we advise you follow the documentation of your specific database management system in question, watch YouTube videos relevant to the subjects you want to learn, and read books.

We’ve provided examples on a database called “hacking_mysql” not without a reason — “Hacking MySQL: Breaking, Optimizing, and Securing MySQL for Your Use Case” is a book depicting indexes in MySQL and discussing other things that help you optimize and secure database instances. Learn more about the book here, and until next time.

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

CREATE DATABASE in PostgreSQL: A Complete Guide

author Antonello Zanini tags POSTGRESQL 6 min 2025-04-30
title

Time-Tested Ways on How to Prevent SQL Injection Attacks

author Lukas Vileikis tags SQL 9 min 2025-04-29
title

How Often Should SQL Transaction Logs Be Backed Up?

author Antonello Zanini tags MySQL POSTGRESQL SQL SQL SERVER 7 min 2025-04-28
title

A Beginner's Guide to Vector Search Using pgvector

author Lukas Vileikis tags POSTGRESQL Vectors 3 min 2025-04-24
title

TRUNCATE vs DELETE: SQL Comparison of the Two Statements

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-04-23
title

SQL Performance Tuning in Relational Databases: A Walkthrough

author Lukas Vileikis tags SQL 10 min 2025-04-22
title

How to Extract the Year from a Date in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-04-21
title

How To List Tables In Postgres: Complete Guide

author Leslie S. Gyamfi tags POSTGRESQL 6 min 2025-04-17
title

A Complete Guide to an SQL Recursive Query Expression

author Lukas Vileikis tags MySQL SQL 5 min 2025-04-16
title

A Guide to the SQL Standard Deviation Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-04-15

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.