如果where标准是!=,则不使用数据库索引

ero*_*ppa 7 sql database

我有一个列的索引,它在查询时正确使用

select * from Table where x = 'somestring'
Run Code Online (Sandbox Code Playgroud)

但是,当查询类似的时候似乎没有使用它

select * from Table where x != 'someotherstring'
Run Code Online (Sandbox Code Playgroud)

这是正常的还是我在查询中遗漏了其他内容?实际的查询当然要大得多,因此可能是由其他因素造成的.为什么索引不会在查询中使用的任何其他想法?

Chi*_*hip 14

这个是正常的.仅当您具有'='条件时才会使用索引.搜索索引为!=条件无效.

同样,这可能会使用索引(在Oracle中)

select * from Table where x like 'some%'
Run Code Online (Sandbox Code Playgroud)

但这不会

select * from Table where x like '%thing%'
Run Code Online (Sandbox Code Playgroud)

此外, select * from Table where x between 1 and 10 将使用索引

但不是 select * from Table where x not between 1 and 10