mysql index and its optimization

index

1. Basic sql statement

1. Create an index

CREATE  [UNIQUE | FULLTEXT | SPATIAL]  INDEX  索引名 ON  表名(字段名) [USING 索引方法]

2. Delete the index

drop index 索引名

3. View the index

show index on tablename;

2. Reasons for performance degradation

1. Badly written query statements
2. Index failure: single-value index, composite index
3. Too many joins (design flaws or non-uniform requirements)
4. Server tuning and various parameter settings (cache, number of threads)

3. Advantages

  1. Speed ​​up queries
  2. Reduce sorting

4. Disadvantages

  1. Increase the gap lock, slow down the speed of updating data
  2. Take the time to build an optimized index

5. Classification

  1. Single value index: only contains a single column
  2. Unique index: the value in the index is unique and can be empty
  3. Composite index: index with multiple columns

6. Search Principle

  1. B tree
  2. hash
  3. r tree
  4. full text

7. Under what circumstances create an index

  1. The primary key is automatically indexed (the user name and the zone in the king will not be the joint primary key, right?? Do not update frequently, and the lottery can be carried out using the user name and partition!)
  2. Foreign key indexing
  3. Indexing of fields used for grouping statistics and sorting
  4. High concurrency tends to build a joint index
  5. Indexing of frequently queried fields

8. Under what circumstances will the index not be established?

  1. Fields that are not used in where, such as the user's own profile
  2. Frequently updated fields do not make sense of indexing
  3. There are very few records in the table below 3 million
  4. The repetition rate in the data is too high

9. Performance analysis

1.explain

  1. use
  2. type
  3. extra
  4. row
  5. possilble key
  6. key
  7. table
  8. id
  9. ref

2. Slow query log

4. Batch data script

5.show profile

6. Global log query

7. General idea

  1. Start and capture the slow query log.
  2. explain + sql analysis.
  3. show profile queries the execution details and life cycle of SQL in the server.
  4. Parameter tuning of the sql data server.

10. Performance optimization

1. Single table connection

2. Two tables are connected

1. Left and right connection plus opposite index (left connection plus right table, right connection plus left table)

3. Three-table optimization case

  1. Small table drives big table
  2. Optimize the subset first
  3. Ensure that the join table has optimized statements
  4. Don't be stingy with join buffer

4. Index optimization principle

  1. Full match my favorite
create index on user a('c', 'd', 'e');
select * from user where c = 'x';  #可以用到索引
select * from user where d = 'y' and  e = 'z' #索引失效了

第二个违背了最佳左前缀法则:
建立索引的第一字段不能丢(索引失效)
建立索引的中间字段不能断(否则部分使用索引)
  1. Do not do any function operations on the index (calculation, function, type conversion)
select * from user where left(id, 4) = 1;#在索引left上使用了函数

  1. After the range is used, all invalidation, try to select (select) the indexed field
select * from user where id = 4 and age > 1 and name = "te";#使用了范围
范围之后的就失效了, id会用到索引,但是age和age之后就会出现索引失效
select id, age, name where id = 4 and age  > 1 and name = "te"#会使用索引,所以仍旧是使用索引
  1. Using <>, != ,is null, null ,is not null ,like "%name", etc. will also cause the index to fail
select * from name where name like "%name";#索引失效
select * from name where name like "name%";#like 百分在右边

What if you have to load the left side?
Use a covering index

create index on user idx_name_age(`name`, `age`);
select name, from user where name like "%tea%";
  1. The varchar type must be enclosed in single quotes
select * from name = '3000';
select * from name = 3000;#索引失效
都能查到,但是出现了隐式的类型转换

The leading eldest brother cannot die, and the middle brother cannot break; the percent sign after the like, the string type is quoted, all after the range is invalid.

  1. group by may produce temporary tables
  2. The small table drives the big table.
select * from A where id in (select id from B);#B表小于大表
select * from A where exists(select id from B);#A表小于大表
exists就是把A中的字段放入子查询寻中进行比较。

Guess you like

Origin blog.csdn.net/fuzekun/article/details/104462447