資料庫索引失效情況

2021-10-07 23:00:10 字數 1434 閱讀 9913

索引失效

1.建立了單字段索引,where條件內多個字段

alter table `test` 

add index `test_index`(`name`) using btree;

select * from test where name = 'test' and *** = 0
2.建立聯合索引,where條件單個字段
alter table `test` 

add index `test_index`(`name`,`***`) using btree;

select * from test where name = 'test';
3.對索引使用內部函式
alter table `test` 

add index `test_index`(`name`) using btree;

select * from test where round(`name`) = 'test'
4.使用is null ,is not null
select * from test where name is null;

select * from test where name is not null;

5.型別錯誤,如欄位型別為varchar,where條件用number
錯誤:select * from test where name = 1;

正確:select * from test where name = '1';

6.對索引列運算,運算包括(+、-、*、/、!、<>、%、like』%_』(%放在前面)、or、in、exist等)
select * from test where name like '%你好';  -- 不生效;

select * from test where name like '你好%'; -- 生效

7.復合索引(復合索引遵守「最左字首」原則,即在查詢條件中使用了復合索引的第乙個字段,索引才會被使用)
alter table `test` 

add index `test_index`(`name`,`***`,`address`) using btree;

select * from test where *** = '1' and address = 'test' ; -- 不生效

select * from test where address = 'test' -- 不生效

select * from test where name = 'test' and *** = '1' and address = 'test'; -- 生效 ()

資料庫索引失效的情況

1 列與列之間的對比比如一張表中的兩列 id和c id 都單獨建立了索引,但是下面這種情況不會走索引 select from table where id c id 這種情況會被認為還不如走全表掃瞄2 存在null值我們在設計資料庫時盡量避免null值的出現,如果不可避免地要出現null值的情況,也...

資料庫索引 索引失效

以下情況不走索引 select from student where name like xiaoyao select from student where not score 100 select from student where score 100 select from student w...

資料庫索引失效

索引失效 1 沒有查詢條件,或者查詢條件沒有建立索引 2 在查詢條件上沒有使用引導列 3 查詢的數量是大表的大部分,應該是30 以上。4 索引本身失效 5 查詢條件使用函式在索引列上 見12 6 對小表查詢 7 提示不使用索引 8 統計資料不真實 9 cbo計算走索引花費過大的情況。其實也包含了上面...