MySQL index tuning (1) basic knowledge of indexes

Basic knowledge of index

In the usual delivery of external projects, we often do some POC to quickly develop related web applications. Every time we design the library table structure, we will consider the use of indexes. The index is used well, which is a qualitative improvement for the query experience. The following summarizes the index. Some optimization points, combined with some simple cases (some archives learned before), do a share

Advantages of indexing

  1. Greatly reduce the amount of data that the server needs to scan
  2. Help the server avoid sorting and temporary tables
  3. Turn random io into sequential io

Usefulness of the index

  1. Quickly find rows that match the where clause
  2. Eliminate rows from consideration. If you can choose between multiple indexes, mysql usually uses the index that finds the fewest rows
  3. If the table has a multi-column index, the optimizer can use any leftmost suffix of the index to find the row
  4. When there is a table link, retrieve row data from other tables
  5. Find the min or max value of a specific index column
  6. If sorting or grouping is done on the leftmost prefix of the available index, then the table is sorted and grouped
  7. In some cases, the query can be optimized to retrieve the value without querying the data row

Index classification

  • Primary key index: referred to as the primary key, the original is PRIMARY KEY, composed of one or more columns, used to uniquely identify a record in the data table. A table can have no primary key, but there can only be one primary key at most, and the primary key value cannot contain NULL
  • Unique index: ensure that no two data rows in the table have exactly the same key value to help maintain data integrity
  • Ordinary index: The most basic index, without any restrictions, is the index we often use.
  • Full-text index: Full-text index (FULLTEXT) can only be applied to the data table of the MyISAM engine, but after mysql5.7, innodb also supports it, acting on columns of CHAR, VARCHAR, and TEXT data types.
  • Combined index: Use several columns as one index for retrieval, using the leftmost matching principle.

Data structure used by the index

  • Hash table
  • B+tree
    This part of the follow-up will be listed separately for sharing.

Index matching method

Execute the following table creation statement:

create table staffs(
    id int primary key auto_increment,
    name varchar(24) not null default '' comment '姓名',
    age int not null default 0 comment '年龄',
    pos varchar(20) not null default '' comment '职位',
    add_time timestamp not null default current_timestamp comment '入职时间'
  ) charset utf8 comment '员工记录表';
-----------
alter table staffs add index idx_nap(name, age, pos);
  • Full-value matching: Full-value matching refers to matching with all columns in the index, for example
    explain select * from staffs where name = 'July' and age = '23' and pos = 'dev';
  • Match the leftmost prefix: only match the first few columns, for example:explain select * from staffs where name = 'July' and age = '23'; explain select * from staffs where name = 'July';
  • Match column prefix: you can match the beginning of a column value, for example;explain select * from staffs where name like 'J%';explain select * from staffs where name like '%y';
  • Matching range value: You can find data in a certain range,explain select * from staffs where name > 'Mary';
  • Exactly match one column and range match another column: You can query all of the first column and part of the second column, for exampleexplain select * from staffs where name = 'July' and age > 25;
  • Query that only accesses the index: When querying, you only need to access the index, without accessing the data row, which is essentially a covering index, for example:explain select name,age,pos from staffs where name = 'July' and age = 25 and pos = 'dev';

Guess you like

Origin blog.csdn.net/sun6838693/article/details/115307491