Overview of mysql index and commonly used optimization methods

Overview of mysql index and commonly used optimization methods

1. Index Overview An
index is a data structure used by the database storage engine to quickly find records. When the amount of data is huge, it avoids full table scans and increases the efficiency of search. For example, when you want to find a certain content of a certain book, the method of looking through the whole book page by page is obviously too clumsy. Therefore, we generally locate the content of a certain chapter in the book quickly according to the catalog index of the book.
The innoDB engine of mysql that we often use is based on the principle of B-Tree indexing, and it is stored in accordance with the original data format.

Second, the use of the index advantages and disadvantages
advantages:

  • Improve the program efficiency of the system and greatly reduce the amount of data that the server needs to scan
  • Help the server avoid sorting and temporary tables
  • Random I/O can be changed into sequential I/O;
    disadvantages:
  • Add and delete operations are slowed down because the index file needs to be updated accordingly
  • To increase memory,
    we can look at the following table:
CREATE TABLE `price_acct_bill_prod` (
  `acct_bill_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `acct_date` varchar(10) NOT NULL COMMENT '账期yyyyMM',
  `msisdn` varchar(16) NOT NULL COMMENT '产生费用的接入号',
  `imsi` varchar(18) DEFAULT NULL COMMENT '号码对应的imsi',
  `cust_id` bigint(20) DEFAULT NULL COMMENT '客户id',
  `cust_name` varchar(255) DEFAULT NULL COMMENT '客户名称',
  `account_id` bigint(20) NOT NULL COMMENT '账户id',
  `expenses` decimal(16,0) NOT NULL COMMENT '账单费用',
  `price_pro_nbr` varchar(30) DEFAULT NULL COMMENT '价格计划编码',
  `usage_data` bigint(20) DEFAULT '0' COMMENT '使用流量',
  `total_data` bigint(20) DEFAULT '0' COMMENT '累计流量',
  `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `status` int(11) DEFAULT '0' COMMENT '状态 0 :未销账 , 1: 已销账',
  PRIMARY KEY (`acct_bill_id`),
  KEY `msisdn` (`msisdn`),
  KEY `imsi` (`imsi`),
  KEY `cust_acct` (`cust_id`,`acct_date`) USING BTREE,
  KEY `acct_date` (`acct_date`),
  KEY `idx_iccid` (`iccid`)
) ENGINE=InnoDB AUTO_INCREMENT=71319084 DEFAULT CHARSET=utf8 COMMENT='账单';

A lot of indexes have been added to the above table, including primary key indexes, joint indexes, ordinary indexes, etc.; from the figure below we can also see that the index method is the BTree method.
Insert picture description here
So how much memory does the index consume? Just look at the picture below. First of all, there are more than 30 million pieces of data in the table. The data occupies about 9G, and the index occupies about 7G.
Insert picture description here

Three, which fields are suitable for adding indexes
Before answering this question, there is a question, that is, whether the index is the best solution to SQL performance. Refer to the answer of "High Performance Mysq":

Only when the performance benefits of using the index are greater than the performance consumed by itself, the index is worth using. For very small tables, a simple full table scan may be more efficient than using indexes. For general medium and large tables, the index will be very effective. However, for very large tables, the cost of establishing and using indexes has also increased. In this case, a technology is needed to directly distinguish a set of data required by the query, instead of matching one record by one record. For example, partitioning technology can be used.

So what fields are suitable for indexing?

 1.表的主键:很多时候我们都会根据主键的id去查询某条数据,自增主键就是主键索引
 2.查询次数比较多,值有很多的列。比如有个用户表:userId就可以设置成主键,而用户的性别就没啥必要

What are the types of indexes?

 1.主键索引
 2.普通索引
 3.唯一索引
 4.联合(组合)索引

Fourth, the method to optimize the query

  1. MySQL index is the principle of left matching: for example, a user table has a joint index: (name, phone) and ordinary index (company)
-- 走索引的情况
select * from user where name = "tom"   走联合索引
select * from user where name = "tom" and phone = "13222220000" 走联合索引
select * from user where  phone = "13222220000" and name = "tom"  走联合索引
select * from user where company like "alibaba%"   走普通索引
-- 不走索引的情况
select * from user where phone = "13222220000"  不走索引
select * from user where company like "%alibaba"   不走索引
  1. Independent column, cannot be assembled query condition
select * from user where company + "abc" ="libabaabc"   不走索引
  1. If query conditions use or, the conditions must be indexed, as long as there is a condition without index, index query will not be used
  2. Determine whether it is nul
select * from user where company is null   走索引
select * from user where company = null   不走索引
  1. group by does not use indexes, it is a full table scan
  2. Grouping wants to improve efficiency, sorting is prohibited, and order by null is added at the end; the improved efficiency is limited.
  3. Don't use >=, use> directly

5. How to judge whether a certain query sql is indexed? Execution plan: explain
directly: explain + sql
Insert picture description here
view the index execution structure in the figure above: type=ref,key=name, which means that this sql query uses an index, and the name is the index; the
following simulates a situation without the index:
Insert picture description here
type=ALL: means full table scan, key=Null, no index is taken.
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_37488998/article/details/110421463