1. What is an Index in MySQL?
An index is a database structure that improves the speed of data retrieval on a table. It acts like a roadmap, allowing MySQL to quickly locate rows without scanning the entire table. While indexes enhance SELECT queries, they require additional storage and can slightly slow down INSERT, UPDATE, and DELETE operations.
2. Primary Key Index
The Primary Key is a unique identifier for each row in a table. Creating a primary key automatically creates a unique index, ensuring each value in the key column is distinct and not null.
Example
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
Explanation:
employee_idis the primary key.- MySQL automatically creates a unique B-Tree index on
employee_id. - Guarantees each employee has a unique identifier.
- Example usage:
SELECT * FROM employees WHERE employee_id = 101;
- The query uses the primary key index to locate the row efficiently.
3. Unique Index
A Unique Index ensures that all values in a column or a combination of columns are distinct. Unlike a primary key, a table can have multiple unique indexes, and columns can accept NULL values (depending on the database engine).
Example
CREATE TABLE users (
user_id INT PRIMARY KEY,
email VARCHAR(100),
username VARCHAR(50),
UNIQUE INDEX idx_email (email)
);
Explanation:
idx_emailenforces uniqueness on theemailcolumn.- Prevents two users from having the same email address.
- Example query:
INSERT INTO users (user_id, email, username)
VALUES (1, '[email protected]', 'john');
- Attempting to insert another user with
'[email protected]'will fail.
4. Normal (Regular) Index
A Normal Index or Non-Unique Index improves query performance without enforcing uniqueness. It is commonly used for columns frequently searched with WHERE, ORDER BY, or GROUP BY.
Example
CREATE INDEX idx_last_name ON employees(last_name);
Explanation:
- Creates an index on the
last_namecolumn. - Speeds up queries filtering by last name:
SELECT * FROM employees WHERE last_name = 'Smith';
- MySQL uses the index to locate rows quickly.
- Unlike unique indexes, duplicates are allowed.
5. Full-Text Index
A Full-Text Index is designed for text searching in large text columns (CHAR, VARCHAR, or TEXT). It supports natural language searches using MATCH() and AGAINST().
Example
CREATE TABLE articles (
article_id INT PRIMARY KEY,
title VARCHAR(255),
body TEXT,
FULLTEXT INDEX idx_body (title, body)
);
Explanation:
idx_bodyis a full-text index ontitleandbody.- Enables fast keyword searches in text fields.
- Example search:
SELECT article_id, title
FROM articles
WHERE MATCH(title, body) AGAINST('MySQL optimization');
- MySQL searches the indexed text efficiently rather than scanning all rows.
- Ideal for blogs, content management systems, and search engines.
6. Key Differences Between Index Types
| Index Type | Enforces Uniqueness | Allows Multiple Indexes | Best Use Case |
|---|---|---|---|
| Primary Key | Yes | Only one per table | Unique row identifier |
| Unique Index | Yes | Multiple allowed | Enforcing column-level uniqueness |
| Normal Index | No | Multiple allowed | Frequent queries and filtering |
| Full-Text Index | No | Multiple allowed | Keyword and text searches |
7. Best Practices for Using Indexes
- Use Primary Keys for table identity: Always define a primary key for relational integrity.
- Use Unique Indexes for critical columns: Emails, usernames, or social security numbers benefit from uniqueness enforcement.
- Add Normal Indexes on frequently searched columns: Improves query performance without uniqueness constraints.
- Use Full-Text Indexes for large text searches: Avoid using normal indexes on large text fields for keyword searches.
- Avoid over-indexing: Excessive indexes slow down write operations.