MySQL Advanced Chapter - Index Creation and Design Principles

1. Classification of indexes

MySQL indexes include common indexes, unique indexes, full-text indexes, single-column indexes, multi-column indexes, and spatial indexes.
In terms of functional logic , there are four main types of indexes, namely ordinary indexes, unique indexes, primary key indexes, and full-text indexes.
According to the physical implementation , indexes can be divided into two types: clustered indexes and non-clustered indexes.
It is divided into single-column index and joint index according to the number of action fields.


2. Three ways to create an index

2.1 Method 1: CREATE TABLE

2.1.1 Small example

CREATE DATABASE dbtest2;

USE dbtest2;

CREATE TABLE dept (
		dept_id INT PRIMARY KEY AUTO_INCREMENT,
		dept_name VARCHAR(20)
);

SHOW INDEX FROM dept;

CREATE TABLE emp (
		emp_id INT PRIMARY KEY AUTO_INCREMENT,
		emp_name VARCHAR(20) UNIQUE,
		dept_id INT,
		CONSTRAINT fk_emp_dept_id FOREIGN KEY(dept_id) REFERENCES dept(dept_id)
);

SHOW INDEX FROM emp;

2.1.2 Common Index

CREATE TABLE book (
	book_id INT,
	book_name VARCHAR(100),
	`authors` VARCHAR(100),
	info VARCHAR(100),
	`comment` VARCHAR(100),
	year_publication YEAR,
	INDEX idx_bname(book_name)
);

SHOW INDEX FROM book;

If we write a simple sql statement and filter with book_name after where, we can use the explain performance analysis tool to see what it looks like?

EXPLAIN 
SELECT *
FROM book
WHERE book_name = 'mysql';

2.1.3 Unique Index

CREATE TABLE book1 (
		book_id INT,
		book_name VARCHAR(100),
		`authors` VARCHAR(100),
		info VARCHAR(100),
		`comment` VARCHAR(100),
		year_publication YEAR,
		UNIQUE INDEX uk_idx_cmt(`comment`)
);

SHOW INDEX FROM book1;

Next, insert three records into the table in turn. When inserting the second record, an error will be reported. And the third record is executed normally.

INSERT INTO book1(book_id, book_name, `comment`)
VALUES(1, 'MySQL高级', '适合有数据库开发经验的人员学习');

INSERT INTO book1(book_id, book_name, `comment`)
VALUES(1, 'MySQL高级', '适合有数据库开发经验的人员学习');

INSERT INTO book1(book_id, book_name, `comment`)
VALUES(1, 'MySQL高级', NULL);

Eventually, in the table book1, there will be two records.

2.1.4 Primary key index

CREATE TABLE book2 (
		book_id INT PRIMARY KEY,
		book_name VARCHAR(100),
		`authors` VARCHAR(100),
		info VARCHAR(100),
		`comment` VARCHAR(100),
		year_publication YEAR
);

SHOW INDEX FROM book2;

2.1.5 Single Column Index

In fact, the ones we created above are all single-column indexes.

CREATE TABLE book3 (
		book_id INT ,
		book_name VARCHAR(100),
		AUTHORS VARCHAR(100),
		info VARCHAR(100) ,
		COMMENT VARCHAR(100),
		year_publication YEAR,
		UNIQUE INDEX idx_bname(book_name)
);

SHOW INDEX FROM book3;

2.1.6 Joint Index

CREATE TABLE book4 (
		book_id INT ,
		book_name VARCHAR(100),
		AUTHORS VARCHAR(100),
		info VARCHAR(100) ,
		COMMENT VARCHAR(100),
		year_publication YEAR,
		INDEX mul_bid_bname_info(book_id, book_name, info)
);

SHOW INDEX FROM book4;

Let's analyze through two SQLs, how does the joint index go in the search process?

EXPLAIN 
SELECT *
FROM book4
WHERE book_id = 1001 AND book_name = 'mysql';

Explanation: Because the joint index we built is in the order of book_id, book_name, info, when building a B+ tree, we first sort by book_id, and when book_id is the same, then sort by book_name, and when book_name is the same, Finally, sort by info. (In the B+ tree, book_name is actually located below book_id, and the search must first go through book_id and then book_name)

So the above sql will go to book_id first, then book_name.

EXPLAIN 
SELECT *
FROM book4
WHERE book_name = 'mysql';

After the above analysis, why does this sql not take the joint index? (Doesn't book_name exist in the joint index?), here is actually a question of the order of building the B+ tree. If you want to use the book_name index in the where filter, you must first use the book_id index, because book_name is in the joint index. It is located after book_id, so book_name is below book_id in the B+ tree. You haven't even reached the second level (hypothetical) of the B+ tree, so how can you reach the third level of the B+ tree? So this sql will not go through the index.

2.1.7 Full-text indexing

CREATE TABLE test4 (
		id INT NOT NULL,
		NAME CHAR(30) NOT NULL,
		age INT NOT NULL,
		info VARCHAR(255),
		FULLTEXT INDEX futxt_idx_info(info(50))
);

SHOW INDEX FROM test4;

2.2 Method 2: ALTER TABLE ... ADD INDEX ...

DROP TABLE IF EXISTS book5;
CREATE TABLE book5 (
		book_id INT ,
		book_name VARCHAR(100),
		`authors` VARCHAR(100),
		info VARCHAR(100) ,
		`comment` VARCHAR(100),
		year_publication YEAR
);

ALTER TABLE book5 ADD INDEX idx_cmt(`comment`);
ALTER TABLE book5 ADD UNIQUE INDEX uk_idx_bname(book_name);
ALTER TABLE book5 ADD INDEX mul_bid_bname_info(book_id, book_name, info);

SHOW INDEX FROM book5;

2.3 Method 3: CREATE INDEX ... ON ...

DROP TABLE IF EXISTS book6;
CREATE TABLE book6 (
		book_id INT ,
		book_name VARCHAR(100),
		`authors` VARCHAR(100),
		info VARCHAR(100) ,
		`comment` VARCHAR(100),
		year_publication YEAR
);

CREATE INDEX idx_cmt ON book6(`comment`);
CREATE UNIQUE INDEX uk_idx_bname ON book6(book_name);
CREATE INDEX mul_bid_bname_info ON book6(book_id, book_name, info);

SHOW INDEX FROM book6;


3. Two ways to delete an index

3.1 Use ALTER TABLE to delete indexes

3.2 Use the DROP INDEX statement to drop an index

When prompted to drop a column in a table, if the column to be dropped is part of the index, the column is also dropped from the index. If all the columns that make up the index are dropped, the entire index will be dropped.
ALTER TABLE book6 DROP INDEX idx_cmt;

DROP INDEX uk_idx_bname ON book6;

ALTER TABLE book6 DROP COLUMN book_name;
ALTER TABLE book6 DROP COLUMN book_id;
ALTER TABLE book6 DROP COLUMN info;

SHOW INDEX FROM book6;


4. Index design principles (to be continued, to be completed tomorrow...)

4.1 What situations are suitable for creating an index?

4.2 What situations are not suitable for creating an index?

Guess you like

Origin blog.csdn.net/weixin_43823808/article/details/124105402
Recommended