索引在哪些條件下會失效

2021-09-02 10:10:49 字數 2006 閱讀 9343

is null 或is not null操作(判斷字段是否為空)

判斷字段是否為空一般是不會應用索引的,因為b樹索引是不索引空值的。

select *

from mtl_material_transactions mmt

where mmt.shipment_number is not null

> 及 < 操作符(大於或小於操作符)

大於或小於操作符一般情況下是不用調整的,因為它有索引就會採用索引查詢。

但有的情況下可以對它進行優化,如乙個表有100萬記錄,乙個數值型字段a,30萬記錄的a=0,30萬記錄的a=1,39萬記錄的a=2,1萬記錄的a=3。那麼執行a>2與a>=3的效果就有很大的區別了,因為a>2時oracle會先找出為2的記錄索引再進行比較,而a>=3時oracle則直接找到=3的記錄索引。

like '%xx'將不使用索引,但like 'xx%'可以使用索引

不使用索引

select *

from mtl_material_transactions mmt

where mmt.shipment_number like '%12806557'

『!=』 將不使用索引. 記住, 索引只能告訴你什麼存在於表中, 而不能告訴你什麼不存在於表中.

不使用索引:

select account_name

from transaction

where amount !=0;

使用索引:

select account_name

from transaction

where amount >0;

『||』是字元連線函式. 就象其他函式那樣, 停用了索引.

不使用索引:

select account_name,amount

from transaction

where account_name||account_type=』amexa』;

在這邊測試時有使用索引

使用索引:

select account_name,amount

from transaction

where account_name = 『amex』

and account_type=』 a』;

『+』是數學函式. 就象其他數學函式那樣, 停用了索引.

不使用索引:

select account_name, amount

from transaction

where amount + 3000 >5000;

使用索引:

select account_name, amount

from transaction

where amount > 2000 ;

相同的索引列不能互相比較,這將會啟用全表掃瞄.

不使用索引:

select account_name, amount

from transaction

where account_name = nvl(:acc_name,account_name);

使用索引:

select account_name, amount

from transaction

where account_name like nvl(:acc_name,』%』);

使用upper,to_number也會導致索引失效

不使用索引

select *

from mtl_material_transactions mmt

where upper(mmt.shipment_number) ='12806557'

not in將會導致索引失效

不使用索引

select *

from mtl_material_transactions mmt

where mmt.shipment_number in ('12806557')

索引在哪些情況下會失效?

1 如果條件中有or,即使其中有條件帶索引也不會使用 這也是為什麼盡量少用or的原因 注意 要想使用or,又想讓索引生效,只能將or條件中的每個列都加上索引 2 3.like查詢是以 開頭,可以是 結束,例如 name like 張 這種情況索引不會失效。3 如果列型別是字串,那一定要在條件中將資料...

mysql索引在in條件下失效的原因

1.如果索引欄位是字串,則必須在字段值外加上引號,如 select from notice where villageid in 0 4100000 1.如果資料量很小,mysql會認為掃瞄全表比使用索引快,自然不會使用索引.2.如果查詢結果資料量很多,mysql也不會使用索引.比如style 欄位...

哪些情況下索引會失效?

索引並不是時時都會生效的,比如以下幾種情況,將導致索引失效 如果條件中有or,即使其中有條件帶索引也不會使用 這也是為什麼盡量少用or的原因 注意 要想使用or,又想讓索引生效,只能將or條件中的每個列都加上索引 2.對於多列索引,不是使用的第一部分,則不會使用索引 3.like查詢是以 開頭 4....