Two, mysql optimization

A, expain plan analysis

Second, create an index

Third, the specific optimization

1、max

General index created to solve

 

2、count

 When the count statistics, if the count (*), will the bank also counted in the empty null, count (field names), not null field will go in statistics, create index and the same max

 

3, how to order by, ordering avoid using filesort

 

 

 1, the first case analysis, indexing, in the following order (name, age, salary)

 

2, the third case

 

4, group by, grouping how to avoid using temporary, compliance with the rules of the order by the same

 

5、limit

 

6, in the exists, small table-driven large table

5 is connected, operations 5000 times, the most resource-consuming connections 

for ( int I = 0 ; I < 5 ; I ++ ) { 

  for ( int I = 0 ; I < 5,000 ; I ++ ) { 
  } 

}
select * from t1 where id in (select id from where t2)
执行顺序是,先t2,再t1,t2是小表,t1是多大表
for(t2){
  for(t1){
  }
}
select * from t1 where exist (select 1 from where t2.id = t1.id)
执行顺序是,先t1,再t2,t2是小表,t1是多大表,所以这种就会t1大表驱动小表,不推荐
for(t1){
  for(t2){
  }
}

7、索引失效的条件

 

Guess you like

Origin www.cnblogs.com/caohanren/p/12405861.html