当前位置:网站首页>[learning] database: several cases of index failure

[learning] database: several cases of index failure

2022-07-05 06:20:00 Daidou family

Seven cases of index failure

Column and column comparison index failure

  • Two columns are indexed separately , The following situations will not be indexed
select * from test where id = c_id

Column existence NULL Value index failure

  • When we talk about database tables , It should be avoided as much as possible NULL Value appears
  • If it can't be avoided , You'd better give one, too default The default value is
  • The default value can be 0,-1
  • The default value of the string can be “ An empty string ”

NOT Condition index failure

where The following conditions will cause the index to fail

  • <>
  • in
  • not in
  • not exists
select * from test where id<>500;
select * from test where id in (1,2,3,4,5);
select * from test where not in (6,7,8,9,0);
select * from test where not exists (select 1 from test_02 where test_02.id=test.id);

LIKE Wildcard index failure

  • Try to use post wildcards for example ’name%’, Because when walking the index, it will match the index before , It can be found at this time
  • If it is ‘%name’ Conditions of the query Pre wildcard
  • The execution plan prefers to choose full table scanning , Therefore, the index will be invalid

The query condition includes function index invalidation

  • In terms of query conditions, try not to use functions for indexes
  • ABS,UPPER,DATE,DAY,YEAR etc.
  • for example :
select * from test where upper(name) = 'SUPPER';
select * from test where DATE(swap_time) = DATE( date_sub( CURRENT_DATE, INTERVAL 1 DAY ) );
  • This is not going to be indexed , Because the index may be different when it is built and after calculation , Unable to locate index .
  • But if the query condition is not to calculate the index column , So you can still go through the index , for example :
select * from test where name = upper('sunnnn');

Data type hermit conversion index failure

  • When the query condition has hermit transformation , The index will fail
  • For example, in the database id yes NUMBER type , But at the time of inquiry , But in the following form :
select * from test where id = '123';

If msyql When you think full table scanning is faster ( Less data ) Index failure

When is index unnecessary ?

When the uniqueness of the field is poor

Frequently updated fields do not need to be

where Fields not used in conditions

Index usage <> when , Results the general

原网站

版权声明
本文为[Daidou family]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140615147883.html