MySQL Performance

How to Create an Index in MySQL (With Examples)

Learn how to create indexes in MySQL to speed up queries. Covers single-column, composite, unique, and full-text indexes with performance tips.

Mar 12, 2026 6 min read

Introduction

Indexes are the single most effective way to speed up MySQL queries. Without an index, MySQL scans every row in the table (full table scan). With the right index, it jumps directly to matching rows.

Create a Basic Index

A single-column index speeds up queries that filter or sort by that column.

-- Create index on a single column:
CREATE INDEX idx_users_email ON users (email);

-- Verify the index was created:
SHOW INDEX FROM users;

Tip: Name your indexes descriptively: idx_{table}_{column}. This makes it easy to identify them later.

Create a Composite (Multi-Column) Index

Composite indexes cover queries that filter on multiple columns. Column order matters — put the most selective column first.

-- Index on two columns:
CREATE INDEX idx_orders_status_date
ON orders (status, created_at);

-- This index helps queries like:
SELECT * FROM orders
WHERE status = 'shipped'
AND created_at > '2026-01-01';

Tip: The leftmost prefix rule applies: an index on (a, b, c) helps queries filtering on (a), (a, b), or (a, b, c), but NOT (b, c) alone.

Create a Unique Index

A unique index enforces that no two rows can have the same value in the indexed column(s).

-- Unique index (prevents duplicates):
CREATE UNIQUE INDEX idx_users_email_unique
ON users (email);

-- This also works as a constraint:
-- INSERT with duplicate email will fail with Error 1062

Tip: If you need both uniqueness and fast lookups, a unique index gives you both in one.

Create a Full-Text Index

Full-text indexes enable natural language search on text columns. Much faster than LIKE "%keyword%" for large text fields.

-- Full-text index for search:
CREATE FULLTEXT INDEX idx_posts_content
ON posts (title, body);

-- Use it with MATCH AGAINST:
SELECT * FROM posts
WHERE MATCH(title, body)
AGAINST('mysql performance' IN NATURAL LANGUAGE MODE);

Tip: Full-text indexes work on CHAR, VARCHAR, and TEXT columns. They require the InnoDB or MyISAM engine.

Add Index to Existing Table with ALTER TABLE

You can also add indexes using ALTER TABLE syntax, which is useful in migrations.

-- Add index via ALTER TABLE:
ALTER TABLE users ADD INDEX idx_users_country (country);

-- Add unique index:
ALTER TABLE users ADD UNIQUE INDEX idx_users_username (username);

-- Drop an index:
ALTER TABLE users DROP INDEX idx_users_country;

Tip: On large tables, adding an index can lock the table. Use ALTER TABLE ... ALGORITHM=INPLACE for online DDL in MySQL 5.6+.

Check Query Performance with EXPLAIN

Use EXPLAIN to verify your indexes are being used.

-- Check if a query uses an index:
EXPLAIN SELECT * FROM users WHERE email = 'john@example.com';

-- Look for:
-- type: ref or const (good) vs ALL (full scan = bad)
-- key: should show your index name
-- rows: should be small number, not total table rows

Tip: If EXPLAIN shows type: ALL, your query is doing a full table scan. Add an index on the WHERE/JOIN columns.

Best Practices

  • Index columns used in WHERE, JOIN, ORDER BY, and GROUP BY clauses
  • Don't over-index — each index slows down INSERT/UPDATE/DELETE operations
  • Remove unused indexes with DROP INDEX to save disk space and write performance
  • Use EXPLAIN regularly to verify indexes are being used
  • For large tables, consider partial indexes or covering indexes

Generate MySQL Queries with AI2SQL

Skip the syntax lookup. Describe what you need in plain English and AI2SQL generates the correct MySQL query instantly.

Try AI2SQL Free

No credit card required

Frequently Asked Questions

How many indexes should a MySQL table have?

There is no hard limit, but aim for 3-5 indexes per table. Each index speeds up reads but slows down writes. Only index columns that appear in WHERE, JOIN, or ORDER BY clauses.

Does creating an index lock the table?

In MySQL 5.6+, most index operations use online DDL (ALGORITHM=INPLACE) which allows reads and writes during index creation. For very large tables, consider using pt-online-schema-change.

Can AI2SQL help me create indexes?

Yes. Describe your slow query to AI2SQL and it can suggest which indexes to create. You can also say "create an index for fast email lookups on the users table" and get the exact SQL.

Generate SQL from Plain English

Stop memorizing syntax. Describe what you need and let AI2SQL write the query for you.

Try AI2SQL Free

No credit card required