MySql basic knowledge summary (SQL optimization part 1)

1. Introduction to the return column of explain
1. The commonly used keywords of type
are system > const > eq_ref > ref > range > index > all.

system: The table has only one row, which is basically not used;
const: The table has at most one row of data matching, and the primary key query triggers more;
eq_ref: For each row combination from the previous table, read a row from the table. This is probably the best join type, except for the const type;
ref: for each combination of rows from the previous table, all rows with matching index values ​​will be read from this table;
range: only retrieve the rows in the given range, Use an index to select rows. When using =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN or IN operators to compare keyword columns with constants, range can be used; index: the join type is the same as
ALL , except that only the index tree is scanned. This is usually faster than ALL, because the index file is usually smaller than the data file;
all: full table scan;
in the actual sql optimization, it finally reaches the ref or range level.

2. Extra common keywords
Using index: only obtain information from the index tree, without querying back to the table;

Using where: The WHERE clause is used to limit which row matches the next table or is sent to the client. Unless you specifically request or check all rows from the table, the query may have some errors if the Extra value is not Using where and the table join type is ALL or index. Need to return to the table query.

Using temporary: mysql often builds a temporary table to hold the results. Typical cases are when the query contains GROUP BY and ORDER BY clauses that can list columns according to different situations;

For the index principle and explain usage, please refer to the previous article: MySQL index principle, explain in detail

2. Trigger index code example
1. Table creation statement + joint index
CREATE TABLE `student` (
  `id` int(10) NOT NULL,
  `name` varchar(20) NOT NULL,
  `age` int(10) NOT NULL,
  `sex` int(11) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  `phone` varchar(100) DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  `deleted` int (11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `student_union_index` (`name`,`age`,`sex`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2. Use the primary key query


3. Use joint index query


4. Joint index, but inconsistent with the index order


Remarks: Because of the mysql optimizer, if the order is inconsistent with the index, the index will also be triggered, but the order should be as consistent as possible in the actual project.

5. Joint index, but one of the conditions is >


6. Joint index, order by


When where and order by are used together, do not use across index columns.
Shangxuetang brings a brand-new Java 300 set course to students! A must-have high-quality tutorial for self-study Java for zero-basic beginners in java_Hand-in-hand diagrams to learn Java, making learning a kind of enjoyment_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/hutubiancheng/article/details/127305124
Recommended