Book a Demo Start Instantly

What is a Database Index?

In the world of databases, an index functions similarly to the index found in a book. It facilitates swift access to the data rows within a table by minimizing the amount of data that needs to be examined. Employed to briskly locate rows with specific column values, indexes negate the need to search through every row in a database table each time it is accessed. While indexes can notably enhance data retrieval performance, they also introduce overhead for write operations, such as insert, update, or delete actions, due to the necessity of updating the index whenever data modifications occur.

Importance of Database Indexing

The crucial role of indexing in database management cannot be overstated. An adept indexing strategy leads to quicker data retrieval, drastically reducing the time required to execute queries. This feature is paramount in today’s data-rich and speed-centric environment. Without indexes, a database system would be forced to a table scan with every row to locate matching entries, a method that becomes increasingly impractical as the database size expands. Moreover, strategic indexing can bolster both database and application performance, enhancing the users’ experience as a result.

Indexes in TiDB

TiDB, as a distributed SQL database, accommodates various index types to boost query performance. Grasping the array of indexes TiDB avails and their respective use cases is fundamental for refining your database schema:

  1. Primary Key Indexes: The quintessential index type, which uniquely delineates each row in a table. In TiDB, the primary key might consist of a solitary column or a composition of columns (composite key).
  2. Unique Indexes: These are akin to primary key indexes albeit not necessarily the primary identifier for a row. Unique indexes assert that no two rows share identical values in their designated columns.
  3. Secondary Indexes (Non-unique Indexes): Employed to ameliorate the performance of queries that do not utilize the primary key or unique columns. Secondary indexes can expedite data access when searching through non-unique criteria.
  4. Composite Indexes: An index spanning two or more table columns. Most efficient when queries involve multiple columns. The sequence of columns in a composite index critically influences its efficiency.
  5. Invisible Indexes: Introduced in recent TiDB versions, this feature allows indexes to be “invisible” to the optimizer, beneficial for assessing the ramifications of index removal without actual deletion.

Deciding on the optimal index type necessitates an understanding of the application’s query patterns and requisites. Appropriate indexing strikes a nuanced equilibrium — sparse indexing might retard queries, whereas excessive indexing could decelerate write operations and inflate storage requirements.

Index Management in TiDB Serverless

As a fully managed MySQL-compatible database with a generous free tier, TiDB Serverless can effortlessly scale from zero to unlimited transactions. It supports index management just like in regular TiDB instances.To create an index in TiDB Serverless, you need to specify the table and key column(s) that you want to index, and the type of index. You can also specify the index name, if desired. Here’s a sample SQL statement to create a unique index on the email column of a table called users:

CREATE UNIQUE INDEX email_idx ON users(email);

To drop an index, you can use the following SQL statement:

DROP INDEX email_idx ON users;

You can also alter an index to change its attributes. For example, you can add a column to an existing index, or modify its type. Here’s an example SQL statement to change the type of an existing index:

ALTER TABLE users DROP INDEX email_idx, ADD FULLTEXT INDEX email_idx(email);

TiDB Serverless also supports composite indexes, which are indexes that cover multiple columns. To create a composite index, simply specify the columns in the order that you want them indexed. Here’s an example SQL statement to create a composite index on the first_name and last_name columns of a table called customers:

CREATE INDEX name_idx ON customers(first_name, last_name);

Overall, TiDB Serverless offers flexible and powerful index management capabilities that make it easy to optimize database performance.

Happy indexing on TiDB Serverless.

Sign Up for Free

Practical Tips for Beginners

When to Create Indexes

Index creation is a nuanced decision-making process, dictated by several guiding principles:

  • High Query Frequency: Columns regularly featured in WHERE clauses, JOIN conditions, or within an ORDER BY statement are prime indexing candidates.
  • High Cardinality: Columns with a vast uniqueness spectrum among their values most substantially benefit from indexing.
  • Balance: Although indexes accelerate data retrieval, they also slow down write operations due to the requisite index updates. Harmonizing your application’s read-write pattern is crucial.

You can find more information about Best Practices for Using Indexes in our documentation.

Common Mistakes to Avoid

  • Over Indexing: Indiscriminate indexing on every column “just in case” can deteriorate performance. Each index occupies disk space and necessitates maintenance amidst write operations.
  • Ignoring Query Patterns: Indexes should be sculpted around your application’s query behaviors. Analyzing query dynamics and customizing your indexing strategies accordingly is paramount.


Mastering the craft of indexing is indispensable for anyone working within the realms of TiDB or any database system at large. Correctly implemented, indexing can significantly expedite database queries, enhancing the efficiency of applications.

However, the power of indexing must be wielded judiciously. By assimilating the various index types supported by TiDB, recognizing when to forge indexes, and acknowledging common indexing pitfalls, developers can devise database schemas that optimize both speed and efficiency. Let the strategic use of indexes unlock your TiDB deployment’s utmost potential, enabling superior performance amidst voluminous data and intricate query requirements.

Try database indexing on TiDB Serverless today and optimize your query performance effortlessly!

Sign Up for Free

Last updated May 24, 2024

Spin up a Serverless database with 25GiB free resources.

Start Now