</>
Skip to content
MySQL lessons (33/48)

MySQL — Indexes

What is an Index?

An index is a data structure that speeds up row lookups, similar to a book's index.

CREATE INDEX idx_email ON customers(email);

Types of Indexes

TypePurpose
PRIMARY KEYUnique identifier, one per table
UNIQUEEnsures no duplicate values
INDEXStandard speedup for lookups
FULLTEXTFull-text search on text columns
COMPOSITEIndex on multiple columns

Create Index

CREATE INDEX idx_name ON customers(last_name, first_name);

Unique Index

CREATE UNIQUE INDEX idx_email ON users(email);

Composite Index

CREATE INDEX idx_dept_salary ON employees(department, salary);

View Indexes

SHOW INDEX FROM customers;

Drop Index

DROP INDEX idx_email ON customers;

When to Create Indexes

  • Columns in WHERE clauses
  • Columns in JOIN conditions
  • Columns in ORDER BY
  • High-cardinality columns (many unique values)

When NOT to Index

  • Small tables
  • Columns with few unique values
  • Frequently updated columns
  • Tables with heavy INSERT/UPDATE workload

Mini Practice

Write SQL code that:

  1. Creates a single-column index
  2. Creates a composite index
  3. Creates a unique index
  4. Views existing indexes on a table

Up Next

Continue with Constraints — enforcing data integrity rules.

Related Topics

Frequently Asked Questions about Indexes

What is Indexes in MySQL?

Indexes is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Indexes?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Indexes.

Why is Indexes important in MySQL?

Indexes is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.