Mysql SQL optimization actual combat EXPLAIN articles

table of Contents

EXPLAIN

1. Single table optimization

1. First create a single table 

2. Insert data

3. Write sql according to the conditions

4. Use EXPLAIN to analyze SQL

5. Start optimization 

6. The optimization is complete


EXPLAIN

 

ps: This blog records SQL optimization actual combat explain. The relevant theoretical knowledge is summarized in other articles, and some keywords or knowledge points will not be explained one by one.

1. Single table optimization

1. First create a single table 

CREATE TABLE `article` (
  `id` int(10) NOT NULL,
  `author_id` int(10) DEFAULT NULL COMMENT '作者ID',
  `category_id` int(10) DEFAULT NULL COMMENT '分组ID',
  `views` varchar(10) DEFAULT NULL COMMENT '浏览次数',
  `comments` varchar(10) DEFAULT NULL COMMENT '备注',
  `title` varchar(255) DEFAULT NULL COMMENT '标题',
  `content` varchar(1000) DEFAULT NULL COMMENT '正文',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Insert data

INSERT INTO `explain`.`article` (`id`, `author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES ('1', '1', '1', '1', '1', '1', '1');
INSERT INTO `explain`.`article` (`id`, `author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES ('2', '2', '2', '2', '2', '2', '2');
INSERT INTO `explain`.`article` (`id`, `author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES ('3', '3', '3', '3', '3', '3', '3');
INSERT INTO `explain`.`article` (`id`, `author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES ('4', '4', '4', '4', '4', '4', '4');
INSERT INTO `explain`.`article` (`id`, `author_id`, `category_id`, `views`, `comments`, `title`, `content`) VALUES ('5', '5', '5', '5', '5', '5', '5');

3. Write sql according to the conditions

When the query category_id is 2 and the comments is greater than 1, the author_id with the most views

SELECT a.author_id FROM `article` a WHERE a.category_id=2 AND a.comments >1 ORDER BY a.views LIMIT 1;

4. Use EXPLAIN to analyze SQL

1. No index is used, the worst result of the full table query 2. Usingfilesort needs to be optimized for specific reasons, please refer to the previous blog

5. Start optimization 

1. Create an index

The above figure uses three fields, create a composite index to include these three fields

2. View the index structure

3. Check again

Analysis: The index is used, the full table scan is processed, but Using filesort is not resolved, but the index is invalid because the comments is a range query, for example: the three fields are the first, second and third floors, the first floor is in normal use, and the second floor cannot be used , The sorting cannot use the internal sorting of the index, and the file sorting is used.

4. Index delete

Because the created index failed to resolve the Using filesort in Extra, the index needs to be deleted and recreated

View index structure

5. Create a new index

Because the ccv index cannot be resolved, assuming that the intermediate range query is skipped to create a cv index

6. The optimization is complete

Guess you like

Origin blog.csdn.net/LiuY521/article/details/114138259
Recommended