MySQL index is an important tool to improve query efficiency, it can speed up data retrieval. Several common indexes in MySQL are introduced in detail below, and specific code examples are provided.
Sample code:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
Sample code:
CREATE TABLE student (
id INT,
name VARCHAR(50),
age INT,
UNIQUE INDEX idx_name (name)
);
Sample code:
CREATE TABLE student (
id INT,
name VARCHAR(50),
age INT,
INDEX idx_age (age)
);
Sample code:
CREATE TABLE articles (
id INT,
title VARCHAR(100),
content TEXT,
FULLTEXT INDEX idx_content (content)
);
Sample code:
CREATE TABLE book (
id INT,
title VARCHAR(100),
author VARCHAR(50),
price DECIMAL(8 ,2),
INDEX idx_title_author_price (title, author, price)
);
Sample code:
CREATE TABLE locations (
id INT,
name VARCHAR(100),
location POINT,
SPATIAL INDEX idx_location (location)
);
Sample code:
CREATE TABLE user (
id INT,
name VARCHAR(50),
age INT,
HASH INDEX idx_age (age)
);
The above are several index types commonly used in MySQL. Different index types are suitable for different query scenarios. In practical applications, selecting the appropriate index type according to specific needs and data characteristics can effectively improve the query efficiency of the database.
The above is the detailed content of What are the different types of MySQL indexes?. For more information, please follow other related articles on the PHP Chinese website!